Selection Sort
A selection sort algorithm sorts the list by selecting the largest element in the unsorted list and places it at the appropriate position (i.e., swapping). In each pass, the largest element in the list is placed at the appropriate position. If there are n elements in the list.
Selection sort advantages and drawbacksÂ
selection sort implementation
#include<stdio.h> #define size 10 int main() { int arr[size]; int i,j,min; int temp; printf("\nENTER %d VALUES: ",size); for(i=0;i<size;i++) scanf("%d",&arr[i]); for(i=0;i<size; i++) { min=i; for(j=i+1;j<size;j++) { if(arr[j]<arr[min]) min=j; } temp=arr[min]; arr[min]=arr[i]; arr[i]=temp; } printf("\nAFTER SORTING: "); for(i=0;i<size;i++) printf("%d ", arr[i]); return 0; }
Analysis
As we have seen selection sort algorithm will search the smallest element in the array and then that element will be a proper position. So in pass 1, it will compare n-1 elements the same process will be for pass 2 but this comparison will be n-2 because the first element is already at the proper position. The elements, which are already at the correct position, will not be disturbed.