use of selection sort algorithm code example
Example 1: selection sort
using namespace std;
void selectionSort(int arr[], int n){
int i,j,min;
for(i=0;i<n-1;i++){
min = i;
for(j=i+1;j<n;j++){
if(arr[j] < arr[min]){
min = j;
}
}
if(min != i){
swap(arr[i],arr[min]);
}
}
}
int main()
{
int arr[] = { 1,4,2,5,333,3,5,7777,4,4,3,22,1,4,3,666,4,6,8,999,4,3,5,32 };
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
for(int i = 0; i < n; i++){
cout << arr[i] << " ";
}
return 0;
}
Example 2: Selection sort
SelectionSort(List) {
for(i from 0 to List.Length) {
SmallestElement = List[i]
for(j from i to List.Length) {
if(SmallestElement > List[j]) {
SmallestElement = List[j]
}
}
Swap(List[i], SmallestElement)
}
}