C Program For Selection Sort Algorithm in Data Structure
Learn How To Sort Integer Arrays using Selection Sort Algorithm in C Programming Language. It is important that we should know about How A For Loop Works before getting further with the C Program Code. The selection sorting technique is one of the simplest and most basic level of sorting algorithm.
What is Selection Sort Algorithm?
Selection Sort Technique scans the List and Searches for the Smallest Element. This Smallest Element is then replaced with the First Element of the Integer Array or the List. Then the Second Smallest Element is Search for and is replaced with the Second Element of the List.
Selection Sort Algorithm Analysis
Selection Sorting Algorithm should be used for Large Lists. Selection Sort is Not a Stable Sort. Since it requires only one Temporary variable, it is an In-Place Sort. Space Complexity is O(1). Efficiency of Selection Sort To Sort an Integer Array (List) is O(n2) in all the cases viz., Worst Case, Best Case.
C Program To Sort Arrays using Selection Sort Algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | #include<stdio.h> int main() { int elements[20], limit; int i, j, temp, smallest; printf("\nEnter Array Limit:\t"); scanf("%d", &limit); printf("\nEnter %d Elements in Array\n", limit); for(i = 0; i < limit; i++) { scanf("%d", &elements[i]); } for(i = 0; i < limit - 1; i++) { smallest = i; for(j = i + 1; j < limit; j++) { if(elements[smallest] > elements[j]) smallest = j; } if(i != smallest) { temp = elements[i]; elements[i] = elements[smallest]; elements[smallest] = temp; } } printf("\nSorted Array\n"); for(i = 0; i < limit; i++) { printf("%d\t", elements[i]); } printf("\n"); return 0; } |
Output

If you have any compilation errors or doubts in this Code To Sort Array using Selection Sort C Program in Data Structures, let us know about in the Comment Section below.