Selection Sort:
The detailed explanation and algorithm can be found here: https://rishabhjainiitbhu.blogspot.com/2019/08/sorting-algorithms.html#more
The basic algorithm was:
Start at the beginning of an array, pass through the array finding the minimum element, put it at the first location in the array. In the next round we find the second minimum, place it at the second location of the array. So on and so forth.
Here is the code:
void selectionSort(int* arr, int len){ | |
for (size_t i = 0; i < len; i++) { | |
int min=i; | |
for (size_t j = i; j < len; j++) { | |
if(arr[j]<=arr[min]){ | |
min = j; | |
} | |
} | |
if(min == i) | |
break; | |
swap(&arr[i], &arr[min]); | |
} | |
} |
No comments:
Post a Comment