SearchingSorting.ppt
-
Upload
devilseyebrows -
Category
Documents
-
view
21 -
download
1
Transcript of SearchingSorting.ppt
1
What is the output of this code fragment?
int nCount = 0;int nCount = 0;int nN = 10;int nN = 10;for(int nX = 0; nX < nN; nX++)for(int nX = 0; nX < nN; nX++) for(int nY = 0; nY <nN; nY++)for(int nY = 0; nY <nN; nY++) nCount++;nCount++;System.out.println(nCount);System.out.println(nCount); What would the output be if What would the output be if nNnN was 5? or 20? was 5? or 20? What effect does doubling What effect does doubling nNnN have on have on nCountnCount??
2
O(n2)
The nested The nested forfor loops "are multiplied" loops "are multiplied" The efficiency of this type of algorithm is The efficiency of this type of algorithm is O(nO(n22))
for(int nX = 0; nX < nN; nX++)for(int nX = 0; nX < nN; nX++) for(int nY = 0; nY <nN; nY++)for(int nY = 0; nY <nN; nY++)
3
Sorting
Sorting is the process of putting something Sorting is the process of putting something (e.g. an array) in order(e.g. an array) in order
There are many ways (There are many ways (algorithmsalgorithms) to do this) to do this There is no, one single There is no, one single bestbest way way Some ways, though, are much Some ways, though, are much worseworse than than
othersothers Computer Scientists measure the efficiency Computer Scientists measure the efficiency
of an algorithm in Big O notationof an algorithm in Big O notation
4
O(n2) Sorting Algorithms
Characterized by nested loopsCharacterized by nested loopsBubble SortBubble SortSelection SortSelection SortInsertion SortInsertion Sort
Doubling the data Doubling the data QuadruplesQuadruples (4 x) the work(4 x) the work
5
Bubble Sort
Let's say we want to sort the following Let's say we want to sort the following numbers: 12 34 18 5 7numbers: 12 34 18 5 7
We'll start by comparing the first twoWe'll start by comparing the first two
12 3412 34 18 5 7 18 5 7
6
Bubble Sort
They are in order, so well go to the next twoThey are in order, so well go to the next two
12 12 3434 18 18 5 7 5 7
7
Bubble Sort
They aren't in order, so we'll They aren't in order, so we'll swapswap them them
12 12 1818 34 34 5 7 5 7
8
Bubble Sort
and so on. . .and so on. . .
12 18 12 18 34 534 5 7 7
9
Bubble Sort
and so on. . .and so on. . .
12 18 12 18 5 345 34 7 7
10
Bubble Sort
and so on. . .and so on. . .
12 18 512 18 5 34 34 77
11
Bubble Sort
and so on. . .and so on. . .
12 18 512 18 5 7 7 3434
12
Bubble Sort
finally, after finally, after one passone pass through the numbers, through the numbers, the last number is in the correct positionthe last number is in the correct position
We'll repeat the process, but we'll stop one We'll repeat the process, but we'll stop one position sooner.position sooner.
12 18 512 18 5 7 7 3434
13
Bubble Sort
12 1812 18 5 5 7 7 3434
14
Bubble Sort
12 12 18 518 5 7 7 3434
15
Bubble Sort
12 12 5 185 18 7 7 3434
16
Bubble Sort
12 5 12 5 18 18 7 7 3434
17
Bubble Sort
12 5 12 5 7 7 18 18 3434
18
Bubble Sort
12 5 12 5 7 7 18 18 3434
19
Bubble Sort
12 512 5 7 7 18 18 3434
20
Bubble Sort
5 125 12 7 7 18 18 3434
21
Bubble Sort
5 5 1212 7 7 18 18 3434
22
Bubble Sort
5 5 77 12 12 18 18 3434
23
Bubble Sort
5 7 5 7 1212 18 18 3434
24
Bubble Sort
5 75 7 1212 18 18 3434
25
Bubble Sort
5 75 7 1212 18 18 3434
26
Bubble Sort
Finally, the numbers are sortedFinally, the numbers are sorted
5 7 12 18 345 7 12 18 34
27
Coding BubbleSort
public static void bubbleSort(int[] list)public static void bubbleSort(int[] list){{ for (int outer = 0; outer < list.length - 1; outer++)for (int outer = 0; outer < list.length - 1; outer++) for (int inner = 0; inner < list.length-outer-1;for (int inner = 0; inner < list.length-outer-1; inner++)inner++)
We'll start with some nested We'll start with some nested forfor loops that loops that take us through the list of numberstake us through the list of numbers
We want to stop one position earlier after We want to stop one position earlier after each passeach pass
list.length-outer-1list.length-outer-1
28
Coding BubbleSort
public static void bubbleSort(int[] list)public static void bubbleSort(int[] list){{ for (int outer = 0; outer < list.length - 1; outer++)for (int outer = 0; outer < list.length - 1; outer++) {{ for (int inner = 0; inner < list.length-outer-1;for (int inner = 0; inner < list.length-outer-1; inner++)inner++) {{ if (list[inner] > list[inner + 1])if (list[inner] > list[inner + 1]) {{ //swap list[inner] & list[inner+1]//swap list[inner] & list[inner+1] int temp = list[inner];int temp = list[inner]; list[inner] = list[inner + 1];list[inner] = list[inner + 1]; list[inner + 1] = temp;list[inner + 1] = temp; }} }} }}}}
29
Counting steps in Bubble Sort
We will be counting the "steps" each algorithm We will be counting the "steps" each algorithm takes to complete the sortingtakes to complete the sorting
For each step, we will increment the For each step, we will increment the stepstep member of the member of the SortsSorts class class
public class Sortspublic class Sorts{{ private long steps;private long steps; A step is:A step is:
1.1. An assignmentAn assignment2.2. A comparisonA comparison3.3. Arithmetic (usually ++)Arithmetic (usually ++)
30
Counting steps in Bubble Sortpublic static void bubbleSort(int[] list)public static void bubbleSort(int[] list){{ steps++; //outer = 0steps++; //outer = 0 for (int outer = 0; outer < list.length - 1; outer++)for (int outer = 0; outer < list.length - 1; outer++) {{ steps++; // outer < list.length – 1steps++; // outer < list.length – 1 steps++; // outer++steps++; // outer++ steps++; // int inner = 0steps++; // int inner = 0 for (int inner = 0; inner < list.length-outer-1;for (int inner = 0; inner < list.length-outer-1; inner++)inner++) {{ steps++; // inner < list.length-outer-1steps++; // inner < list.length-outer-1 steps++; // inner++steps++; // inner++ steps++; // list[inner] > list[inner + 1]steps++; // list[inner] > list[inner + 1]
if (list[inner] > list[inner + 1])if (list[inner] > list[inner + 1]) {{ //swap list[inner] & list[inner+1]//swap list[inner] & list[inner+1] int temp = list[inner];int temp = list[inner]; list[inner] = list[inner + 1];list[inner] = list[inner + 1]; list[inner + 1] = temp;list[inner + 1] = temp; steps+=3; //3 assignmentssteps+=3; //3 assignments }} }} }}}}
31
What is the output?
public class SwapMyst {public class SwapMyst { public static void main(String[] args) {public static void main(String[] args) { mystery1(3,4);mystery1(3,4); mystery2(3,4);mystery2(3,4); }} public static void mystery1(int nNum1, int nNum2){public static void mystery1(int nNum1, int nNum2){ nNum1 = nNum2;nNum1 = nNum2; nNum2 = nNum1;nNum2 = nNum1; System.out.println(nNum1 + ", " + nNum2);System.out.println(nNum1 + ", " + nNum2); }} public static void mystery2(int nNum1, int nNum2){public static void mystery2(int nNum1, int nNum2){ int nTemp = nNum1;int nTemp = nNum1; nNum1 = nNum2;nNum1 = nNum2; nNum2 = nTemp;nNum2 = nTemp; System.out.println(nNum1 + ", " + nNum2);System.out.println(nNum1 + ", " + nNum2); }}}}
32
The simplest O(nThe simplest O(n22) sort? ) sort? Supposedly how garden gnomes sort flower Supposedly how garden gnomes sort flower
pots at nightpots at night Start with the first twoStart with the first two
12 3412 34 18 5 7 18 5 7
Gnome Sort (from Wikipedia)
33
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
12 3412 34 18 5 7 18 5 7
Gnome Sort
34
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
12 12 34 1834 18 5 7 5 7
Gnome Sort
35
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
12 12 18 3418 34 5 7 5 7
Gnome Sort
36
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
12 1812 18 34 5 7 34 5 7
Gnome Sort
37
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
1212 18 18 3434 5 7 5 7
Gnome Sort
38
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
1212 18 18 3434 5 5 7 7
Gnome Sort
39
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
1212 18 18 55 34 34 7 7
Gnome Sort
40
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
1212 18 5 18 5 34 734 7
Gnome Sort
41
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
1212 5 18 5 18 34 734 7
Gnome Sort
42
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
12 5 12 5 18 18 34 734 7
Gnome Sort
43
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
5 12 5 12 18 18 34 734 7
Gnome Sort
44
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
5 5 12 18 12 18 34 734 7
Gnome Sort
45
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
5 5 1212 18 18 34 34 7 7
Gnome Sort
46
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
5 5 1212 18 18 34 34 7 7
Gnome Sort
47
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
5 5 1212 18 18 7 7 34 34
Gnome Sort
48
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
5 5 1212 18 18 7 7 3434
Gnome Sort
49
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
5 5 1212 7 7 18 18 3434
Gnome Sort
50
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
5 5 12 7 12 7 18 18 3434
Gnome Sort
51
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
5 5 7 12 7 12 18 18 3434
Gnome Sort
52
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
5 7 5 7 12 12 18 18 3434
Gnome Sort
53
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
55 7 12 7 12 18 18 3434
Gnome Sort
54
If the numbers are out of order, swap them If the numbers are out of order, swap them and move to the leftand move to the left
If they are in order, leave them alone and If they are in order, leave them alone and move rightmove right
55 77 12 12 18 18 3434
Gnome Sort
55
When the gnome gets all the way to the When the gnome gets all the way to the right, he's done!right, he's done!
55 77 12 12 18 18 34 34
Gnome Sort
56
void gnomesort(int n, int ar[]) void gnomesort(int n, int ar[]) {{int i = 0;int i = 0;
while (i < n) while (i < n) {{ //if the numbers are in order //if the numbers are in order
if (i == 0 || ar[i-1] <= ar[i]) if (i == 0 || ar[i-1] <= ar[i]) i++; //move righti++; //move right
else //swap else //swap {{ int tmp = ar[i]; int tmp = ar[i]; ar[i] = ar[i-1]; ar[i] = ar[i-1]; ar[i-1] = tmp;}ar[i-1] = tmp;} i--; //move lefti--; //move left }}
}}
Coding Gnome Sort
57
Selection Sort
Like Bubble Sort, but with fewer swapsLike Bubble Sort, but with fewer swaps We'll use a flag to keep track of the We'll use a flag to keep track of the
position of the smallest integerposition of the smallest integer Then we will do one swap at the end of Then we will do one swap at the end of
each passeach pass
12 12 34 34 18 5 7 18 5 7nFlag = 0nFlag = 0
58
Selection Sort
12 34 12 34 18 18 5 7 5 7
nFlag = 0nFlag = 0
59
Selection Sort
12 34 18 12 34 18 5 5 7 7
nFlag = 0 3nFlag = 0 3
60
Selection Sort
12 34 18 5 12 34 18 5 77
nFlag = 3nFlag = 3
61
Selection Sort
After one pass through the numbers, we'll After one pass through the numbers, we'll swap the position marked by the flag with swap the position marked by the flag with the first numberthe first number
12 12 34 18 34 18 5 5 7 7
nFlag = 3nFlag = 3
62
Selection Sort
After one pass through the numbers, we'll After one pass through the numbers, we'll swap the position marked by the flag with swap the position marked by the flag with the first numberthe first number
5 5 34 18 34 18 1212 7 7
nFlag = 3nFlag = 3
63
Selection Sort
The first number is now sorted, so we'll The first number is now sorted, so we'll start the process again from the next start the process again from the next positionposition
5 5 34 34 1818 12 712 7
nFlag = 1nFlag = 1
64
Selection Sort
5 5 34 18 34 18 12 12 7 7
nFlag = 2nFlag = 2
65
Selection Sort
5 5 34 18 12 34 18 12 77
nFlag = 3nFlag = 3
66
Selection Sort
5 5 34 34 18 12 18 12 77
nFlag = 4nFlag = 4
67
Selection Sort
5 5 7 7 18 12 34 18 12 34
nFlag = 4nFlag = 4
68
Selection Sort
5 5 7 7 18 18 12 12 34 34
nFlag = 2nFlag = 2
69
Selection Sort
5 5 7 7 18 18 12 12 34 34
nFlag = 3nFlag = 3
70
Selection Sort
5 5 7 7 1212 18 18 3434
nFlag = 3nFlag = 3
71
Selection Sort
5 5 7 7 1212 18 18 34 34
nFlag = 3nFlag = 3
72
Selection Sort
5 5 7 7 1212 18 3418 34
nFlag = 3nFlag = 3
73
Coding Selection Sortvoid selectionSort(int[] list)void selectionSort(int[] list){{ int min, temp;int min, temp; for (int outer = 0; outer < list.length - 1; outer++)for (int outer = 0; outer < list.length - 1; outer++) {{ min = outer; min = outer; for (int inner = outer + 1; inner < list.length;for (int inner = outer + 1; inner < list.length; inner++)inner++) {{ if (list[inner] < list[min])if (list[inner] < list[min]) {{ min = inner;min = inner; }} }} //swap list[outer] & list[flag]//swap list[outer] & list[flag] temp = list[outer];temp = list[outer]; list[outer] = list[min];list[outer] = list[min]; list[min] = temp;list[min] = temp; }}}}
74
A Common Confusion minmin = outer; = outer; for (int inner = outer + 1; inner < list.length;for (int inner = outer + 1; inner < list.length; inner++)inner++) {{ if (list[inner] < list[min])if (list[inner] < list[min]) {{ minmin = inner; = inner; }} }} //swap list[outer] & list[flag]//swap list[outer] & list[flag] temp = list[outer];temp = list[outer]; list[outer] = list[outer] = list[min]list[min];; list[min]list[min] = temp; = temp; The Flag The Flag minmin, stores the , stores the indexindex of the smallest of the smallest
numbernumber Don't confuse Don't confuse minmin with with list[min]list[min] That's like confusing the apartment number with That's like confusing the apartment number with
the person who lives in that apartmentthe person who lives in that apartment
75
What is the output?public class SelectionExample {public class SelectionExample { public static void main(String[] args) {public static void main(String[] args) { int [] naNums = {7,-2,5,-11,67};int [] naNums = {7,-2,5,-11,67}; int nMyst = mystery(naNums);int nMyst = mystery(naNums); System.out.println(nMyst);System.out.println(nMyst); System.out.println(naNums[nMyst]);System.out.println(naNums[nMyst]); }} public static int mystery(int [] naInts)public static int mystery(int [] naInts) {{ int nFlag = 0;int nFlag = 0; for(int nI = 1; nI < naInts.length; nI++)for(int nI = 1; nI < naInts.length; nI++) {{ if(naInts[nI] < naInts[nFlag])if(naInts[nI] < naInts[nFlag]) {{ System.out.println("Flag changed to " + System.out.println("Flag changed to " +
nI);nI); nFlag = nI;nFlag = nI; }} }} return nFlag;return nFlag; }}}}
76
What is the output? How would it change if the code in red was 2?
String [] saPets = String [] saPets = {"cat", "bird","elephant","ant","dog"};{"cat", "bird","elephant","ant","dog"}; //bubble sort the list of pets//bubble sort the list of pets for(int nI = 0; nI < for(int nI = 0; nI < saPets.lengthsaPets.length; nI++); nI++) for(int nJ = 0; nJ < saPets.length - nI -1; nJ+for(int nJ = 0; nJ < saPets.length - nI -1; nJ+
+)+) {{ if(saPets[nJ].compareTo(saPets[nJ+1]) > 0) if(saPets[nJ].compareTo(saPets[nJ+1]) > 0) // if saPets[nJ + 1] comes before // if saPets[nJ + 1] comes before // saPets[nJ] alphabetically// saPets[nJ] alphabetically {{ String sTemp = saPets[nJ];String sTemp = saPets[nJ]; saPets[nJ] = saPets[nJ + 1];saPets[nJ] = saPets[nJ + 1]; saPets[nJ + 1] = sTemp;saPets[nJ + 1] = sTemp; }} }}
for(int nK = 0; nK < saPets.length; nK++)for(int nK = 0; nK < saPets.length; nK++) System.out.println(saPets[nK]);System.out.println(saPets[nK]);
77
Insertion Sort
Insertion Sort starts by sorting the first Insertion Sort starts by sorting the first part of the listpart of the list
The first unsorted number is The first unsorted number is insertedinserted into into the sorted part of the listthe sorted part of the list
12 34 18 5 712 34 18 5 7
78
Insertion Sort
A single number by itself is sorted, so we A single number by itself is sorted, so we will start with the first number as the will start with the first number as the sorted part of the listsorted part of the list
We'll store the first number after the We'll store the first number after the sorted section in a temporary variablesorted section in a temporary variable
12 12 __ 18 5 7 __ 18 5 7nTemp = 34nTemp = 34
79
Insertion Sort
While the numbers in the sorted section While the numbers in the sorted section are larger than the number in nTemp, we are larger than the number in nTemp, we will move them up one placewill move them up one place
12 12 __ 18 5 7 __ 18 5 7
nTemp = 34nTemp = 34
80
Insertion Sort
Then we will move the number in nTemp Then we will move the number in nTemp into the empty position, and the next into the empty position, and the next number in nTempnumber in nTemp
12 3412 34 __ 5 7 __ 5 7
nTemp = 18nTemp = 18
81
Insertion Sort
12 __ 3412 __ 34 5 7 5 7
nTemp = 18nTemp = 18
82
Insertion Sort
12 18 3412 18 34 __ 7 __ 7
nTemp = 5nTemp = 5
83
Insertion Sort
12 18 __ 3412 18 __ 34 7 7
nTemp = 5nTemp = 5
84
Insertion Sort
12 __ 18 3412 __ 18 34 7 7
nTemp = 5nTemp = 5
85
Insertion Sort
__ 12 18 34__ 12 18 34 7 7
nTemp = 5nTemp = 5
86
Insertion Sort
5 12 18 345 12 18 34 __ __
nTemp = 7nTemp = 7
87
Insertion Sort
5 12 18 __ 345 12 18 __ 34
nTemp = 7nTemp = 7
88
Insertion Sort
5 12 __ 18 345 12 __ 18 34
nTemp = 7nTemp = 7
89
Insertion Sort
5 __ 12 18 345 __ 12 18 34
nTemp = 7nTemp = 7
90
Insertion Sort
5 7 12 18 345 7 12 18 34
nTemp = 7nTemp = 7
91
Coding Insertion Sortvoid insertionSort(int[] list)void insertionSort(int[] list){{ for (int outer = 1; outer < list.length; for (int outer = 1; outer < list.length; outer++)outer++) {{ int position = outer;int position = outer; int key = list[position];int key = list[position]; // Shift larger values to the right// Shift larger values to the right while (position > 0 && while (position > 0 && list[position - 1] > key)list[position - 1] > key) {{ list[position] = list[position - 1];list[position] = list[position - 1]; position--;position--; }} list[position] = key;list[position] = key; }}}}
92
Big O notationExpressionExpression ExplanationExplanation Effect of Effect of
Doubling DataDoubling Data
O(1)O(1) ConstantConstant has no effecthas no effect
O(log n)O(log n) LogarithmicLogarithmic increases by 1increases by 1
O(n)O(n) LinearLinear doublesdoubles
O(n log n)O(n log n) Linear-LogarithmicLinear-Logarithmic >2x & <4x>2x & <4x
O(nO(n22)) QuadraticQuadratic quadruplesquadruples
O(2O(2nn)) ExponentialExponential If data increases by If data increases by oneone, work , work doublesdoubles
93
Big O notationExpressionExpression ExampleExample
O(1)O(1) Array storage/retrievalArray storage/retrieval
O(log n)O(log n) Binary SearchBinary Search
O(n)O(n) Linear search, Radix SortLinear search, Radix Sort
O(n log n)O(n log n) Merge Sort, Quick SortMerge Sort, Quick Sort
O(nO(n22)) Bubble, Selection, Insertion sortBubble, Selection, Insertion sort
O(2O(2nn)) Towers of HanoiTowers of Hanoi
94
O(n log n) Sorting Algorithms
Characterized by recursive "Divide Characterized by recursive "Divide and Conquer" strategiesand Conquer" strategiesMerge SortMerge SortQuick SortQuick Sort
Doubling the Data increases the Doubling the Data increases the work more than 2x but less than 4xwork more than 2x but less than 4x
95
Here's a clever idea
With an O(nWith an O(n22) sort, doubling the number of ) sort, doubling the number of numbers quadruples the worknumbers quadruples the work
So let's say 5 numbers takes 40 steps to sortSo let's say 5 numbers takes 40 steps to sort How many steps would 10 numbers take?How many steps would 10 numbers take?
96
Here's a clever idea
With an O(nWith an O(n22) sort, doubling the number of ) sort, doubling the number of numbers quadruples the worknumbers quadruples the work
So let's say 5 numbers takes 40 steps to sortSo let's say 5 numbers takes 40 steps to sort Sorting 10 numbers would take 160 stepsSorting 10 numbers would take 160 steps What if we sorted those 10 numbers What if we sorted those 10 numbers 5 at a 5 at a
timetime?? Would that save steps?Would that save steps?
97
Here's a clever idea
{6,2,1,3,9,8,5,7,4,0}{6,2,1,3,9,8,5,7,4,0} becomesbecomes{6,2,1,3,9} {8,5,7,4,0}{6,2,1,3,9} {8,5,7,4,0} How many steps to sort each group of 5?How many steps to sort each group of 5?
98
Here's a clever idea
{6,2,1,3,9,8,5,7,4,0}{6,2,1,3,9,8,5,7,4,0} becomesbecomes{6,2,1,3,9} {8,5,7,4,0}{6,2,1,3,9} {8,5,7,4,0} and with 80 steps (40 for each) I haveand with 80 steps (40 for each) I have{1,2,3,6,9} {0,4,5,7,8}{1,2,3,6,9} {0,4,5,7,8} So if I can figure out how to combine them So if I can figure out how to combine them
into one sorted group with less than another into one sorted group with less than another 80 steps I'll be ahead80 steps I'll be ahead
99
Merging
Merging is combining two smaller sorted Merging is combining two smaller sorted groups into one larger sorted groupgroups into one larger sorted group
{1,2,3,6,9} {0,4,5,7,8}{1,2,3,6,9} {0,4,5,7,8} It only takes one pass (loop) through the It only takes one pass (loop) through the
numbers, so it's faster than nested loop numbers, so it's faster than nested loop sortingsorting
100
Merging
I'll set up an empty place to hold the final I'll set up an empty place to hold the final sorted numberssorted numbers
{{11,2,3,6,9} {,2,3,6,9} {00,4,5,7,8} { },4,5,7,8} { } I'll start with the first two numbers, and I'll start with the first two numbers, and
copy the smallercopy the smaller
101
Merging
I'll set up an empty place to hold the final I'll set up an empty place to hold the final sorted numberssorted numbers
{{11,2,3,6,9} {0,,2,3,6,9} {0,44,5,7,8} {0 },5,7,8} {0 } I'll I'll move to the next number, and copy I'll I'll move to the next number, and copy
the smallerthe smaller
102
Merging
I'll set up an empty place to hold the final I'll set up an empty place to hold the final sorted numberssorted numbers
{1,{1,22,3,6,9} {0,,3,6,9} {0,44,5,7,8} {0 ,1},5,7,8} {0 ,1} an so on. . .an so on. . .
103
Merging
I'll set up an empty place to hold the final I'll set up an empty place to hold the final sorted numberssorted numbers
{1,2,{1,2,33,6,9} {0,,6,9} {0,44,5,7,8} {0 ,1,2},5,7,8} {0 ,1,2} an so on. . .an so on. . .
104
Merging
I'll set up an empty place to hold the final I'll set up an empty place to hold the final sorted numberssorted numbers
{1,2,3,{1,2,3,66,9} {0,,9} {0,44,5,7,8} {0 ,1,2,3},5,7,8} {0 ,1,2,3} an so on. . .an so on. . .
105
Merging
I'll set up an empty place to hold the final I'll set up an empty place to hold the final sorted numberssorted numbers
{1,2,3,{1,2,3,66,9} {0,4,,9} {0,4,55,7,8} {0 ,1,2,3,4},7,8} {0 ,1,2,3,4} an so on. . .an so on. . .
106
Merging
I'll set up an empty place to hold the final I'll set up an empty place to hold the final sorted numberssorted numbers
{1,2,3,{1,2,3,66,9} {0,4,5,,9} {0,4,5,77,8} {0 ,1,2,3,4,5},8} {0 ,1,2,3,4,5} an so on. . .an so on. . .
107
Merging
I'll set up an empty place to hold the final I'll set up an empty place to hold the final sorted numberssorted numbers
{1,2,3,6,{1,2,3,6,99} {0,4,5,} {0,4,5,77,8} {0 ,1,2,3,4,5,6},8} {0 ,1,2,3,4,5,6} an so on. . .an so on. . .
108
Merging
I'll set up an empty place to hold the final I'll set up an empty place to hold the final sorted numberssorted numbers
{1,2,3,6,{1,2,3,6,99} {0,4,5,7,} {0,4,5,7,88} {0 ,1,2,3,4,5,6,7}} {0 ,1,2,3,4,5,6,7} an so on. . .an so on. . .
109
Merging
I'll set up an empty place to hold the final I'll set up an empty place to hold the final sorted numberssorted numbers
{1,2,3,6,{1,2,3,6,99} {0,4,5,7,8} {0 ,1,2,3,4,5,6,7,8}} {0,4,5,7,8} {0 ,1,2,3,4,5,6,7,8} we I get to the end of one group, I can just we I get to the end of one group, I can just
copy the rest overcopy the rest over
110
Merging
I'll set up an empty place to hold the final I'll set up an empty place to hold the final sorted numberssorted numbers
{1,2,3,6,9} {0,4,5,7,8} {1,2,3,6,9} {0,4,5,7,8} {0 ,1,2,3,4,5,6,7,8,9}{0 ,1,2,3,4,5,6,7,8,9}
we I get to the end of one group, I can just we I get to the end of one group, I can just copy the rest overcopy the rest over
111
Using a merge function First I'll rewrite my sort so that it only sorts part First I'll rewrite my sort so that it only sorts part
of the numbers, not all of themof the numbers, not all of thempublic void selectionSort(public void selectionSort( int[] list, int first, int last)int[] list, int first, int last)//sorts list from positions first to last//sorts list from positions first to last//code not shown//code not shown Then, I'll write a merge function that merges the Then, I'll write a merge function that merges the
numbers from first to mid, and mid + 1 to lastnumbers from first to mid, and mid + 1 to lastvoid merge(void merge( int[] a, int first, int mid, int last)int[] a, int first, int mid, int last)//code not shown//code not shown
112
How it could workint list = {6,2,1,3,9,8,5,7,4,0};int list = {6,2,1,3,9,8,5,7,4,0};selectionSort(list,0,4);selectionSort(list,0,4);
113
How it could workint list = {6,2,1,3,9,8,5,7,4,0};int list = {6,2,1,3,9,8,5,7,4,0};selectionSort(list,0,4);selectionSort(list,0,4);//sorts {6,2,1,3,9} to {1,2,3,6,9}//sorts {6,2,1,3,9} to {1,2,3,6,9}//list is now //list is now {{1,2,3,6,91,2,3,6,9,8,5,7,4,0};,8,5,7,4,0};
114
How it could workint list = {6,2,1,3,9,8,5,7,4,0};int list = {6,2,1,3,9,8,5,7,4,0};selectionSort(list,0,4);selectionSort(list,0,4);//sorts {6,2,1,3,9} to {1,2,3,6,9}//sorts {6,2,1,3,9} to {1,2,3,6,9}//list is now //list is now {{1,2,3,6,91,2,3,6,9,8,5,7,4,0};,8,5,7,4,0}; selectionSort(list,5,9);selectionSort(list,5,9);
115
How it could workint list = {6,2,1,3,9,8,5,7,4,0};int list = {6,2,1,3,9,8,5,7,4,0};selectionSort(list,0,4);selectionSort(list,0,4);//sorts {6,2,1,3,9} to {1,2,3,6,9}//sorts {6,2,1,3,9} to {1,2,3,6,9}//list is now //list is now {{1,2,3,6,91,2,3,6,9,8,5,7,4,0};,8,5,7,4,0}; selectionSort(list,5,9);selectionSort(list,5,9);//sorts {8,5,7,4,0} to {0,4,5,7,8}//sorts {8,5,7,4,0} to {0,4,5,7,8}//list is now //list is now {1,2,3,6,9,{1,2,3,6,9,0,4,5,7,80,4,5,7,8};};
116
How it could workint list = {6,2,1,3,9,8,5,7,4,0};int list = {6,2,1,3,9,8,5,7,4,0};selectionSort(list,0,4);selectionSort(list,0,4);//sorts {6,2,1,3,9} to {1,2,3,6,9}//sorts {6,2,1,3,9} to {1,2,3,6,9}//list is now //list is now {{1,2,3,6,91,2,3,6,9,8,5,7,4,0};,8,5,7,4,0}; selectionSort(list,5,9);selectionSort(list,5,9);//sorts {8,5,7,4,0} to {0,4,5,7,8}//sorts {8,5,7,4,0} to {0,4,5,7,8}//list is now //list is now {1,2,3,6,9,{1,2,3,6,9,0,4,5,7,80,4,5,7,8};};merge(list,0,4,9);merge(list,0,4,9);
117
How it could workint list = {6,2,1,3,9,8,5,7,4,0};int list = {6,2,1,3,9,8,5,7,4,0};selectionSort(list,0,4);selectionSort(list,0,4);//sorts {6,2,1,3,9} to {1,2,3,6,9}//sorts {6,2,1,3,9} to {1,2,3,6,9}//list is now //list is now {{1,2,3,6,91,2,3,6,9,8,5,7,4,0};,8,5,7,4,0}; selectionSort(list,5,9);selectionSort(list,5,9);//sorts {8,5,7,4,0} to {0,4,5,7,8}//sorts {8,5,7,4,0} to {0,4,5,7,8}//list is now //list is now {1,2,3,6,9,{1,2,3,6,9,0,4,5,7,80,4,5,7,8};};merge(list,0,4,9);merge(list,0,4,9);//merges {1,2,3,6,9}//merges {1,2,3,6,9}//and {0,4,5,7,8}//and {0,4,5,7,8}////list is now list is now {0,1,2,3,4,5,6,7,8,9}{0,1,2,3,4,5,6,7,8,9}
118
What is the output?int[] naArray = {4,7,-2,4,1,13,8};int[] naArray = {4,7,-2,4,1,13,8};int nFirst = 0;int nFirst = 0;int nLast = naArray.length - 1;int nLast = naArray.length - 1;int nMid = (nFirst + nLast)/2; int nMid = (nFirst + nLast)/2; //nMid is last position in first part of array//nMid is last position in first part of arrayselectionSort(naArray,nFirst,nMid);selectionSort(naArray,nFirst,nMid);print(naArray);print(naArray);selectionSort(naArray,nMid + 1,nLast);selectionSort(naArray,nMid + 1,nLast);print(naArray);print(naArray);merge(naArray,nFirst,nMid,nLast);merge(naArray,nFirst,nMid,nLast);print(naArray);print(naArray);
void print(int[] naList)void print(int[] naList){{
for(int nI = 0; nI < naList.length; nI++)for(int nI = 0; nI < naList.length; nI++) System.out.print(naList[nI] + ", ");System.out.print(naList[nI] + ", "); System.out.println();System.out.println();
}}public void selectionSort(int[] list, int first, int last)public void selectionSort(int[] list, int first, int last)//sorts list from positions first to last//sorts list from positions first to last//code not shown//code not shown
void merge(int[] a, int first, int mid, int last)void merge(int[] a, int first, int mid, int last)//code not shown//code not shown
119
How small should the groups be?
If dividing the numbers to be sorted into 2 If dividing the numbers to be sorted into 2 groups saves steps, would 4 be better?groups saves steps, would 4 be better?
120
How small should the groups be?
If dividing the numbers to be sorted into 2 If dividing the numbers to be sorted into 2 groups saves steps, would 4 be better?groups saves steps, would 4 be better?
YES!YES!
121
How small should the groups be?
If dividing the numbers to be sorted into 4 If dividing the numbers to be sorted into 4 groups saves steps, would 8 be better?groups saves steps, would 8 be better?
122
How small should the groups be?
If dividing the numbers to be sorted into 4 If dividing the numbers to be sorted into 4 groups saves steps, would 8 be better?groups saves steps, would 8 be better?
YES!YES!
123
Merge Sort 12 34 18 5 7
Merge Sort works with the idea that a single number by itself is always sorted. Merge Sort recursively breaks down the array into smaller and smaller sections, until each section only has one number.
124
Merge Sort 12 34 18 5 7
12 34 18 5 7
125
Merge Sort 12 34 18 5 7
12 34
12 34
18 5 7
5 718
126
Merge Sort 12 34 18 5 7
12 34
12 34
18 5 7
5 718
5 7
127
Merge Sort 12 34 18 5 7
12 34
12 34
18 5 7
5 718
5 7
Now we'll call private void merge(int[] a, int first, int mid, int last)
which will merge two sorted sections of an array backtogether
128
Merge Sort 12 34 18 5 7
12 34
12 34
18 5 7
5 718
5 712 34
5 7
129
Merge Sort 12 34 18 5 7
12 34
12 34
18 5 7
5 718
5 712 34
5 7
5 7 18
130
Merge Sort 12 34 18 5 7
12 34
12 34
18 5 7
5 718
5 712 34
5 7
5 7 18
5 7 12 18 34
131
Coding Merge Sort
Merge Sort uses two methods:Merge Sort uses two methods:
public void mergeSort(int[] a,public void mergeSort(int[] a, int first, int last)int first, int last)
private void merge(int[] a,private void merge(int[] a, int first, int mid, int last)int first, int mid, int last)
Writing Writing mergeSortmergeSort is easy is easy mergemerge is a little tougher is a little tougher
132
Coding Merge Sort
public void mergeSort(int[] a,public void mergeSort(int[] a, int first, int last)int first, int last){{
if( ?? )if( ?? ) {{ mergeSort(a, ?, ?);mergeSort(a, ?, ?); mergeSort(a, ?, ?);mergeSort(a, ?, ?); }} merge(a, ?, ? , ?);merge(a, ?, ? , ?);}}
133
Javabat mergeTwo http://www.javabat.com/prob?id=AP1.merghttp://www.javabat.com/prob?id=AP1.merg
eTwoeTwo Start with two arrays of strings, A and B, Start with two arrays of strings, A and B,
each with its elements in alphabetical order each with its elements in alphabetical order and without duplicates. Return a new array and without duplicates. Return a new array containing the first N elements from the two containing the first N elements from the two arrays. The result array should be in arrays. The result array should be in alphabetical order and alphabetical order and without duplicateswithout duplicates. A . A and B will both have a length which is N or and B will both have a length which is N or more. more.
134
mergeTwo(String[] a, String[] b, int n)
mergeTwo({"a", "c", "z"}, {"b", "f", "z"}, 3) Three arguments:Three arguments:
String[] aString[] aString[] bString[] bint nint n
135
mergeTwo({"a", "c", "z"}, {"b", "f", "z"}, 3)
The first thing I need is a new String array of the The first thing I need is a new String array of the right sizeright size
String saC[]= new String[String saC[]= new String[33];];
136
mergeTwo({"a", "c", "z"}, {"b", "f", "z"}, 3)
The first thing I need is a new String array of the The first thing I need is a new String array of the right sizeright size
String saC[]= new String[String saC[]= new String[33];]; I'll also need three index variables, one for each I'll also need three index variables, one for each
arrayarray int nA=0,nB=0,nC=0;int nA=0,nB=0,nC=0;
137
mergeTwo({"a", "c", "z"}, {"b", "f", "z"}, 3)
{ ?? , ?? , ?? } //saC{ ?? , ?? , ?? } //saC
138
mergeTwo({"a", "c", "z"}, {"b", "f", "z"}, 3)
{ "a" , ?? , ?? }{ "a" , ?? , ?? }
139
mergeTwo({"a", "c", "z"}, {"b", "f", "z"}, 3)
{ "a" , "b" , ?? }{ "a" , "b" , ?? }
140
mergeTwo({"a", "c", "z"}, {"b", "f", "z"}, 3)
{ "a" , "b" , "c" }{ "a" , "b" , "c" }
and stop!and stop!
141
mergeTwo({"a", "c", "z"}, {"c", "f", "z"}, 3)
{ ?? , ?? , ?? }{ ?? , ?? , ?? }
142
mergeTwo({"a", "c", "z"}, {"c", "f", "z"}, 3)
{ "a" , ?? , ?? }{ "a" , ?? , ?? }
143
mergeTwo({"a", "c", "z"}, {"c", "f", "z"}, 3)
{ "a" , "c" , ?? }{ "a" , "c" , ?? } Notice that we incremented the indexes Notice that we incremented the indexes
in in bothboth arrays! arrays!
144
mergeTwo({"a", "c", "z"}, {"c", "f", "z"}, 3)
{ "a" , "c" , "f" }{ "a" , "c" , "f" }
and stop!and stop!
145
comparing Strings
< and > don't work< and > don't work
146
comparing Strings
use use compareTocompareTo instead insteadpublic String[] mergeTwo(public String[] mergeTwo(
String[] a, String[] b, int n) String[] a, String[] b, int n)
{{
//lots of java//lots of java
if(if(a[nA].a[nA].compareTo(compareTo(b[nB]b[nB]) < 0)) < 0)
147
comparing Strings
< and > don't work< and > don't workif(if(a[nA].a[nA].compareTo(compareTo(b[nB]b[nB]) < 0)) < 0)
If the If the element in a at position nAelement in a at position nA comes before comes before b[nB]b[nB] alphabetically alphabetically
148
comparing Strings
< and > don't work< and > don't workif(if(a[nA].a[nA].compareTo(compareTo(b[nB]b[nB]) > 0)) > 0)
If the If the element in a at position nAelement in a at position nA comes after comes after b[nB]b[nB] alphabetically alphabetically
149
comparing Strings
< and > don't work< and > don't workif(if(a[nA].a[nA].compareTo(compareTo(b[nB]b[nB]) == 0)) == 0)
If the If the element in a at position nAelement in a at position nA is the same as is the same as b[nB]b[nB] alphabetically alphabetically
150
Coding Merge
private void merge(int[] naArray,private void merge(int[] naArray, int nFirst, int nMid, int nLast)int nFirst, int nMid, int nLast)
nFirstnFirst is the position of the first element is the position of the first element in the first (sorted) section of the arrayin the first (sorted) section of the array
nMidnMid is the position of the the last element is the position of the the last element in the first (sorted) section of the arrayin the first (sorted) section of the array
nLastnLast is the position of the last element is the position of the last element in the second (sorted) section of the arrayin the second (sorted) section of the array
151
Coding Merge
{17,12,5,8,9,3,6,7,2,1}{17,12,5,8,9,3,6,7,2,1}
In this part of the array, In this part of the array, nFirstnFirst is 2 is 2 nMidnMid is 4 is 4 nLastnLast is 7 is 7
152
Coding Merge
{17,12,5,8,9,3,6,7,2,1}{17,12,5,8,9,3,6,7,2,1}
We'll start with a temporary array We'll start with a temporary array big enough to hold the two sorted big enough to hold the two sorted sectionssections
int[] naTemp = new int[??];int[] naTemp = new int[??];
153
Coding Merge
{17,12,5,8,9,3,6,7,2,1}{17,12,5,8,9,3,6,7,2,1}
Then, we'll compare the numbers at Then, we'll compare the numbers at nFirstnFirst and and nMid + 1nMid + 1, and copy the , and copy the smaller one to smaller one to naTempnaTemp
154
Coding Merge
{17,12,5,8,9,3,6,7,2,1}{17,12,5,8,9,3,6,7,2,1}
In this case In this case nMid + 1nMid + 1 had the smaller had the smaller numbernumber
We'll move to the next value after We'll move to the next value after nMid + nMid + 11 after copying it's value to after copying it's value to naTempnaTemp
3
155
Coding Merge
{17,12,5,8,9,3,6,7,2,1}{17,12,5,8,9,3,6,7,2,1}
3 5
156
Coding Merge
{17,12,5,8,9,3,6,7,2,1}{17,12,5,8,9,3,6,7,2,1}
3 5 6
157
Coding Merge
{17,12,5,8,9,3,6,7,2,1}{17,12,5,8,9,3,6,7,2,1}
3 5 6 7
158
Coding Merge
{17,12,5,8,9,3,6,7,2,1}{17,12,5,8,9,3,6,7,2,1}
Once we get to the end of one section, we Once we get to the end of one section, we can copy the rest into the temp arraycan copy the rest into the temp array
3 5 6 7 8 9
159
"pseudo-code" for merge declare and initialize nTempIndex to first;declare and initialize nTempIndex to first; declare and initialize nIndex1 to first, nIndex2 to mid + 1declare and initialize nIndex1 to first, nIndex2 to mid + 1 declare naTemp as copy of the array adeclare naTemp as copy of the array a while nIndex1 <=mid and nIndex2<=lastwhile nIndex1 <=mid and nIndex2<=last
if a[nIndex1] less than a[nIndex2]if a[nIndex1] less than a[nIndex2] set naTemp[nTempIndex] to a[nIndex1]set naTemp[nTempIndex] to a[nIndex1] increment nIndex1 and nTempIndexincrement nIndex1 and nTempIndex
elseelse set naTemp[nTempIndex] to a[nIndex2]set naTemp[nTempIndex] to a[nIndex2] increment nIndex2 and nTempIndex++increment nIndex2 and nTempIndex++
while nIndex1<=midwhile nIndex1<=mid set naTemp[nTempIndex] to a[nIndex1]set naTemp[nTempIndex] to a[nIndex1] increment nTempIndex and nIndex1increment nTempIndex and nIndex1
while nIndex2<=lastwhile nIndex2<=last set naTemp[nTempIndex] to a[nIndex2]set naTemp[nTempIndex] to a[nIndex2] increment nTempIndex and nIndex2increment nTempIndex and nIndex2
copy naTemp to the array acopy naTemp to the array a
160
Copying an array
int[] na1 = {1,2,3,4};int[] na1 = {1,2,3,4};
int[] na2 = na1;int[] na2 = na1; na2na2 is is NOTNOT a copy of a copy of na1na1 na2na2 is an is an ALIASALIAS of of na1na1
{1,2,3,4}{1,2,3,4}
161
Copying an array
int[] na1 = {1,2,3,4};int[] na1 = {1,2,3,4};int[] na2 = new int[na1.length];int[] na2 = new int[na1.length];for(int nI = 0; nI < na1.length; nI++)for(int nI = 0; nI < na1.length; nI++)
na2[nI] = na1[nI];na2[nI] = na1[nI]; NowNow na2 na2 is a copy of is a copy of na1na1
{1,2,3,4}{1,2,3,4}
{1,2,3,4}{1,2,3,4}
162
Efficiency in MergeSort
The temporary array in merge sort only needs to The temporary array in merge sort only needs to hold the values we are currently sortinghold the values we are currently sorting
Remember, MergeSort recursively breaks down Remember, MergeSort recursively breaks down the array into smaller and smaller sectionsthe array into smaller and smaller sections
You can significantly reduce steps by only You can significantly reduce steps by only copying the values you are working on (from copying the values you are working on (from nFnFirstirst to nL to nLastast) rather than the entire array) rather than the entire array
for(int nI = for(int nI = nFirstnFirst; nI <= ; nI <= nLastnLast; nI++); nI++)
na2[nI] = na1[nI];na2[nI] = na1[nI];
163
Quick Sort 12 34 18 5 7
Quick Sort is a two step process:1. A helper function partitions the numbers
into two groups2. Quick Sort is recursively called to sort
the two partitions
164
Quick Sort 12 34 18 5 7
Pivot: 12
165
Quick Sort 12 34 18 5 7
Pivot: 12
34 185 7
166
Quick Sort 12 34 18 5 7
Pivot: 12
34 185 7
167
Quick Sort 12 34 18 5 7
Pivot: 12
34 185 7 12
168
Quick Sort 12 34 18 5 7
Pivot: 5
34 185 7 12
169
Quick Sort 12 34 18 5 7
Pivot: 5
34 18 5 7 12
7 12
170
Quick Sort 12 34 18 5 7
Pivot: 5
34 18 5 7 12
5 7 12
171
Quick Sort 12 34 18 5 7
Pivot: 34
34 18 34 18 12
5 7 12 18
172
Quick Sort 12 34 18 5 7
Pivot: 34
34 18 5 7 12
5 7 12 3418
173
Coding Quick Sort
void quicksort(int []naA, int nLow, int nHigh)void quicksort(int []naA, int nLow, int nHigh)
{{ int nPivot;int nPivot; if ( nHigh > nLow )if ( nHigh > nLow ) {{ nPivot = partition( naA, nLow, nHigh );nPivot = partition( naA, nLow, nHigh ); quicksort(naA, nLow, nPivot-1 );quicksort(naA, nLow, nPivot-1 ); quicksort(naA, nPivot+1, nHigh );quicksort(naA, nPivot+1, nHigh ); }}}}
174
Coding Quick Sortint partition(int []naA, int nLow, int nHigh)int partition(int []naA, int nLow, int nHigh){{ int nPos = nLow;int nPos = nLow; int nPivot = naA[nLow];int nPivot = naA[nLow]; while(nLow < nHigh)while(nLow < nHigh) {{
while (nLow < naA.length && naA[nLow] <= nPivot)while (nLow < naA.length && naA[nLow] <= nPivot) nLow++;nLow++;
while (naA[nHigh] > nPivot)while (naA[nHigh] > nPivot) nHigh--;nHigh--;
if (nLow < nHigh) //swapif (nLow < nHigh) //swap {{
int nTemp = naA[nLow];int nTemp = naA[nLow]; naA[nLow] = naA[nHigh];naA[nLow] = naA[nHigh]; naA[nHigh] = nTemp;naA[nHigh] = nTemp; }} }} while(nPos < nHigh)while(nPos < nHigh) {{ naA[nPos]=naA[nPos+1];naA[nPos]=naA[nPos+1]; nPos++;nPos++; }} naA[nHigh] = nPivot;naA[nHigh] = nPivot; return nHigh;return nHigh;}}
175
What would be the Big O expression for "guess and check" sort? In "Guess and Check" sort, we randomly In "Guess and Check" sort, we randomly
arrange an array of numbers, and check to arrange an array of numbers, and check to see if it's sortedsee if it's sorted
If it's not sorted, we randomly arrange it If it's not sorted, we randomly arrange it againagain
176
What would be the Big O expression for "guess and check" sort? Let's say the array had two numbers 13 & 17: how many Let's say the array had two numbers 13 & 17: how many
ways could they be arranged?ways could they be arranged?13, 1713, 1717, 1317, 13
Let's increase the size of the data by one, adding the Let's increase the size of the data by one, adding the number 7. How many ways could they be arranged number 7. How many ways could they be arranged now?now?
7, 13, 177, 13, 177, 17, 137, 17, 1313, 7, 1713, 7, 1713, 17, 713, 17, 717, 13, 717, 13, 717, 7, 1317, 7, 13
177
What would be the Big O expression for "guess and check" sort? Increasing the size of the data by just one Increasing the size of the data by just one
more than doublesmore than doubles the number of steps! the number of steps! This is an example of This is an example of O(n!)O(n!)
178
Graph of O(2n), O(n3), O(n2),O(n logn), O(n), O(log n)
179
Here's a sort that's faster than Quick Sort
It's called "Bucket Sort"It's called "Bucket Sort" Let's say I want to sort the numbers Let's say I want to sort the numbers
{5,9,3,7,2,1}{5,9,3,7,2,1} I need to know the I need to know the largestlargest and and smallestsmallest
possible values: In this case I know the possible values: In this case I know the numbers are all between 1 and 10numbers are all between 1 and 10
I then make "buckets" to hold each of the I then make "buckets" to hold each of the possible valuespossible values
180
Bucket Sort
5 9 3 7 2 1
1 2 3 4 5
6 7 8 9 10
181
Bucket Sort
5 9 3 7 2 1
1 2 3 4 5
6 7 8 9 10
182
Bucket Sort
5 9 3 7 2 1
1 2 3 455
6 7 8 9 10
183
Bucket Sort
5 9 3 7 2 1
1 2 3 455
6 7 8 9 10
184
Bucket Sort
5 9 3 7 2 1
1 2 3 455
6 7 899 10
185
Bucket Sort
5 9 3 7 2 1
1 2 3 455
6 7 899 10
186
Bucket Sort
5 9 3 7 2 1
1 233 4
55
6 7 899 10
187
Bucket Sort
5 9 3 7 2 1
1 233 4
55
6 7 899 10
188
Bucket Sort
5 9 3 7 2 1
1 233 4
55
677 8
99 10
189
Bucket Sort
5 9 3 7 2 1
1 233 4
55
677 8
99 10
190
Bucket Sort
5 9 3 7 2 1
122
33 4
55
677 8
99 10
191
Bucket Sort
5 9 3 7 2 1
122
33 4
55
677 8
99 10
192
Bucket Sort
5 9 3 7 2 1
11
22
33 4
55
677 8
99 10
193
Bucket Sort
5 9 3 7 2 1
11
22
33 4
55
677 8
99 10
Now I can just go through the buckets in order
194
Bucket Sort
I only need to make 2 passes through the I only need to make 2 passes through the numbers, once to put them in the buckets numbers, once to put them in the buckets and once to take them outand once to take them out
For 6 numbers, that's 12 stepsFor 6 numbers, that's 12 steps How many steps for 12 numbers?How many steps for 12 numbers? What's the Big-O Notation for Bucket Sort?What's the Big-O Notation for Bucket Sort? Why isn't Bucket Sort used more commonly Why isn't Bucket Sort used more commonly
than Quick Sort?than Quick Sort?
195
Javabat: Scores increasing
http://www.javabat.com/prob?id=AP1.scorehttp://www.javabat.com/prob?id=AP1.scoresIncreasingsIncreasing
In some ways, it's kind of like sorting:In some ways, it's kind of like sorting: Go through the numbers two at a timeGo through the numbers two at a time If you find two that are out of order, instead If you find two that are out of order, instead
of swapping, just return of swapping, just return falsefalse!!
196
Linear Search
Let's say you had an array of 100 numbers Let's say you had an array of 100 numbers in no particular order and you wanted to in no particular order and you wanted to know if the number 17 was in the arrayknow if the number 17 was in the array
You would have to look at the first number, You would have to look at the first number, then the second, and so on until you found then the second, and so on until you found 17 or reached the end of the array17 or reached the end of the array
That's That's linear searchlinear search, an , an O(n)O(n) algorithm algorithm
197
Binary Search
I'm thinking of a number between 1 and 100I'm thinking of a number between 1 and 100 I want you to try and guess itI want you to try and guess it I'll tell you if your guess is too high, too low I'll tell you if your guess is too high, too low
or if you guessed correctlyor if you guessed correctly What would be the first number you would What would be the first number you would
guess?guess? If your answer is 50, you know how If your answer is 50, you know how Binary Binary
SearchSearch works works
198
Binary Search
If someone correctly uses the Binary Search If someone correctly uses the Binary Search strategy, how many guesses will they need strategy, how many guesses will they need at mostat most to guess the number? to guess the number?
199
Binary Search
Number of GuessesNumber of Guesses Possible NumbersPossible Numbers00 10010011 505022 252533 131344 7755 4466 2277 11
200
Binary Search
Anyone should be able to guess the number Anyone should be able to guess the number in in at mostat most 7 guesses 7 guesses
How many guesses would we need if we How many guesses would we need if we doubled the possibilities to 200 (a number doubled the possibilities to 200 (a number between 1 and 200)?between 1 and 200)?
8, just one more guess8, just one more guess Binary Search is an Binary Search is an O(log n)O(log n) algorithm algorithm
201
Playing the number guessing game
nHigh 100nHigh 100
nGuess 50nGuess 50
nLow 1nLow 1
202
Playing the number guessing game
nHigh 100nHigh 100
nGuess 50nGuess 50
nLow 1nLow 1
Too HighToo High
203
Playing the number guessing game
nHigh 100 49nHigh 100 49
nGuess 50 25nGuess 50 25
nLow 1nLow 1
204
Playing the number guessing game
nHigh 100 49nHigh 100 49
nGuess 50 25nGuess 50 25
nLow 1nLow 1
Too LowToo Low
205
Playing the number guessing game
nHigh 100 49nHigh 100 49
nGuess 50 25 37nGuess 50 25 37
nLow 1 26nLow 1 26
206
Playing the number guessing game
nHigh 100 49nHigh 100 49
nGuess 50 25 37nGuess 50 25 37
nLow 1 26nLow 1 26
Too HighToo High
207
Playing the number guessing game
nHigh 100 49 36nHigh 100 49 36
nGuess 50 25 37 31nGuess 50 25 37 31
nLow 1 26nLow 1 26
And so on. . . .And so on. . . .
Until either we guess the number orUntil either we guess the number or
there are no more numbers to guessthere are no more numbers to guess
208
Practice Quiz Questions
Using the Binary Search strategy of the Using the Binary Search strategy of the "number guessing game", what elements of "number guessing game", what elements of the following array would be visited if we the following array would be visited if we were searching for 7?were searching for 7?
{0,3,4,7,11,13,15,17,21,33}{0,3,4,7,11,13,15,17,21,33} Is binary search always faster than Is binary search always faster than
sequential search?sequential search?
209
{0,3,4,7,11,13,15,17,21,33}
nLow 0 nGuess 4 nHigh 9nLow 0 nGuess 4 nHigh 9
210
{0,3,4,7,11,13,15,17,21,33}
nLow 0 nGuess 1 nHigh 3nLow 0 nGuess 1 nHigh 3
211
{0,3,4,7,11,13,15,17,21,33}
nLow 2 nGuess 2 nHigh 3nLow 2 nGuess 2 nHigh 3
212
{0,3,4,7,11,13,15,17,21,33}
nLow 3 nGuess 3 nHigh 3nLow 3 nGuess 3 nHigh 3
213
Radix Sort
"Bucket Sort" is very efficient in terms of "Bucket Sort" is very efficient in terms of steps—O(n)steps—O(n)
But it is very But it is very inefficientinefficient in terms of memory in terms of memory And it cannot handle And it cannot handle duplicate valuesduplicate values By changing the "buckets" to linked lists, By changing the "buckets" to linked lists,
memory can be handled efficiently as wellmemory can be handled efficiently as well And And anyany numbernumber of items can be stored in a of items can be stored in a
linked list, not just one like a "bucket"linked list, not just one like a "bucket"
214
radix sort 0123456789
An array of 10Linked Lists
215
radix sort 0123456789
The firstpass willarrangethe numbersin order ofthe onesdigit
216
radix sort0123456789
217
radix sort0123456789
218
radix sort0123456789
219
radix sort0123456789
220
radix sort0123456789
221
radix sort0123456789
Now, we'll repeat the process usingthe tens digit
222
radix sort0123456789
0123456789
223
radix sort0123456789
0123456789
224
radix sort0123456789
0123456789
225
radix sort0123456789
0123456789
226
radix sort0123456789
0123456789
227
radix sort0123456789
0123456789
Once again,this time using the hundreds digit
228
radix sort
0123456789
0123456789
229
radix sort
0123456789
0123456789
230
radix sort
0123456789
0123456789
231
radix sort
0123456789
0123456789
232
radix sort
0123456789
0123456789
233
radix sort
0123456789
After threepasses, onefor eachdigit, the numbers arein order
234
Radix Sort
Three passes, one for each digit, sorted the Three passes, one for each digit, sorted the 5 numbers5 numbers
Basically, that's Basically, that's 15 steps15 steps How many steps for 10 numbers with a How many steps for 10 numbers with a
maximum of 3 digits?maximum of 3 digits?
235
Radix Sort
O(n)O(n) Faster than QuickSort, MergeSort and other Faster than QuickSort, MergeSort and other
O(n log n) algorithmsO(n log n) algorithms Less efficient use of memory than other Less efficient use of memory than other
sorting algorithms, thoughsorting algorithms, though
236
hash tables (AB only)
A hash table is another variation of the bucket A hash table is another variation of the bucket sort:sort: Elements are Elements are arrangedarranged, but , but notnot sortedsorted or or
orderedordered in a way you would expect in a way you would expect Unlike radix sort, we only makeUnlike radix sort, we only make one pass one pass
through the datathrough the data Not really a sorting algorithm, but a Not really a sorting algorithm, but a data data
structurestructure that can be used to store and retrieve that can be used to store and retrieve items.items.
237
hash tables
Let's say I have five numbers, each of which is Let's say I have five numbers, each of which is between 0 and 400:between 0 and 400:
7 266 399 12 1257 266 399 12 125 I'll make an array of 5 linked lists—the I'll make an array of 5 linked lists—the hash tablehash table I'll take the I'll take the number % 5number % 5 and use that to decide and use that to decide
which of the 5 lists the number will be assignedwhich of the 5 lists the number will be assigned I'm using I'm using % 5% 5 because there are because there are 55 numbers to be numbers to be
sorted—to sorted—to 55 lists lists In this example, In this example, % 5% 5 is the "hash function" is the "hash function"
238
hash tables
0
1
2
3
4
239
hash tables
0
1
2
3
4
240
hash tables
0
1
2
3
4
241
hash tables
0
1
2
3
4
242
hash tables
0
1
2
3
4
243
hash tables
0
1
2
3
4
12 and 7 are placed in the same location by the hash function. This is called a collision. 12 is inserted at the head of the linked list.
244
hash tables
0
1
2
3
4
245
Once the table is complete, it's quick and easy to retrieve an item
0
1
2
3
4
Let's say I'm searching for the value 532. I do 532%5 = 2. That means that 532 would be at position 2 if it is in the hash table. We go to the the linked list in position 2 and do a linear search for the value 532.
246
Once the table is complete, it's quick and easy to retrieve an item
0
1
2
3
4
Let's say I'm searching for the value 532. I do 532%5 = 2. That means that 532 would be at position 2 if it is in the hash table. We go to the the linked list in position 2 and do a linear search for the value 532.
247
hash tables
0
1
2
3
4
In this example, one of the five spots is empty.% of null pointers = 20%The average length of the (non-empty) linked lists are:(1 + 1 + 2 + 1)/4 = 1.25Ideally, we'd like to have 0 % null pointers and an average length of 1. We could write a different hash function and see if it makes an improvement
248
hash function: add the digits up and then % 5
0
1
2
3
4
249
hash function: sin(number) *2.5 + 2.5
0
1
2
3
4
250
hash function: .number * 6
0
1
2
3
4
251
hash tables
If there are no collisions, putting a single If there are no collisions, putting a single item in the Hash table is item in the Hash table is O(1), O(1), putting all putting all the items in the Hash Table isthe items in the Hash Table is O(n) O(n)
If there are no collisions, searching for a If there are no collisions, searching for a single item is single item is O(1)O(1)
Just use the "hash function" to find where Just use the "hash function" to find where the number would be storedthe number would be stored
252
hashCode() You don't have to think of your own You don't have to think of your own
hashing methodhashing method Every class inherits Every class inherits hashCode()hashCode() from from
ObjectObject Uses "Some Formula" we don't know or Uses "Some Formula" we don't know or
care aboutcare about Every Object is associated with an integer Every Object is associated with an integer
value called its hash codevalue called its hash code equalequal objects have objects have equalequal hash codes hash codes