SearchingSorting.ppt

252
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 nN nN was 5? or 20? was 5? or 20? What effect does doubling What effect does doubling nN nN have on have on nCount nCount ? ?

Transcript of SearchingSorting.ppt

Page 1: 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??

Page 2: SearchingSorting.ppt

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++)

Page 3: SearchingSorting.ppt

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

Page 4: SearchingSorting.ppt

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

Page 5: SearchingSorting.ppt

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

Page 6: SearchingSorting.ppt

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

Page 7: SearchingSorting.ppt

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

Page 8: SearchingSorting.ppt

8

Bubble Sort

and so on. . .and so on. . .

12 18 12 18 34 534 5 7 7

Page 9: SearchingSorting.ppt

9

Bubble Sort

and so on. . .and so on. . .

12 18 12 18 5 345 34 7 7

Page 10: SearchingSorting.ppt

10

Bubble Sort

and so on. . .and so on. . .

12 18 512 18 5 34 34 77

Page 11: SearchingSorting.ppt

11

Bubble Sort

and so on. . .and so on. . .

12 18 512 18 5 7 7 3434

Page 12: SearchingSorting.ppt

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

Page 13: SearchingSorting.ppt

13

Bubble Sort

12 1812 18 5 5 7 7 3434

Page 14: SearchingSorting.ppt

14

Bubble Sort

12 12 18 518 5 7 7 3434

Page 15: SearchingSorting.ppt

15

Bubble Sort

12 12 5 185 18 7 7 3434

Page 16: SearchingSorting.ppt

16

Bubble Sort

12 5 12 5 18 18 7 7 3434

Page 17: SearchingSorting.ppt

17

Bubble Sort

12 5 12 5 7 7 18 18 3434

Page 18: SearchingSorting.ppt

18

Bubble Sort

12 5 12 5 7 7 18 18 3434

Page 19: SearchingSorting.ppt

19

Bubble Sort

12 512 5 7 7 18 18 3434

Page 20: SearchingSorting.ppt

20

Bubble Sort

5 125 12 7 7 18 18 3434

Page 21: SearchingSorting.ppt

21

Bubble Sort

5 5 1212 7 7 18 18 3434

Page 22: SearchingSorting.ppt

22

Bubble Sort

5 5 77 12 12 18 18 3434

Page 23: SearchingSorting.ppt

23

Bubble Sort

5 7 5 7 1212 18 18 3434

Page 24: SearchingSorting.ppt

24

Bubble Sort

5 75 7 1212 18 18 3434

Page 25: SearchingSorting.ppt

25

Bubble Sort

5 75 7 1212 18 18 3434

Page 26: SearchingSorting.ppt

26

Bubble Sort

Finally, the numbers are sortedFinally, the numbers are sorted

5 7 12 18 345 7 12 18 34

Page 27: SearchingSorting.ppt

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

Page 28: SearchingSorting.ppt

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; }} }} }}}}

Page 29: SearchingSorting.ppt

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 ++)

Page 30: SearchingSorting.ppt

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 }} }} }}}}

Page 31: SearchingSorting.ppt

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); }}}}

Page 32: SearchingSorting.ppt

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)

Page 33: SearchingSorting.ppt

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

Page 34: SearchingSorting.ppt

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

Page 35: SearchingSorting.ppt

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

Page 36: SearchingSorting.ppt

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

Page 37: SearchingSorting.ppt

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

Page 38: SearchingSorting.ppt

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

Page 39: SearchingSorting.ppt

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

Page 40: SearchingSorting.ppt

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

Page 41: SearchingSorting.ppt

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

Page 42: SearchingSorting.ppt

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

Page 43: SearchingSorting.ppt

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

Page 44: SearchingSorting.ppt

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

Page 45: SearchingSorting.ppt

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

Page 46: SearchingSorting.ppt

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

Page 47: SearchingSorting.ppt

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

Page 48: SearchingSorting.ppt

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

Page 49: SearchingSorting.ppt

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

Page 50: SearchingSorting.ppt

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

Page 51: SearchingSorting.ppt

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

Page 52: SearchingSorting.ppt

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

Page 53: SearchingSorting.ppt

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

Page 54: SearchingSorting.ppt

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

Page 55: SearchingSorting.ppt

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

Page 56: SearchingSorting.ppt

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

Page 57: SearchingSorting.ppt

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

Page 58: SearchingSorting.ppt

58

Selection Sort

12 34 12 34 18 18 5 7 5 7

nFlag = 0nFlag = 0

Page 59: SearchingSorting.ppt

