Computer Science 1620 Sorting
cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection (e.g. select 5 students with highest gpas) etc the sorting problem given a list of non-sorted data, rearrange the data into ascending or descending order
Sorting Sort
Sorting a well-studied area of Computer Science many clever algorithms exist Simple: bubble sort selection sort insertion sort Complex: merge sort quick sort (very popular!!) We will focus on the simple sorting algorithms, as the complex sorts involve recursion.
Sorting – the general idea the sort will be conducted using a series of swaps (discussed on next slide) we will not use any extra arrays inline the quality of a sorting algorithm is typically based on two metrics: # of comparisons # of swaps
Simple Sorting The swap procedure swap(a[i], a[j]) swaps the elements i and j in a, so that a[i] becomes a[j] and vice versa eg. swap(a[3], a[5]) provided by algorithm header file #include Before After
Bubble Sort a very simple sorting algorithm the basic idea: when two adjacent items are found to be out of order, swap them adjacent in this case means two items that are side by side in an array (a[1], a[2]) or (b[i], b[i+1])
Bubble Sort – Algorithm Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements (that is, (1,2), (2,3), (3,4), and so on) Step If a pair of adjacent elements are found to be out of order, swap them
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them In order, so we don't swap
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap.
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Are items still out of order? yes, so we repeat
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them In order, no swap
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them In order, no swap
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap.
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them In order, no swap
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Are items still out of order? yes, so we repeat
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them In order, no swap
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap 4517
31 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap 45
63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them In order, no swap. 45
50 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap 63
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them In order, no swap 50
63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them In order, no swap 50
63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 Are items still out of order? yes, so we repeat
63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 Out of order, so we swap 2417
63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724
63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724
63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724
63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724
63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724
63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724
63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Are items still out of order? no, we are done
Bubble Sort guaranteed to sort our data requires up to n passes through the array
#include using namespace std; void bubble_sort(int a[], int sz) { Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them }
#include using namespace std; void bubble_sort(int a[], int sz) { Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them }
#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them }
#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them }
#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { Step 1.1: for each pair of adjacent elements Step If these items out of order, swap }
#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { Step If these items out of order, swap }
#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { Step If these items out of order, swap }
#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { Step If these items out of order Step swap the two items }
#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { Step If these items out of order Step swap the two items }
#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { if (a[j] > a[j+1]) { Step swap the two items }
#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { if (a[j] > a[j+1]) { Step swap the two items }
#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); }
#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; ; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); } What is condition here?
#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; j < sz; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); } Will this work?
#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); }
#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); }
How do we know if the list is out of order? Question: What happens in our "check" loop when all of the items are in order?
63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724
63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724
63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724
63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724
63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724
63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724
63 Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them 50 In order, no swap 1724
What did you notice? no swaps took place our list is sorted if during our check loop, no swaps took place during our check loop, if a swap occurs, then remember this use a boolean variable initially false true if a swap occurs
#include using namespace std; void bubble_sort(int a[], int sz) { while (items in list are out of order) { for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); }
#include using namespace std; void bubble_sort(int a[], int sz) { bool swapped = true; while (swapped) { swapped = false; for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); swapped = true; }
Why do I initially set swapped to true to guarantee that the while loop executes at least once what is another way to guarantee this? make it a do-while loop
#include using namespace std; void bubble_sort(int a[], int sz) { bool swapped; do { swapped = false; for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); swapped = true; } } while (swapped); }
An Improvement to Bubble Sort this initial version of bubble sort works fine how many comparisons does it make? each time through the "check" loop: n-1 how many times does it go through the check loop: n (in the worst case) therefore, it can make up to n(n-1) comparisons
Better Bubble Sort what do you notice at the end of each "check" loop
Bubble Sort After first time through loop
Bubble Sort After second time through loop
63 Bubble Sort After third time through loop 50
Each time through the loop, the ith largest value gets put in its final position first time through the loop, largest item placed in last spot second time through the loop, second largest item placed in second-last spot ith time through the loop, ith largest item placed in ith largest spot
What does this mean? on the second pass through the loop, do I need to compare the last item? no – it's the largest item, so belongs in the last spot this means that each time through the loop, the number of items to sort gets smaller
Bubble Sort After first time through loop Only these elements need to be sorted
Bubble Sort After second time through loop Only these elements need to be sorted
63 Bubble Sort After third time through loop 50 Only these elements need to be sorted
#include using namespace std; void bubble_sort(int a[], int sz) { bool swapped; do { swapped = false; for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); swapped = true; } } while (swapped); }
#include using namespace std; void bubble_sort(int a[], int sz) { bool swapped; do { swapped = false; for (int j = 0; j < sz - 1; j++) { if (a[j] > a[j+1]) { swap(a[j], a[j+1]); swapped = true; } sz--; } while (swapped); }
How good is this simple fix? how many comparisons: first time through the loop: n – 1 second time through the loop: n-2 ith time through the loop: n-i Number of comparisons in the worst case:
Hence: this very small extension to the bubble sort algorithm improves the # of comparisons by 50%
Bubble Sort – Problems suppose you are sorting an array of strings each time you call swap, the following must occur* copy first string to temporary storage copy second string value to first string copy temporary storage value to second string if strings are big, this can be costly for time ie. swap can be expensive
Bubble Sort what makes bubble sort so "expensive" in terms of computation is the number of swaps that it does number of comparisons: n(n-1)/2 in worst case number of swaps: n(n-1)/2 in worst case
Selection Sort suppose my items are arranged like this what will the first "check" pass of bubble sort look like?
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them In order, so we don't swap
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them Out of order, so we swap.
Bubble Sort Step 1: Repeat while items in list are out of order Step 1.1: Check each pair of adjacent elements Step If a pair of adjacent elements are found to be out of order, swap them
What did you notice? the biggest number (96) got placed in the last spot (discussed last day) however, the number gets "dragged" along by a number of swaps what if, instead of dragging it to its final location, I swapped it directly there?
Selection Sort Algorithm: Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array in other words find the smallest element, swap it into the first spot find the 2 nd smallest element, swap it into the 2 nd spot find the 3 rd smallest element, swap it into the 3 rd lspot
Selection Sort Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array 8550
Selection Sort Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array 8550 smallest
Selection Sort Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array nd smallest
Selection Sort Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array rd smallest
Selection Sort Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array th smallest
Selection Sort Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array th smallest
Selection Sort Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array th smallest
Selection Sort Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array th smallest
Selection Sort Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array th smallest
#include using namespace std; void selection_sort(int a[], int n) { Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array }
#include using namespace std; void selection_sort(int a[], int n) { Step 1: for each i between 1 and n Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array }
#include using namespace std; void selection_sort(int a[], int n) { for (int i = 1; i <= n; i++) { Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array } Will this work?
#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array } Remember: arrays are ordered 0 to n-1, not 1 to n
#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { Step 1.1 find the i-th smallest number in the array Step 1.2 swap that number in the i-th spot in the array }
#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = (i-th smallest number in the array); Step 1.2 swap that number in the i-th spot in the array }
#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = (i-th smallest number in the array); Step 1.2 swap that number in the i-th spot in the array }
#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = (i-th smallest number in the array); swap(a[ith], a[i]); }
#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = (i-th smallest number in the array); swap(a[ith], a[i]); } How do we find the ith smallest?
Let's simplify this: how do we set ith to point to the smallest number? One solution: assume smallest is at location 0 loop through the other elements if you find a smaller number at another location, remember that location
#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = (i-th smallest number in the array); swap(a[ith], a[i]); } assume smallest is at location 0 loop through the other elements if you find a smaller number at another location, remember that location
#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = 0; swap(a[ith], a[i]); } assume smallest is at location 0 loop through the other elements if you find a smaller number at another location, remember that location
#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = 0; for (int j = 1; j < n; j++) { } swap(a[ith], a[i]); } assume smallest is at location 0 loop through the other elements if you find a smaller number at another location, remember that location
#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = 0; for (int j = 1; j < n; j++) { if (a[j] < a[ith]) { } swap(a[ith], a[i]); } assume smallest is at location 0 loop through the other elements if you find a bigger number at another location, remember that location
#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = 0; for (int j = 1; j < n; j++) { if (a[j] < a[ith]) { ith = j; } swap(a[ith], a[i]); } assume smallest is at location 0 loop through the other elements if you find a bigger number at another location, remember that location
This works fine for finding the smallest but what about finding the 2 nd smallest, or 3 rd smallest, etc? let's consider what the array looks like after the ith smallest element is found
Bubble Sort Our array, initially 8550
Bubble Sort After smallest is found 1750
Bubble Sort After second smallest is found 1750
Bubble Sort After third smallest is found 1750
Bubble Sort After third smallest is found 1750
What do you notice? after the smallest is found, it is placed in location 0 this means that the 2 nd smallest item is the smallest item between location 1 and location n-1 after the 2 nd smallest is found, it is placed in location 1 this means that the 3 rd smallest item is the smallest item between location 2 and location n-1
Bubble Sort Our array, initially 8550 Find smallest in this set
Bubble Sort After smallest is found 1750 Find smallest in this set
Bubble Sort After second smallest is found 1750 Find smallest in this set
Bubble Sort After third smallest is found 1750 Find smallest in this set
In other words we only need to search the elements beginning at i
#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = 0; for (int j = 1; j < n; j++) { if (a[j] < a[ith]) { ith = j; } swap(a[ith], a[i]); }
#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n; i++) { int ith = i; for (int j = i+1; j < n; j++) { if (a[j] < a[ith]) { ith = j; } swap(a[ith], a[i]); }
A Couple of Minor Improvements 1) We do not need to do the last pass through the outer loop 2) We do not need to swap if i == ith
#include using namespace std; void selection_sort(int a[], int n) { for (int i = 0; i < n-1; i++) { int ith = i; for (int j = i+1; j < n; j++) { if (a[j] < a[ith]) { ith = j; } if (i != ith) swap(a[ith], a[i]); }
Selection Sort how many comparisons does selection sort make? first pass through loop: n-1 second pass through loop: n-2 third pass through loop: n-3 …. n-1 pass through loop: 1
Selection Sort just as many compares as bubble sort how many swaps (worst case)? N -1 this is a big improvement over bubble sort, which required n(n-1)/2 swaps
Insertion Sort nested loop, just like selection sort outer loop moves one at a time through each element of the array at each element "picks up" element out of the array places the element in its corresponding spot behind this index result when outer loop is at index i, everything behind index i is sorted
Insertion Sort Example:
Insertion Sort – Outer Loop loop through all elements one at a time starting at 2 nd element current
Insertion Sort – Outer Loop Step 1: take current element out of the array current
Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element or nothing left to shift current
Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 85 > 24, so it's shifted current
Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element nothing left to shift, so stop current 85
Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting current 85
Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting current 85
Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again current 85
Insertion Sort – Outer Loop Step 1: take current element out of the array current 85
Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 85 > 63, so shift current 85
Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 24 < 63, so stop! current 85
Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting current 8563
Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again current 8563
Insertion Sort – Outer Loop Step 1: take current element out of the array current 8563
Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 85 > 45, so shift current 8563
Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 63 > 45, so shift current 8563
Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 24 < 45, so stop! current 8563
Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting current 8563
Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again current 8563
Insertion Sort – Outer Loop Step 1: take current element out of the array current 8563
Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 85 > 17, so shift current 8563
Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 63 > 17, so shift current 8563
Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 45 > 17, so shift current 8563
Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element 24 > 17, so shift current 8563
Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element nothing left to shift, so stop! current 8563
Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting current 8563
Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again current 8563
Insertion Sort – Outer Loop And so on notice that the list to the left of current is always sorted current 8563
How do we code this? tricky Algorithm: for each element (starting at 2 nd ) take element out of array Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift place the current element in the empty spot created by shifting
#include using namespace std; void insertion_sort(int a[], int n) { Step 1: for each element (starting at 2nd) Step 1.1 Take element out of array Step 1.2 Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift Step 1.3 place the current element in the empty spot created by shifting }
#include using namespace std; void insertion_sort(int a[], int n) { Step 1: for each element (starting at 2nd) Step 1.1 Take element out of array Step 1.2 Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift Step 1.3 place the current element in the empty spot created by shifting }
#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { Step 1.1 Take element out of array Step 1.2 Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift Step 1.3 place the current element in the empty spot created by shifting }
#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { Step 1.1 Take element out of array Step 1.2 Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift Step 1.3 place the current element in the empty spot created by shifting }
#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; Step 1.2 Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift Step 1.3 place the current element in the empty spot created by shifting } NOTE: This doesn't take the actual number out of the array – just a copy of it.
#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; Step 1.2 Shift elements behind this element to the right, stopping when element to be shifted is less than current element, or nothing left to shift Step 1.3 place the current element in the empty spot created by shifting }
#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( ) { a[j] = a[j-1]; j--; } Step 1.3 place the current element in the empty spot created by shifting }
#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( ) { a[j] = a[j-1]; j--; } Step 1.3 place the current element in the empty spot created by shifting }
#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( ) { a[j] = a[j-1]; j--; } a[j] = element; }
#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( ) { a[j] = a[j-1]; j--; } a[j] = element; } We want to loop until there is nothing left to shift the item to be shifted (a[j-1]) is less than element
#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( ) { a[j] = a[j-1]; j--; } a[j] = element; } We want to loop until there is nothing left to shift the item to be shifted (a[j-1]) is less than element
#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( (j > 0) ) { a[j] = a[j-1]; j--; } a[j] = element; } We want to loop until there is nothing left to shift the item to be shifted (a[j-1]) is less than element
#include using namespace std; void insertion_sort(int a[], int n) { for (int i = 1; i < n; i++) { int element = a[i]; int j = i; while ( (j > 0) && (a[j-1] > element) ) { a[j] = a[j-1]; j--; } a[j] = element; } We want to loop until there is nothing left to shift the item to be shifted (a[j-1]) is less than element
And so on What is the complexity of insertion sort? How many comparisons: First pass: 1 comparisons Second pass: 2 comparisons … ith passi comparisons n-1 passn-1 comparison
Swaps how many swaps? no swaps – just shifts shifting takes 1/3 as much work as swap how many shifts? possibly one for each comparison hence:
Which is the best algorithm? one source reports: selection sort roughly 60% faster than bubble sort (without optimization) why? less swaps insertion sort is roughly 40% faster than selection sort why? selection sort guaranteed to do n(n-1) comparisons insertion sort only does this many in the worst case in the best case, insertion sort requires linear comparisons
Insertion Sort Example:
Insertion Sort – Outer Loop loop through all elements one at a time starting at 2 nd element current
Insertion Sort – Outer Loop Step 1: take current element out of the array current
Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element or nothing left to shift current
Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element nothing left to shift, so stop current
Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting 1724 current
Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again current
Insertion Sort – Outer Loop Step 1: take current element out of the array current
Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element or nothing left to shift current
Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element nothing left to shift, so stop current
Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting 1731 current
Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again 1731 current
Insertion Sort – Outer Loop Step 1: take current element out of the array current
Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element or nothing left to shift current
Insertion Sort – Outer Loop Step 2: shift elements behind this element to the right stop when element to be shifted is less than current element nothing left to shift, so stop current
Insertion Sort – Outer Loop Step 3: place the current element in the empty spot created by shifting 1745 current
Insertion Sort – Outer Loop Step 4: move to next element in list, and being process again 1745 current
And so on as you can see, insertion sort only does 1 comparison per item when the list is already sorted
Insertion Sort insertion sort is often daunting to new programmers much less intuitive than bubble sort or selection sort remember: after 1 st pass of inner loop, first two items are sorted after 2 nd pass of inner loop, first three items are sorted after 3 rd pass of inner loop, first four items are sorted etc …
Insertion Sort Example:
Insertion Sort – Outer Loop After first pass notice that first two items are sorted
Insertion Sort – Outer Loop After second pass notice that first three items are sorted
Insertion Sort – Outer Loop After third pass notice that first four items are sorted
Insertion Sort – Outer Loop After 4 th pass notice that first 5 th items are sorted