#Sorting#Computer-Science#CS251 Loops through the array, for each index finds the position it goes in the array and swaps it with the element at that position.

Psuedocode

SelectionSort(A: array of size n)
	for (i = 0, i < n-1, 1)
		index <- i
		for (j = i+1, j < n, 1)
			if (A[j] < A[index])
				index <- j
			endif
		endfor
		if (index != i)
			swap(A, i, index)
		endif
	endfor
end SelectionSort

Details

  • Is Unstable
  • Is in place
  • Time Complexity
    • If Array is sorted
      • # Compares =
      • # Swaps =
    • If array is in opposite order
      • # Compares =
      • # Swaps =

Best Case

Worst Case