The selection sort algorithm is a simple sorting technique that sorts an array by repeatedly finding the minimum element from the unsorted part and placing it at the beginning. The process continues until the entire array is sorted.
Contribute Materials
Your contribution can guide someone’s learning journey. Share your
documents today.
Blog: Selection Sort (Database programming) 1
Secure Best Marks with AI Grader
Need help grading? Try our AI Grader for instant feedback on your assignments.
Selection Sort (Database programming) 6thAugust 2019 Selection Sort Algorithm The selection sort algorithm can be referred to as the process through which values are sorted. For sorting a data in the ascending order, a comparison is undertaken between the 0thelement and the other elements. If it is found that the 0thelement is greater in value than the element it is being compared with, there is an interchange between the two values. What happens here is that, after the first iteration occurs, the element with the smallest value is placed at the 0thposition. The process is undertaken continuously until there is sorting out of the entire array. Selection sort can be defined as the simplest sorting algorithm. It first finds out the smallest element within the array and then it swaps that element with the one in the first position. After that, it searches for the second smallest element and will swap it with the element in the second position. This process will be continued till the whole array has been sorted.The figure that is represented below depicts the technique in which the selection sort takes place. 2
An example of the process through which selection sort is done in programming is as follows: #include<iostream> usingnamespacestd; intmain() { inta[100],i,n,p,k,min,loc,tmp; cout<<"\n------------ SELECTION SORT ------------ \n\n"; 3
cout<<"Enter No. of Elements:";cin>>n; cout<<"\nEnter Elements:\n"; for(i=1;i<=n;i++){cin>>a[i]; } for(p=1;p<=n-1;p++)// Loop for Pass { min=a[p];// ElementSelection loc=p; for(k=p+1;k<=n;k++)// Finding Min Value { if (min > a[k]) { min=a[k]; loc=k; } } tmp=a[p]; a[p]=a[loc]; a[loc]=tmp; } cout<<"\nAfter Sorting: \n"; for(i=1;i<=n;i++){ cout<<a[i]<<endl; 4
Paraphrase This Document
Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
} } Selection sort algorithm is also defined as the process which helps in sorting an array by adopting the process where repeatedly the minimum element is found out in an ascending order. The sorting is done from the unsorted part and it is put in the beginning. The algorithm ensures that there are two sub-arrays within a given array. It consists of the sub-array that has already been sorted and the sub-array which is remaining and is unsorted. In each of the iterations of selection sort, the minimum element that derived through ascending order from the sub-array that is unsorted is selected and shifted to the sub-array that is sorted. The steps that have been discussed above can be more effectively depicted with the help of the following example. 5
A Flowchart of Selection Sort 6 arr[] = 64 25 12 22 11 // Find the minimum element in arr[0...4] // and place it at beginning 11 25 12 22 64 // Find the minimum element in arr[1...4] // and place it at beginning of arr[1...4] 11 12 25 22 64 // Find the minimum element in arr[2...4] // and place it at beginning of arr[2...4] 11 12 22 25 64 // Find the minimum element in arr[3...4] // and place it at beginning of arr[3...4] 11 12 22 25 64