Basic Sorting Algorithms: Example Designs
Swapping Elements
1 procedure SWAP(XS, I, J) This procedure simply explains what we
mean by swapping two elements in an
2 TEMP = XS[I]
array, which is a useful action in many
3 XS[I] = XS[J] sorting algorithms.
4 XS[J] = TEMP
5 end procedure
Selection Sort
1 procedure SELECTION_SORT(XS, N) Notice in line 4 that J starts looping from I
instead of 0; this is because we know that
2 loop I from 0 to N - 1 the elements from 0 to I are already
3 LEAST_INDEX = I sorted.
4 loop J from I to N - 1
5 if XS[J] < XS[LEAST_INDEX] then
6 LEAST_INDEX = J
7 end if
8 end loop
9 SWAP(XS, LEAST_INDEX, I)
10 end loop
11 end procedure
Bubble Sort
Notice in line 2 that we only loop I to the
1 procedure BUBBLE_SORT(XS, N) second last position of the array; this is
2 loop I from 0 to N - 2 because we are comparing each element
3 loop J from 0 to N - 2 - I to the element on its right and we don't
4 if XS[J] > XS[J + 1] then want to go out of bounds.
5 SWAP(XS, J, J + 1)
Also notice in line 3 that we subtract I
6 end if
from the upper bound of J; this is because
7 end loop we know that the elements after this point
8 end loop are already sorted.
9 end procedure
Insertion Sort
Notice in line 2 that we start from index 1
1 procedure INSERTION_SORT(XS, N)
so that we can safely compare the
2 loop I from 1 to N - 1 previous element without going out of
3 J = I bounds.
4 loop while J > 0 and XS[J - 1] > XS[J]
5 SWAP(XS, J, J - 1) Also notice in line 4 that we only continue
6 J = J - 1 swapping elements while the previous
element is greater than the element we
7 end loop
are inserting; this is because we know that
8 end loop all elements after that are already sorted.
9 end procedure
Basic Searching Algorithms: Example Designs
Sequential Search (Random Access)
Here, HAYSTACK is an array
1 function SEQUENTIAL_SEARCH(HAYSTACK, N, NEEDLE)
that is not necessarily
2 loop I from 0 to N - 1 already sorted.
3 if HAYSTACK[I] = NEEDLE then
4 return true
5 end if
6 end loop
7 return false
8 end function
Sequential Search (Sequential Access)
1 function SEQUENTIAL_SEARCH(HAYSTACK, NEEDLE) Here, HAYSTACK is a
2 HAYSTACK.resetNext() collection (and hence
cannot be randomly
3 loop while HAYSTACK.hasNext()
accessed).
4 if HAYSTACK.getNext() = NEEDLE then
5 return true
6 end if
7 end loop
8 return false
9 end function
Binary Search
1 function BINARY_SEARCH(HAYSTACK, N, NEEDLE) Here, HAYSTACK is an array
2 // Precondition: HAYSTACK is sorted that is assumed to already
3 START = 0 have been sorted.
4 END = N - 1
5 loop while START ≤ END
6 MID_POINT = (START + END) div 2
7 if HAYSTACK[MID_POINT] = NEEDLE then
8 return true
9 else
10 if HAYSTACK[MID_POINT] < NEEDLE then
11 START = MID_POINT + 1
12 else
13 END = MID_POINT - 1
14 end if
15 end if
16 end loop
17 return false
18 end function
Binary Search (Recursive)
The array HAYSTACK is
1 function BINARY_SEARCH(HAYSTACK, START, END, NEEDLE)
assumed to already be
2 // Precondition: HAYSTACK is sorted sorted.
3 if START > END then
4 return false Notice the breakout
5 end if condition in lines 3—5.
6 MID_POINT = (START + END) div 2
7 if HAYSTACK[MID_POINT] = NEEDLE then
8 return true
9 else
10 if HAYSTACK[MID_POINT] < NEEDLE then Recursion occurs in lines 10
11 return BINARY_SEARCH( and 18. (Notice that all
variables are treated as
12 HAYSTACK,
constants.)
13 MID_POINT + 1,
14 END,
15 NEEDLE
16 ) When we implement binary
17 else search in languages that
pass by value by default, we
18 return BINARY_SEARCH(
should explicitly pass
19 HAYSTACK, HAYSTACK by reference to
20 START, prevent a copy of
21 MID_POINT - 1, HAYSTACK from being
22 NEEDLE passed in each call. For
example, C++ passes vector
23 )
objects by value whereas
24 end if Python passes list objects
25 end if by reference.
26 end function