voidselection_sort(int arr[], int len){ int i, j, min, temp; for (i = 0; i < len - 1; i++) { min = i; for (j = i + 1; j < len; j++) if (arr[min] > arr[j]) min = j; temp = arr[min]; arr[min] = arr[i]; arr[i] = temp; } }
Python
defselection_sort(L): N = len(L) exchanges_count = 0 for i in range(N-1): min_index = i for j in range(i+1, N): if L[min_index] > L[j]: min_index = j if min_index != i: L[min_index], L[i] = L[i], L[min_index] exchanges_count += 1 print('iteration #{}: {}'.format(i, L)) print('Total {} swappings'.format(exchanges_count)) return L
publicstaticvoidselection_sort(int[] arr){ int i, j, min, temp, len = arr.length; for (i = 0; i < len - 1; i++) { min = i; for (j = i + 1; j < len; j++) if (arr[min] > arr[j]) min = j; temp = arr[min]; arr[min] = arr[i]; arr[i] = temp; } }
JavaScript
Array.prototype.selection_sort = function() { var i, j, min; var temp; for (i = 0; i < this.length - 1; i++) { min = i; for (j = i + 1; j < this.length; j++) if (this[min] > this[j]) min = j; temp = this[min]; this[min] = this[i]; this[i] = temp; } };