59

Selection Sort

12 34 18 12 34 18 5 5 7 7

nFlag = 0 3nFlag = 0 3

Page 60: SearchingSorting.ppt

60

Selection Sort

12 34 18 5 12 34 18 5 77

nFlag = 3nFlag = 3

Page 61: SearchingSorting.ppt

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

Page 62: SearchingSorting.ppt

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

Page 63: SearchingSorting.ppt

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

Page 64: SearchingSorting.ppt

64

Selection Sort

5 5 34 18 34 18 12 12 7 7

nFlag = 2nFlag = 2

Page 65: SearchingSorting.ppt

65

Selection Sort

5 5 34 18 12 34 18 12 77

nFlag = 3nFlag = 3

Page 66: SearchingSorting.ppt

66

Selection Sort

5 5 34 34 18 12 18 12 77

nFlag = 4nFlag = 4

Page 67: SearchingSorting.ppt

67

Selection Sort

5 5 7 7 18 12 34 18 12 34

nFlag = 4nFlag = 4

Page 68: SearchingSorting.ppt

68

Selection Sort

5 5 7 7 18 18 12 12 34 34

nFlag = 2nFlag = 2

Page 69: SearchingSorting.ppt

69

Selection Sort

5 5 7 7 18 18 12 12 34 34

nFlag = 3nFlag = 3

Page 70: SearchingSorting.ppt

70

Selection Sort

5 5 7 7 1212 18 18 3434

nFlag = 3nFlag = 3

Page 71: SearchingSorting.ppt

71

Selection Sort

5 5 7 7 1212 18 18 34 34

nFlag = 3nFlag = 3

Page 72: SearchingSorting.ppt

72

Selection Sort

5 5 7 7 1212 18 3418 34

nFlag = 3nFlag = 3

Page 73: SearchingSorting.ppt

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; }}}}

Page 74: SearchingSorting.ppt

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

Page 75: SearchingSorting.ppt

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; }}}}

Page 76: SearchingSorting.ppt

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]);

Page 77: SearchingSorting.ppt

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

Page 78: SearchingSorting.ppt

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

Page 79: SearchingSorting.ppt

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

Page 80: SearchingSorting.ppt

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

Page 81: SearchingSorting.ppt

81

Insertion Sort

12 __ 3412 __ 34 5 7 5 7

nTemp = 18nTemp = 18

Page 82: SearchingSorting.ppt

82

Insertion Sort

12 18 3412 18 34 __ 7 __ 7

nTemp = 5nTemp = 5

Page 83: SearchingSorting.ppt

83

Insertion Sort

12 18 __ 3412 18 __ 34 7 7

nTemp = 5nTemp = 5

Page 84: SearchingSorting.ppt

84

Insertion Sort

12 __ 18 3412 __ 18 34 7 7

nTemp = 5nTemp = 5

Page 85: SearchingSorting.ppt

85

Insertion Sort

__ 12 18 34__ 12 18 34 7 7

nTemp = 5nTemp = 5

Page 86: SearchingSorting.ppt

86

Insertion Sort

5 12 18 345 12 18 34 __ __

nTemp = 7nTemp = 7

Page 87: SearchingSorting.ppt

87

Insertion Sort

5 12 18 __ 345 12 18 __ 34

nTemp = 7nTemp = 7

Page 88: SearchingSorting.ppt

88

Insertion Sort

5 12 __ 18 345 12 __ 18 34

nTemp = 7nTemp = 7

Page 89: SearchingSorting.ppt

89

Insertion Sort

5 __ 12 18 345 __ 12 18 34

nTemp = 7nTemp = 7

Page 90: SearchingSorting.ppt

90

Insertion Sort

5 7 12 18 345 7 12 18 34

nTemp = 7nTemp = 7

Page 91: SearchingSorting.ppt

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; }}}}

Page 92: SearchingSorting.ppt

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

Page 93: SearchingSorting.ppt

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

Page 94: SearchingSorting.ppt

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

Page 95: SearchingSorting.ppt

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?

Page 96: SearchingSorting.ppt

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?

Page 97: SearchingSorting.ppt

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?

Page 98: SearchingSorting.ppt

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

Page 99: SearchingSorting.ppt

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

Page 100: SearchingSorting.ppt

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

Page 101: SearchingSorting.ppt

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

Page 102: SearchingSorting.ppt

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. . .

Page 103: SearchingSorting.ppt

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. . .

Page 104: SearchingSorting.ppt

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. . .

Page 105: SearchingSorting.ppt

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. . .

