Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time

STD 01 - Selection Sort

Intro

Selection sort works with two subarrays: the sorted section, and the unsorted section. It works by taking the minimum value in the unsorted array, putting it at the end of the sorted array, and continuing until there is no unsorted array.

sort

Task

Implement this pseudocode in

  • Python,
  • or C++,
  • or (if you like) both,

adding comments as necessary. Be aware that min may be a protected term so you may need a different variable name e.g. minn. Note that a swap needs to be performed, and this, in the pseudocode, is the subfunction SWAP. You need to work out how to implement the swap. This does not have to be done using a subfunction: the swap could be implemented directly – the choice is yours.

Pseudo-code

SELECTION_SORT(A)
	FOR i TO length(A)-1
		min ← i
		FOR j ← i + 1 TO length(A)
			IF A[j] < A[min]
				min ← j
		SWAP (A, i, min)
	RETURN A