Blog Page.

Selection-Sort (C#)


Thomas J. Fournier


//Selection Sort(Array[])
//   for i = 0 to Array.Length
//      //set current element as the minimum
//      min = i
//      //check the element to be minimum
//      for j = i + 1 to Array.Length 
//         if list[j] < list[min] then
//            min = j;
//      // swap the minimum element with the current element*/
//      if min != i then
//         swap Array[min] and Array[i]

public static void SelectionSort(int[] integerArray) {
	int n = integerArray.Length;
	for (int i = 0; i < n; i++) {
		int min = i;
		for (int j = i + 1; j < n; j++) {
			if (integerArray[j] < integerArray[min]) {
				min = j;
			}
		}
		if (min != i) {
			int temp = integerArray[min];
			integerArray[min] = integerArray[i];
			integerArray[i] = temp;
		}
	}
}