Page 106: SearchingSorting.ppt

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. . .

Page 107: SearchingSorting.ppt

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. . .

Page 108: SearchingSorting.ppt

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. . .

Page 109: SearchingSorting.ppt

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

Page 110: SearchingSorting.ppt

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

Page 111: SearchingSorting.ppt

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

Page 112: SearchingSorting.ppt

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);

Page 113: SearchingSorting.ppt

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};

Page 114: SearchingSorting.ppt

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);

Page 115: SearchingSorting.ppt

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};};

Page 116: SearchingSorting.ppt

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);

Page 117: SearchingSorting.ppt

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}

Page 118: SearchingSorting.ppt

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

Page 119: SearchingSorting.ppt

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?

Page 120: SearchingSorting.ppt

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!

Page 121: SearchingSorting.ppt

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?

Page 122: SearchingSorting.ppt

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!

Page 123: SearchingSorting.ppt

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.

Page 124: SearchingSorting.ppt

124

Merge Sort 12 34 18 5 7

12 34 18 5 7

Page 125: SearchingSorting.ppt

125

Merge Sort 12 34 18 5 7

12 34

12 34

18 5 7

5 718

Page 126: SearchingSorting.ppt

126

Merge Sort 12 34 18 5 7

12 34

12 34

18 5 7

5 718

5 7

Page 127: SearchingSorting.ppt

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

Page 128: SearchingSorting.ppt

128

Merge Sort 12 34 18 5 7

12 34

12 34

18 5 7

5 718

5 712 34

5 7

Page 129: SearchingSorting.ppt

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

Page 130: SearchingSorting.ppt

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

Page 131: SearchingSorting.ppt

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

Page 132: SearchingSorting.ppt

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, ?, ? , ?);}}

Page 133: SearchingSorting.ppt

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.

Page 134: SearchingSorting.ppt

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

Page 135: SearchingSorting.ppt

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];];

Page 136: SearchingSorting.ppt

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;

Page 137: SearchingSorting.ppt

137

mergeTwo({"a", "c", "z"}, {"b", "f", "z"}, 3)

{ ?? , ?? , ?? } //saC{ ?? , ?? , ?? } //saC

Page 138: SearchingSorting.ppt

138

mergeTwo({"a", "c", "z"}, {"b", "f", "z"}, 3)

{ "a" , ?? , ?? }{ "a" , ?? , ?? }

Page 139: SearchingSorting.ppt

139

mergeTwo({"a", "c", "z"}, {"b", "f", "z"}, 3)

{ "a" , "b" , ?? }{ "a" , "b" , ?? }

Page 140: SearchingSorting.ppt

140

mergeTwo({"a", "c", "z"}, {"b", "f", "z"}, 3)

{ "a" , "b" , "c" }{ "a" , "b" , "c" }

and stop!and stop!

Page 141: SearchingSorting.ppt

141

mergeTwo({"a", "c", "z"}, {"c", "f", "z"}, 3)

{ ?? , ?? , ?? }{ ?? , ?? , ?? }

Page 142: SearchingSorting.ppt

142

mergeTwo({"a", "c", "z"}, {"c", "f", "z"}, 3)

{ "a" , ?? , ?? }{ "a" , ?? , ?? }

Page 143: SearchingSorting.ppt

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!

Page 144: SearchingSorting.ppt

144

mergeTwo({"a", "c", "z"}, {"c", "f", "z"}, 3)

{ "a" , "c" , "f" }{ "a" , "c" , "f" }

and stop!and stop!

Page 145: SearchingSorting.ppt

145

comparing Strings

< and > don't work< and > don't work

Page 146: SearchingSorting.ppt

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)

Page 147: SearchingSorting.ppt

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

Page 148: SearchingSorting.ppt

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

Page 149: SearchingSorting.ppt

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

Page 150: SearchingSorting.ppt

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

Page 151: SearchingSorting.ppt

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

Page 152: SearchingSorting.ppt

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[??];

Page 153: SearchingSorting.ppt

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

Page 154: SearchingSorting.ppt

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

Page 155: SearchingSorting.ppt

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

Page 156: SearchingSorting.ppt

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

Page 157: SearchingSorting.ppt

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

Page 158: SearchingSorting.ppt

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

Page 159: SearchingSorting.ppt

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

Page 160: SearchingSorting.ppt

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}

Page 161: SearchingSorting.ppt

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}

Page 162: SearchingSorting.ppt

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];

Page 163: SearchingSorting.ppt

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

Page 164: SearchingSorting.ppt

164

Quick Sort 12 34 18 5 7

