Sorting & Sorting
Algorithms
Sorting
1
2
Goals
●Identify the sorting problem
●Define Stable vs. Unstable Sorts
●Explore 3 Simple Sorting Algorithms
○Selection Sort
○Bubble Sort
○Insertion Sort
Sorting
3
The Sorting Problem
Given a list or array of elements (usually of uniform type)
Return:
●Nothing (input list is modified) –in-place sorting
●A new list in sorted order
Post-Condition: after the function, the list is sorted:
●(ascending) each element is ≥ the one before it
●(descending) each element is ≤ the one before it
Sorting
4
Imagine you have the following unsorted list:
[ 30 | 10 | 3 | 45 | 50 | 100 | 0 ]
How would you go about sorting it?
Sorting
5
Perhaps we move the smallest element to
be first by swapping 0 with 30.
[ 30 | 10 | 3 | 45 | 50 | 100 | 0]
[ 0| 10 | 3 | 45 | 50 | 100 | 30 ]
Sorting
6
Then second smallest by swapping 3 with
10…
[ 0| 10 | 3| 45 | 50 | 100 | 30 ]
[ 0| 3| 10 | 45 | 50 | 100 | 30 ]
Sorting
7
Ten is in the correct place, so we don’t need
to move any element.
[ 0| 3| 10 | 45 | 50 | 100 | 30 ]
Sorting
8
We move 30 to the correct location, then
swap a few more positions…
[ 0| 3| 10 | 45 | 50 | 100 | 30 ]
….
[0 | 3 |10 |30 |45 |50 |100 ]
Sorting
9
Sorting Problem: Solutions
●Many sorting algorithms
●Some are faster/slower than others
●Some use more/less memory than others
●Some work better with specific kinds of data
●Some can utilize multiple computers / processors
Sorting
10
Properties of Sorting Algorithms
●Time Complexity (Big-O)
Sorting
11
Properties of Sorting Algorithms
●Time Complexity (Big-O)
i. “How does the amount of work performed scale with input size?”
Sorting
12
Properties of Sorting Algorithms
●Time Complexity (Big-O)
i. “How does the amount of work performed scale with input size?”
●Space Complexity
Sorting
13
Properties of Sorting Algorithms
●Time Complexity (Big-O)
i. “How does the amount of work performed scale with input size?”
●Space Complexity
i. “How does the amount of extra space used scale with input size?”
Sorting
14
Properties of Sorting Algorithms
●Time Complexity (Big-O)
i. “How does the amount of work performed scale with input size?”
●Space Complexity
i. “How does the amount of extra space used scale with input size?”
●Stability
Sorting
15
Properties of Sorting Algorithms
●Time Complexity (Big-O)
i. “How does the amount of work performed scale with input size?”
●Space Complexity
i. “How does the amount of extra space used scale with input size?”
●Stability
i. Stable = preserves order of elements when possible
ii. “Is the original order of equivalent elements guaranteed to be the
same?”
Sorting
16
Stability ex:
[ 3 | 10 | 52 | 10 | 50 |
10
]
Sorting
17
Stability ex:
[ 3 | 10 | 52 | 10 | 50 |
10
]
Stable: [ 3 | 10 | 10 |
10
| 50 | 52 ]
Sorting
18
Stability ex:
[ 3 | 10 | 52 | 10 | 50 |
10
]
Stable: [ 3 | 10 | 10 |
10
| 50 | 52 ]
Unstable: Any other order of 10s
Sorting
Selection Sort
Sorting
19
20
Selection sort
Approach: “select” item for each location and swap:
●find smallest item, place in 1st position
●find second smallest, place in 2nd position
●…
●General: find smallest from unsorted part, place
where it belongs by swapping with what’s already
there.
●Animation illustrating Selection Sort
Sorting
21
Mid search snapshot:
●Two Zones
●Sorted Zone
■is sorted
■contains the already addressed indices of the original
list
●Unsorted Zone
■is not sorted
■contains the remaining indices of the original list
Sorting
22
Implementation
Nested loops:
●Outer loop repeats n-1 times, n being size of list
●Initialize a minIndex variable between the outer and the inner loop to
hold the smallest value index
●Inner loop finds the index of the smallest value (compare each
value at index minIndex with the current index at inner iteration,
updating minIndex whenever a smaller value is found)
●When inner loop finishes, swap the minIndex element with the
current position
Sorting
23
Sorting
public static void selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIndex = i;
// Identify index of smallest
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap minimum to current index (i)
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
Example Implementation
24
Key Questions:
●Why repeat n-1 times?
●Why initialize minIndex between the outer and inner loops?
●Why does the inner loop start at i+1?
●Stable or Unstable?
Sorting
25
Key Questions:
●Why repeat n-1 times?
i. The last element will already be sorted.
ii. Sorting based on comparison, the n’th element has nothing left it needs
to be compared to.
●Why initialize minIndex between the outer and inner loops?
●Why does the inner loop start at i+1?
●Stable or Unstable?
Sorting
26
Key Questions:
●Why repeat n-1 times?
i. The last element will already be sorted.
ii. Sorting based on comparison, the n’th element has nothing left it needs
to be compared to.
●Why initialize minIndex between the outer and inner loops?
i. Reset the minIndex value each time.
●Why does the inner loop start at i+1?
●Stable or Unstable?
Sorting
27
Key Questions:
●Why repeat n-1 times?
i. The last element will already be sorted.
ii. Sorting based on comparison, the n’th element has nothing left it needs
to be compared to.
●Why initialize minIndex between the outer and inner loops?
i. Reset the minIndex value each time.
●Why does the inner loop start at i+1?
i. Search for minimum element should start after the current position being
evaluated.
Sorting
28
Key Questions:
●Why repeat n-1 times?
i. The last element will already be sorted.
ii. Sorting based on comparison, the n’th element has nothing left it needs
to be compared to.
●Why initialize minIndex between the outer and inner loops?
i. Reset the minIndex value each time.
●Why does the inner loop start at i+1?
●Stable or Unstable?
i. Unstable -> swaps may jumble original order.
Sorting
Insertion Sort
Sorting
29
30
Insertion sort
Approach: Keep two zones, a sorted and unsorted zone. “Insert”
next element into sorted zone.
●keep left-side sorted
●each loop:
■shift next element left until sorted
●Animation illustrating Insertion Sort
Sorting
31
Insertion sort
Approach: Keep two zones, a sorted and unsorted zone. “Insert”
next element into sorted zone.
●keep left-side sorted
●each loop:
■shift next element left until sorted
■swap left until previous element is smaller (or equivalent) OR
there are no elements left to swap with.
●Animation illustrating Insertion Sort
Sorting
32
Mid search snapshot:
●Sorted Zone
■contains original elements re-ordered to be sorted
●Unsorted Zone
■contains original elements
■has not been touched yet
Sorting
33
Implementation
Nested loops:
●Outer loop repeats n-1 times, n being size of list.
●Inner loop starts at current index for outer loop and goes back,
checking each current element with previous until the beginning of
the list or until a smaller value is found
Sorting
34
Sorting
public static void insertionSort(int[] arr) {
int n = arr.length;
for (int i = 1; i < n; i++) {
int key = arr[i]; // element to be inserted
int j = i - 1; // index to start comparison with
// Shift elements that are greater than key to one position ahead
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
// Insert key at the correct position
arr[j + 1] = key;
}
}
Example Implementation
35
Key Questions:
●Why does the outer loop start at 1?
●Why use a while loop for the internal loop rather than a for loop like
Selection Sort?
●Stable or Unstable?
Sorting
36
Key Questions:
●Why does the outer loop start at 1?
i. Indices [1,n-1] represent the elements that need to be inserted in the
previous portion of the list. The element at 0 is implicitly sorted via
swaps.
●Why use a while loop for the internal loop rather than a for loop like
Selection Sort?
●Stable or Unstable?
Sorting
37
Key Questions:
●Why does the outer loop start at 1?
i. Indices [1,n-1] represent the elements that need to be inserted in the
previous portion of the list. The element at 0 is implicitly sorted via
swaps.
●Why use a while loop for the internal loop rather than a for loop like
Selection Sort?
i. Indefinite number of swaps
ii. For loop can also work with correct condition/setup.
●Stable or Unstable?
Sorting
38
Key Questions:
●Why does the outer loop start at 1?
i. Indices [1,n-1] represent the elements that need to be inserted in the
previous portion of the list. The element at 0 is implicitly sorted via
swaps.
●Why use a while loop for the internal loop rather than a for loop like
Selection Sort?
i. Indefinite number of swaps
ii. For loop can also work with correct condition/setup.
●Stable or Unstable?
i. Stable
Sorting
39
Key Questions:
●Why does the outer loop start at 1?
i. Indices [1,n-1] represent the elements that need to be inserted in the
previous portion of the list. The element at 0 is implicitly sorted via
swaps.
●Why use a while loop for the internal loop rather than a for loop like
Selection Sort?
i. Indefinite number of swaps
ii. For loop can also work with correct condition/setup.
●Stable or Unstable?
i. Stable -> an element that started later in the array is never inserted
before a previous one that is equivalent to it. (arr[j] > key in while loop
Sorting
Bubble Sort
Sorting
40
41
Bubble sort
●Approach: While the list is not sorted, iterate through the list
swapping adjacent pairs.
●Called “bubble” sort because large numbers bubble to the “top” (end
of the list)
Animation of what Bubble Sort looks like
Sorting
42
Mid search snapshot:
●Sorted Zone
■At end of array
■contains the largest i numbers of the original list
sorted (ith iteration)
●Unsorted Zone
■contains the smallest n-i elements of the original list
Sorting
43
Implementation
Nested loops:
●Outer loop repeats n-1 times, n being size of list, or until list is sorted
●Inner loop repeats n-1-i times, checking current value against next
value, swapping if current value is larger than next value. In this
case, i represents the current iteration.
Sorting
44
Sorting
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) { // loop “bubble”s next largest element
for (int j = 0; j < n - 1 - i; j++) { //
if (arr[j] > arr[j + 1]) {
// swap
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
Example Implementation
45
Key Questions:
●Why use n-1-i in the internal loop?
●Stable or Unstable?
●Does the example behave more like Selection Sort or Insertion Sort?
●How could you optimize?
Sorting
46
Key Questions:
●Why use n-1-i in the internal loop?
i. Every iteration of the outer loop, i elements are already sorted at the
end of the array, so we don’t need to evaluate them again.
●Stable or Unstable?
●How could you optimize?
Sorting
47
Key Questions:
●Why use n-1-i in the internal loop?
i. Every iteration of the outer loop, i elements are already sorted at the
end of the array, so we don’t need to evaluate them.
●Stable or Unstable?
i. Stable -> like insertion sort, never bubbling beyond something equivalent
or greater
●How could you optimize?
Sorting
48
Key Questions:
●Why use n-1-i in the internal loop?
i. Every iteration of the outer loop, i elements are already sorted at the
end of the array, so we don’t need to evaluate them as potential swaps.
●Stable or Unstable?
i. Stable -> like insertion sort, never bubbling beyond something equivalent
or greater
●How could you optimize?
i. Add an early exit -> if no swaps happen, the list is already sorted and the
process does not need to continue.
Sorting
49
Sorting
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
boolean swapped = false;
for (int j = 0; j < n - 1 - i; j++) {
// swap if greater
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = true;
}
}
// if no swaps occurred, end early.
if (!swapped){
return; // if more work to happen after, use a break statement here
}
}
Example Optimized Implementation
50
Simple Sorts: Lecture Recap
Sorting
Selection Sort:
●Series of “search for
minimum value”/
“swap to minimum
position” actions.
●Unstable
○Swaps don’t
preserve
existing order.
●Slowest of these 3
sorts.
Insertion Sort:
●Series of “insert”
actions - shifting
elements while
greater, then
inserting value.
●Stable
●Performs better than
other 2 simple sorts.
○Shift vs. swap
○More low level
details (CSCI
2021).
Bubble Sort:
●Series of “bubbling”
the n’th greatest
element to the “top”
(end) of the array.
●Stable
●Can be optimized to
perform better than
Selection sort.
●Nat’s least favorite