`
Selection Sort works on the principle of iterating through the array, find the smallest element to the right of the current index and exchange it with the element at the current index. At any given moment the array at the left of the current index is already sorted. This article gives a visual representation of selection sort in action.
Let A = {A1, A2, …., AN} where N ∈ Z
FOR i = 0 TO N-1 do:
minIndex = i
FOR j = i+1 to N do:
IF A[j] < A[min] do:
min = j
END IF
END FOR
temp = A[i]
A[i] = A[minIndex]
A[minIndex] = temp
END FOR
On average, Selection Sort does N2/2 comparsions and N swaps. Hence, the runtime complexity of the program is of the order O(N2).
An experiment can be conducted to verify the runtime of the selection sort using the following steps:
The ratio of two times is close to 4 as:
\(T(N) \propto N^2\)
\(T(N) = c_1 N^2\)
\(T(2N) \propto (2N)^2\)
\(T(2N) = c_2(2N)^2\)
\(T(2N) = 4c_2N^2\)
\(\dfrac{T(2N)}{T(N)} = \dfrac{4c_2N^2}{c_1 N^2}\)
\(\dfrac{T(2N)}{T(N)} = 4c\)
Assuming c to be small for large size inputs
\(\dfrac{T(2N)}{T(N)} \approx 4\)
Bubble Sort is a comparison based sort in which each element in the input list is compared against every other element and exchanged if necessary. It is named as bubble sort as the largest elements bubbles to the end of the list.
Let A = {A1, A2, …., AN} where N ∈ Z
FOR i = N-1 TO 1 do:
FOR j = 0 to i-1 do:
IF A[j] > A[i] do:
temp = A[j]
A[j] = A[i]
A[i] = temp
END IF
END FOR
END FOR
Bubble sort is rarely used in practice because of its poor peformance.
Irrespective of the input characterstics, there are
\(\dfrac{N*(N-1)}{2}\)
comparisons making it a poor alternative
compared to other sorts such as insertion sort.
Insertion Sort can be thought as of sorting a hand while playing cards where we pick one card at a time and place it in proper position in the cards already held in hand.
Let A = {A1, A2, …., AN} where N ∈ Z
FOR i = 1 to N-1 do:
FOR j = i to 1 do:
IF A[j] >= A[j-1]:
break
END IF
temp = A[j]
A[j-1] = A[j]
A[j] = temp
END FOR
END FOR
For random arrays, insertion sort uses O(N2) compares and O(N2) exchanges. For already sorted arrays, there are O(N) compares and 0 exchanges making this algorithm ideal for nearly sorted or smaller arrays.