Pivot: 12

Page 165: SearchingSorting.ppt

165

Quick Sort 12 34 18 5 7

Pivot: 12

34 185 7

Page 166: SearchingSorting.ppt

166

Quick Sort 12 34 18 5 7

Pivot: 12

34 185 7

Page 167: SearchingSorting.ppt

167

Quick Sort 12 34 18 5 7

Pivot: 12

34 185 7 12

Page 168: SearchingSorting.ppt

168

Quick Sort 12 34 18 5 7

Pivot: 5

34 185 7 12

Page 169: SearchingSorting.ppt

169

Quick Sort 12 34 18 5 7

Pivot: 5

34 18 5 7 12

7 12

Page 170: SearchingSorting.ppt

170

Quick Sort 12 34 18 5 7

Pivot: 5

34 18 5 7 12

5 7 12

Page 171: SearchingSorting.ppt

171

Quick Sort 12 34 18 5 7

Pivot: 34

34 18 34 18 12

5 7 12 18

Page 172: SearchingSorting.ppt

172

Quick Sort 12 34 18 5 7

Pivot: 34

34 18 5 7 12

5 7 12 3418

Page 173: SearchingSorting.ppt

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 ); }}}}

Page 174: SearchingSorting.ppt

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;}}

Page 175: SearchingSorting.ppt

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

Page 176: SearchingSorting.ppt

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

Page 177: SearchingSorting.ppt

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!)

Page 178: SearchingSorting.ppt

178

Graph of O(2n), O(n3), O(n2),O(n logn), O(n), O(log n)

Page 179: SearchingSorting.ppt

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

Page 180: SearchingSorting.ppt

180

Bucket Sort

5 9 3 7 2 1

1 2 3 4 5

6 7 8 9 10

Page 181: SearchingSorting.ppt

181

Bucket Sort

5 9 3 7 2 1

1 2 3 4 5

6 7 8 9 10

Page 182: SearchingSorting.ppt

182

Bucket Sort

5 9 3 7 2 1

1 2 3 455

6 7 8 9 10

Page 183: SearchingSorting.ppt

183

Bucket Sort

5 9 3 7 2 1

1 2 3 455

6 7 8 9 10

Page 184: SearchingSorting.ppt

184

Bucket Sort

5 9 3 7 2 1

1 2 3 455

6 7 899 10

Page 185: SearchingSorting.ppt

185

Bucket Sort

5 9 3 7 2 1

1 2 3 455

6 7 899 10

Page 186: SearchingSorting.ppt

186

Bucket Sort

5 9 3 7 2 1

1 233 4

55

6 7 899 10

Page 187: SearchingSorting.ppt

187

Bucket Sort

5 9 3 7 2 1

1 233 4

55

6 7 899 10

Page 188: SearchingSorting.ppt

188

Bucket Sort

5 9 3 7 2 1

1 233 4

55

677 8

99 10

Page 189: SearchingSorting.ppt

189

Bucket Sort

5 9 3 7 2 1

1 233 4

55

677 8

99 10

Page 190: SearchingSorting.ppt

190

Bucket Sort

5 9 3 7 2 1

122

33 4

55

677 8

99 10

Page 191: SearchingSorting.ppt

191

Bucket Sort

5 9 3 7 2 1

122

33 4

55

677 8

99 10

Page 192: SearchingSorting.ppt

192

Bucket Sort

5 9 3 7 2 1

11

22

33 4

55

677 8

99 10

Page 193: SearchingSorting.ppt

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

Page 194: SearchingSorting.ppt

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?

Page 195: SearchingSorting.ppt

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!!

Page 196: SearchingSorting.ppt

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

Page 197: SearchingSorting.ppt

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

Page 198: SearchingSorting.ppt

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?

Page 199: SearchingSorting.ppt

199

Binary Search

Number of GuessesNumber of Guesses Possible NumbersPossible Numbers00 10010011 505022 252533 131344 7755 4466 2277 11

Page 200: SearchingSorting.ppt

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

Page 201: SearchingSorting.ppt

201

Playing the number guessing game

nHigh 100nHigh 100

nGuess 50nGuess 50

nLow 1nLow 1

Page 202: SearchingSorting.ppt

202

Playing the number guessing game

nHigh 100nHigh 100

nGuess 50nGuess 50

nLow 1nLow 1

Too HighToo High

Page 203: SearchingSorting.ppt

203

Playing the number guessing game

nHigh 100 49nHigh 100 49

nGuess 50 25nGuess 50 25

nLow 1nLow 1

Page 204: SearchingSorting.ppt

