0

Selection sort algorithm?

First of all I want to apologize for my English. As far as I know about the Selection sort alogrithm, it searches through the array the smallest value and swaps it with the leftmost number and so on until the entire array is sorted. However, in the SoloLearn's provided alogrithm I can't see any search through the array. Actually it reminds me of the bubble sort algorithm because it compares the first value of the array with the next one and so on ( https://code.sololearn.com/c76BVmoFy5lG/#cs ). Am I right? I did my own "Selection sort alogirthm" that actually searches through the array the smallest value using Array.IndexOf(), although it doesn't swaps anything but places the sorted numbers in a temporal array. ( https://code.sololearn.com/cqbnXKf1Vt73/#cs ) So I wanted to know whether I am right or not about the sololearn's selection sort algorithm and the theory behind it and what you guys think about my solution.

2nd Jul 2018, 6:54 PM
Piero
Piero - avatar
1 ответ
+ 5
You added { } to the inner for loop that are not in SoloLearn's code. It has: static void SelectionSort(int[] arr) { for(int k = 0; k < arr.Length-1; k++) { int min_idx = k; for(int j = k+1; j < arr.Length; j++) if (arr[j] < arr[min_idx]) min_idx = j; //swap int temp = arr[min_idx]; arr[min_idx] = arr[k]; arr[k] = temp; } }
2nd Jul 2018, 7:03 PM
John Wells
John Wells - avatar