CSC 385 - Data Structures and Algorithms
University of Illinois Springfield
College of Health, Science, and Technology
\[ \begin{align*} S(16) &= (\frac{n}{4})^2 + (\frac{n}{4})^2 + (\frac{n}{4})^2 + (\frac{n}{4})^2 + 2n \\ &= 4(\frac{n}{4})^2 + 2n \\ &= 4(\frac{16}{4})^2 + 2(16) \\ &= 4(4^2) + 32 \\ &= 64 + 32 \\ &= 96 \end{align*} \]
Dividing and merging
\[ \begin{align*} T(1) &= 0 \\ T(n) &= 2T(n/2) + n \end{align*} \]
a1 = [14 19 20 27] a2 = [8 10 15 25]
| a1 | a2 | Temp |
|---|---|---|
| [14 19 20 27] | [8 10 15 25] | [] |
| [-14- 19 20 27] | [-8- 10 15 25] | [8] |
| [-14- 19 20 27] | [8 -10- 15 25] | [8 10] |
| [-14- 19 20 27] | [8 10 -15- 25] | [8 10 14] |
| [14 -19- 20 27] | [8 10 -15- 25] | [8 10 14 15] |
| [14 -19- 20 27] | [8 10 15 -25-] | [8 10 14 15 19] |
| [14 19 -20- 27] | [8 10 15 -25-] | [8 10 14 15 19 20] |
| [14 19 20 -27-] | [8 10 15 -25-] | [8 10 14 15 19 20 25] |
[10 1 6 3 4 9 7 8 0][10 4 0][1 3 0] and [10 6 9 7 8]\[ \begin{align*} T(1) &= 0 \\ T(n) &= 2T(\frac{n}{2}) + n \end{align*} \]
\[ \begin{align*} T(1) &= 0 \\ T(n) &= T(n - 1) + T(0) + n \\ &= T(n - 1) + n \end{align*} \]
a = [0 3 7 8 2 1 4]
[0 3 7 4 2 1 8], pivot is 4[0 3 7 1 2 4 8], pivot is 4a = [0 3 7 1 2 4 8]; leftindex = 2; rightindex = 4, 7 > 4 and 2 < 4 so swap and increment leftindex and decrement rightindexa = [0 3 2 1 7 4 8]; leftindex = 3; rightindex = 3, 1 < 4 so increment leftindexa = [0 3 2 1 7 4 8]; leftindex = 4; rightindex = 3, leftindex > rightindex so we swap the pivot with the element at leftindexa = [0 3 2 1 4 7 8]; leftindex = 4; rightindex = 3, pivot is in its properly sorted location ending the first partition[123 398 210 19 528 3 513 129 220 294]| digit | bucket |
|---|---|
| 0 | [210 220] |
| 1 | [] |
| 2 | [] |
| 3 | [123 3 513] |
| 4 | [294] |
| 5 | [] |
| 6 | [] |
| 7 | [] |
| 8 | [398 528] |
| 9 | [19 129] |
| digit | bucket |
|---|---|
| 0 | [210 220] |
| 1 | [] |
| 2 | [] |
| 3 | [123 3 513] |
| 4 | [294] |
| 5 | [] |
| 6 | [] |
| 7 | [] |
| 8 | [398 528] |
| 9 | [19 129] |
[210 220 123 3 513 294 398 528 19 129][210 220 123 3 513 294 398 528 19 129]| digit | bucket |
|---|---|
| 0 | [3] |
| 1 | [210 513 19] |
| 2 | [220 123 528 129] |
| 3 | [] |
| 4 | [] |
| 5 | [] |
| 6 | [] |
| 7 | [] |
| 8 | [] |
| 9 | [294 398] |
| digit | bucket |
|---|---|
| 0 | [3] |
| 1 | [210 513 19] |
| 2 | [220 123 528 129] |
| 3 | [] |
| 4 | [] |
| 5 | [] |
| 6 | [] |
| 7 | [] |
| 8 | [] |
| 9 | [294 398] |
[3 210 513 19 220 123 528 129 294 398][3 210 513 19 220 123 528 129 294 398]| digit | bucket |
|---|---|
| 0 | [3 19] |
| 1 | [123 129] |
| 2 | [210 220 294] |
| 3 | [398] |
| 4 | [] |
| 5 | [513 528] |
| 6 | [] |
| 7 | [] |
| 8 | [] |
| 9 | [] |
| digit | bucket |
|---|---|
| 0 | [3 19] |
| 1 | [123 129] |
| 2 | [210 220 294] |
| 3 | [398] |
| 4 | [] |
| 5 | [513 528] |
| 6 | [] |
| 7 | [] |
| 8 | [] |
| 9 | [] |
[3 19 123 129 210 220 294 398 513 528]radix sort(collection) {
maxdigits = maximum number of digits in collection
digitPosition = 1
buckets = array of lists of size 10
for i in 1 to maxdigits {
for index in 0 to length(collection) - 1 {
digit = (collection[index] / digitPosition) % 10
buckets[digit].add(collection[index])
}
read buckets back into collection
clear buckets
digitPosition *= 10
}
}
| Algorithm | Average Case | Best Case | Worst Case |
|---|---|---|---|
| Radix Sort | \(O(n)\) | \(O(n)\) | \(O(n)\) |
| Merge Sort | \(O(n log n)\) | \(O(n log n)\) | \(O(n log n)\) |
| Quick Sort | \(O(n log n)\) | \(O(n log n)\) | \(O(n^2)\) |
| Shell sort | \(O(n^{1.5})\) | \(O(n)\) | \(O(n^2)\) or \(O(n^{1.5})\) |
| Insertion Sort | \(O(n^2)\) | \(O(n)\) | \(O(n^2)\) |
| Selection Sort | \(O(n^2)\) | \(O(n^2)\) | \(O(n^2)\) |

CSC 385 - Data Structures and Algorithms