204

Playing the number guessing game

nHigh 100 49nHigh 100 49

nGuess 50 25nGuess 50 25

nLow 1nLow 1

Too LowToo Low

Page 205: SearchingSorting.ppt

205

Playing the number guessing game

nHigh 100 49nHigh 100 49

nGuess 50 25 37nGuess 50 25 37

nLow 1 26nLow 1 26

Page 206: SearchingSorting.ppt

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

Page 207: SearchingSorting.ppt

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

Page 208: SearchingSorting.ppt

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?

Page 209: SearchingSorting.ppt

209

{0,3,4,7,11,13,15,17,21,33}

nLow 0 nGuess 4 nHigh 9nLow 0 nGuess 4 nHigh 9

Page 210: SearchingSorting.ppt

210

{0,3,4,7,11,13,15,17,21,33}

nLow 0 nGuess 1 nHigh 3nLow 0 nGuess 1 nHigh 3

Page 211: SearchingSorting.ppt

211

{0,3,4,7,11,13,15,17,21,33}

nLow 2 nGuess 2 nHigh 3nLow 2 nGuess 2 nHigh 3

Page 212: SearchingSorting.ppt

212

{0,3,4,7,11,13,15,17,21,33}

nLow 3 nGuess 3 nHigh 3nLow 3 nGuess 3 nHigh 3

Page 213: SearchingSorting.ppt

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"

Page 214: SearchingSorting.ppt

214

radix sort 0123456789

An array of 10Linked Lists

Page 215: SearchingSorting.ppt

215

radix sort 0123456789

The firstpass willarrangethe numbersin order ofthe onesdigit

Page 216: SearchingSorting.ppt

216

radix sort0123456789

Page 217: SearchingSorting.ppt

217

radix sort0123456789

Page 218: SearchingSorting.ppt

218

radix sort0123456789

Page 219: SearchingSorting.ppt

219

radix sort0123456789

Page 220: SearchingSorting.ppt

220

radix sort0123456789

Page 221: SearchingSorting.ppt

221

radix sort0123456789

Now, we'll repeat the process usingthe tens digit

Page 222: SearchingSorting.ppt

222

radix sort0123456789

0123456789

Page 223: SearchingSorting.ppt

223

radix sort0123456789

0123456789

Page 224: SearchingSorting.ppt

224

radix sort0123456789

0123456789

Page 225: SearchingSorting.ppt

225

radix sort0123456789

0123456789

Page 226: SearchingSorting.ppt

226

radix sort0123456789

0123456789

Page 227: SearchingSorting.ppt

227

radix sort0123456789

0123456789

Once again,this time using the hundreds digit

Page 228: SearchingSorting.ppt

228

radix sort

0123456789

0123456789

Page 229: SearchingSorting.ppt

229

radix sort

0123456789

0123456789

Page 230: SearchingSorting.ppt

230

radix sort

0123456789

0123456789

Page 231: SearchingSorting.ppt

231

radix sort

0123456789

0123456789

Page 232: SearchingSorting.ppt

232

radix sort

0123456789

0123456789

Page 233: SearchingSorting.ppt

233

radix sort

0123456789

After threepasses, onefor eachdigit, the numbers arein order

Page 234: SearchingSorting.ppt

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?

Page 235: SearchingSorting.ppt

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

Page 236: SearchingSorting.ppt

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.

Page 237: SearchingSorting.ppt

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"

Page 238: SearchingSorting.ppt

238

hash tables

0

1

2

3

4

Page 239: SearchingSorting.ppt

239

hash tables

0

1

2

3

4

Page 240: SearchingSorting.ppt

240

hash tables

0

1

2

3

4

Page 241: SearchingSorting.ppt

241

hash tables

0

1

2

3

4

Page 242: SearchingSorting.ppt

242

hash tables

0

1

2

3

4

Page 243: SearchingSorting.ppt

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.

Page 244: SearchingSorting.ppt

244

hash tables

0

1

2

3

4

Page 245: SearchingSorting.ppt

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.

Page 246: SearchingSorting.ppt

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.

Page 247: SearchingSorting.ppt

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

Page 248: SearchingSorting.ppt

248

hash function: add the digits up and then % 5

0

1

2

3

4

Page 249: SearchingSorting.ppt

249

hash function: sin(number) *2.5 + 2.5

0

1

2

3

4

Page 250: SearchingSorting.ppt

250

hash function: .number * 6

0

1

2

3

4

Page 251: SearchingSorting.ppt

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

Page 252: SearchingSorting.ppt

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