Sunteți pe pagina 1din 252

What is the output of this code fragment?

int nCount = 0; int nN = 10; for(int nX = 0; nX < nN; nX++) for(int nY = 0; nY <nN; nY++) nCount++; System.out.println(nCount); What would the output be if nN was 5? or 20? What effect does doubling nN have on nCount?

O(n2)
The nested for loops "are multiplied" The efficiency of this type of algorithm is O(n2)

for(int nX = 0; nX < nN; nX++) for(int nY = 0; nY <nN; nY++)

Sorting
Sorting is the process of putting something (e.g. an array) in order There are many ways (algorithms) to do this There is no, one single best way Some ways, though, are much worse than others Computer Scientists measure the efficiency of an algorithm in Big O notation

O(n2) Sorting Algorithms


Characterized

by nested loops

Bubble Sort Selection Sort Insertion Sort Doubling the data Quadruples (4 x) the work
4

Bubble Sort
Let's say we want to sort the following numbers: 12 34 18 5 7 We'll start by comparing the first two

12 34 18 5 7

Bubble Sort

They are in order, so well go to the next two 12 34 18 5 7

Bubble Sort

They aren't in order, so we'll swap them 12 18 34 5 7

Bubble Sort

and so on. . . 12 18 34 5 7

Bubble Sort

and so on. . . 12 18 5 34 7

Bubble Sort

and so on. . . 12 18 5 34 7

10

Bubble Sort

and so on. . . 12 18 5 7 34

11

Bubble Sort
finally, after one pass through the numbers, the last number is in the correct position We'll repeat the process, but we'll stop one position sooner.

12 18 5 7 34

12

Bubble Sort
12 18 5 7 34

13

Bubble Sort
12 18 5 7 34

14

Bubble Sort
12 5 18 7 34

15

Bubble Sort
12 5 18 7 34

16

Bubble Sort
12 5 7 18 34

17

Bubble Sort
12 5 7 18 34

18

Bubble Sort
12 5 7 18 34

19

Bubble Sort
5 12 7 18 34

20

Bubble Sort
5 12 7 18 34

21

Bubble Sort
5 7 12 18 34

22

Bubble Sort
5 7 12 18 34

23

Bubble Sort
5 7 12 18 34

24

Bubble Sort
5 7 12 18 34

25

Bubble Sort

Finally, the numbers are sorted 5 7 12 18 34

26

Coding BubbleSort
public static void bubbleSort(int[] list) { for (int outer = 0; outer < list.length - 1; outer++) for (int inner = 0; inner < list.length-outer-1; inner++)

We'll start with some nested for loops that take us through the list of numbers We want to stop one position earlier after each pass

list.length-outer-1
27

Coding BubbleSort
public static void bubbleSort(int[] list) { for (int outer = 0; outer < list.length - 1; outer++) { for (int inner = 0; inner < list.length-outer-1; inner++) { if (list[inner] > list[inner + 1]) { //swap list[inner] & list[inner+1] int temp = list[inner]; list[inner] = list[inner + 1]; list[inner + 1] = temp; } } } }
28

Counting steps in Bubble Sort


We will be counting the "steps" each algorithm takes to complete the sorting For each step, we will increment the step member of the Sorts class public class Sorts { private long steps; A step is: 1. An assignment 2. A comparison 3. Arithmetic (usually ++)

29

Counting steps in Bubble Sort


public static void bubbleSort(int[] list) { steps++; //outer = 0 for (int outer = 0; outer < list.length - 1; outer++) { steps++; // outer < list.length 1 steps++; // outer++ steps++; // int inner = 0 for (int inner = 0; inner < list.length-outer-1; inner++) { steps++; // inner < list.length-outer-1 steps++; // inner++ steps++; // list[inner] > list[inner + 1] if (list[inner] > list[inner + 1]) { //swap list[inner] & list[inner+1] int temp = list[inner]; list[inner] = list[inner + 1]; list[inner + 1] = temp; steps+=3; //3 assignments }
30

What is the output?


public class SwapMyst { public static void main(String[] args) { mystery1(3,4); mystery2(3,4); } public static void mystery1(int nNum1, int nNum2){ nNum1 = nNum2; nNum2 = nNum1; System.out.println(nNum1 + ", " + nNum2); } public static void mystery2(int nNum1, int nNum2){ int nTemp = nNum1; nNum1 = nNum2; nNum2 = nTemp; System.out.println(nNum1 + ", " + nNum2); } 31 }

Gnome Sort (from Wikipedia)


The simplest O(n2) sort? Supposedly how garden gnomes sort flower pots at night Start with the first two

12 34 18 5 7

32

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

12 34 18 5 7

33

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

12 34 18 5 7

34

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

12 18 34 5 7

35

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

12 18 34 5 7

36

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

12 18 34 5 7

37

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

12 18 34 5 7

38

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

12 18 5 34 7

39

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

12 18 5 34 7

40

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

12 5 18 34 7

41

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

12 5 18 34 7

42

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

5 12 18 34 7

43

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

5 12 18 34 7

44

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

5 12 18 34 7

45

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

5 12 18 34 7

46

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

5 12 18 7 34

47

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

5 12 18 7 34

48

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

5 12 7 18 34

49

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

5 12 7 18 34

50

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

5 7 12 18 34

51

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

5 7 12 18 34

52

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

5 7 12 18 34

53

Gnome Sort
If the numbers are out of order, swap them and move to the left If they are in order, leave them alone and move right

5 7 12 18 34

54

Gnome Sort

When the gnome gets all the way to the right, he's done!

5 7 12 18 34

55

Coding Gnome Sort


void gnomesort(int n, int ar[]) { int i = 0; while (i < n) { //if the numbers are in order if (i == 0 || ar[i-1] <= ar[i]) i++; //move right else //swap { int tmp = ar[i]; ar[i] = ar[i-1]; ar[i-1] = tmp;} i--; //move left }
56

Selection Sort

Like Bubble Sort, but with fewer swaps We'll use a flag to keep track of the position of the smallest integer Then we will do one swap at the end of each pass 12 34 18 5 7 nFlag = 0
57

Selection Sort
12 34 18 5 7 nFlag = 0

58

Selection Sort
12 34 18 5 7 nFlag = 0 3

59

Selection Sort
12 34 18 5 7 nFlag = 3

60

Selection Sort

After one pass through the numbers, we'll swap the position marked by the flag with the first number 12 34 18 5 7 nFlag = 3

61

Selection Sort

After one pass through the numbers, we'll swap the position marked by the flag with the first number 5 34 18 12 7 nFlag = 3

62

Selection Sort

The first number is now sorted, so we'll start the process again from the next position 5 34 18 12 7 nFlag = 1

63

Selection Sort
5 34 18 12 7 nFlag = 2

64

Selection Sort
5 34 18 12 7 nFlag = 3

65

Selection Sort
5 34 18 12 7 nFlag = 4

66

Selection Sort
5 7 18 12 34 nFlag = 4

67

Selection Sort
5 7 18 12 34 nFlag = 2

68

Selection Sort
5 7 18 12 34 nFlag = 3

69

Selection Sort
5 7 12 18 34 nFlag = 3

70

Selection Sort
5 7 12 18 34 nFlag = 3

71

Selection Sort
5 7 12 18 34 nFlag = 3

72

Coding Selection Sort


void selectionSort(int[] list) { int min, temp; for (int outer = 0; outer < list.length - 1; outer++) { min = outer; for (int inner = outer + 1; inner < list.length; inner++) { if (list[inner] < list[min]) { min = inner; } } //swap list[outer] & list[flag] temp = list[outer]; list[outer] = list[min]; list[min] = temp; }
73

A Common Confusion
min = outer; for (int inner = outer + 1; inner < list.length; inner++) { if (list[inner] < list[min]) { min = inner; } } //swap list[outer] & list[flag] temp = list[outer]; list[outer] = list[min]; list[min] = temp;

The Flag min, stores the index of the smallest number Don't confuse min with list[min] That's like confusing the apartment number with the person who lives in that apartment

74

What is the output?


public class SelectionExample { public static void main(String[] args) { int [] naNums = {7,-2,5,-11,67}; int nMyst = mystery(naNums); System.out.println(nMyst); System.out.println(naNums[nMyst]); } public static int mystery(int [] naInts) { int nFlag = 0; for(int nI = 1; nI < naInts.length; nI++) { if(naInts[nI] < naInts[nFlag]) { System.out.println("Flag changed to " + nI); nFlag = nI; } } return nFlag; } } 75

What is the output? How would it change if the code in red was 2?
String [] saPets = {"cat", "bird","elephant","ant","dog"}; //bubble sort the list of pets for(int nI = 0; nI < saPets.length; nI++) for(int nJ = 0; nJ < saPets.length - nI -1; nJ++) { if(saPets[nJ].compareTo(saPets[nJ+1]) > 0) // if saPets[nJ + 1] comes before // saPets[nJ] alphabetically { String sTemp = saPets[nJ]; saPets[nJ] = saPets[nJ + 1]; saPets[nJ + 1] = sTemp; } } for(int nK = 0; nK < saPets.length; nK++) System.out.println(saPets[nK]);
76

Insertion Sort

Insertion Sort starts by sorting the first part of the list The first unsorted number is inserted into the sorted part of the list 12 34 18 5 7

77

Insertion Sort

A single number by itself is sorted, so we will start with the first number as the sorted part of the list We'll store the first number after the sorted section in a temporary variable 12 __ 18 5 7 nTemp = 34
78

Insertion Sort

While the numbers in the sorted section are larger than the number in nTemp, we will move them up one place 12 __ 18 5 7 nTemp = 34

79

Insertion Sort

Then we will move the number in nTemp into the empty position, and the next number in nTemp 12 34 __ 5 7 nTemp = 18

80

Insertion Sort
12 __ 34 5 7 nTemp = 18

81

Insertion Sort
12 18 34 __ 7 nTemp = 5

82

Insertion Sort
12 18 __ 34 7 nTemp = 5

83

Insertion Sort
12 __ 18 34 7 nTemp = 5

84

Insertion Sort
__ 12 18 34 7 nTemp = 5

85

Insertion Sort
5 12 18 34 __ nTemp = 7

86

Insertion Sort
5 12 18 __ 34 nTemp = 7

87

Insertion Sort
5 12 __ 18 34 nTemp = 7

88

Insertion Sort
5 __ 12 18 34 nTemp = 7

89

Insertion Sort
5 7 12 18 34 nTemp = 7

90

Coding Insertion Sort


void insertionSort(int[] list) { for (int outer = 1; outer < list.length; outer++) { int position = outer; int key = list[position]; // Shift larger values to the right while (position > 0 && list[position - 1] > key) { list[position] = list[position - 1]; position--; } list[position] = key; } }
91

Big O notation
Expression
O(1) O(log n) O(n) O(n log n) O(n2) O(2n)

Explanation
Constant Logarithmic Linear Linear-Logarithmic Quadratic Exponential

Effect of Doubling Data has no effect


increases by 1 doubles >2x & <4x quadruples
If data increases by one, work doubles
92

Big O notation
Expression
O(1) O(log n)

Example
Array storage/retrieval Binary Search

O(n)
O(n log n) O(n2) O(2n)

Linear search, Radix Sort


Merge Sort, Quick Sort Bubble, Selection, Insertion sort Towers of Hanoi
93

O(n log n) Sorting Algorithms


Characterized

by recursive "Divide and Conquer" strategies

Merge Sort Quick Sort


Doubling

the Data increases the work more than 2x but less than 4x
94

Here's a clever idea


With an O(n2) sort, doubling the number of numbers quadruples the work So let's say 5 numbers takes 40 steps to sort How many steps would 10 numbers take?

95

Here's a clever idea


With an O(n2) sort, doubling the number of numbers quadruples the work So let's say 5 numbers takes 40 steps to sort Sorting 10 numbers would take 160 steps What if we sorted those 10 numbers 5 at a time? Would that save steps?

96

Here's a clever idea


{6,2,1,3,9,8,5,7,4,0} becomes {6,2,1,3,9} {8,5,7,4,0} How many steps to sort each group of 5?

97

Here's a clever idea


{6,2,1,3,9,8,5,7,4,0} becomes {6,2,1,3,9} {8,5,7,4,0} and with 80 steps (40 for each) I have {1,2,3,6,9} {0,4,5,7,8} So if I can figure out how to combine them into one sorted group with less than another 80 steps I'll be ahead
98

Merging
Merging is combining two smaller sorted groups into one larger sorted group {1,2,3,6,9} {0,4,5,7,8} It only takes one pass (loop) through the numbers, so it's faster than nested loop sorting

99

Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} { } I'll start with the first two numbers, and copy the smaller

100

Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} {0 } I'll I'll move to the next number, and copy the smaller

101

Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} {0 ,1} an so on. . .

102

Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} {0 ,1,2} an so on. . .

103

Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} {0 ,1,2,3} an so on. . .

104

Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} {0 ,1,2,3,4} an so on. . .

105

Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} {0 ,1,2,3,4,5} an so on. . .

106

Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} {0 ,1,2,3,4,5,6} an so on. . .

107

Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} {0 ,1,2,3,4,5,6,7} an so on. . .

108

Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {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 copy the rest over

109

Merging
I'll set up an empty place to hold the final sorted numbers {1,2,3,6,9} {0,4,5,7,8} {0 ,1,2,3,4,5,6,7,8,9} we I get to the end of one group, I can just copy the rest over

110

Using a merge function

First I'll rewrite my sort so that it only sorts part of the numbers, not all of them

public void selectionSort( int[] list, int first, int last) //sorts list from positions first to last //code not shown

Then, I'll write a merge function that merges the numbers from first to mid, and mid + 1 to last

void merge( int[] a, int first, int mid, int last) //code not shown

111

How it could work


int list = {6,2,1,3,9,8,5,7,4,0}; selectionSort(list,0,4);

112

How it could work


int list = {6,2,1,3,9,8,5,7,4,0}; selectionSort(list,0,4); //sorts {6,2,1,3,9} to {1,2,3,6,9} //list is now {1,2,3,6,9,8,5,7,4,0};

113

How it could work


int list = {6,2,1,3,9,8,5,7,4,0}; selectionSort(list,0,4); //sorts {6,2,1,3,9} to {1,2,3,6,9} //list is now {1,2,3,6,9,8,5,7,4,0}; selectionSort(list,5,9);

114

How it could work


int list = {6,2,1,3,9,8,5,7,4,0}; selectionSort(list,0,4); //sorts {6,2,1,3,9} to {1,2,3,6,9} //list is now {1,2,3,6,9,8,5,7,4,0}; selectionSort(list,5,9); //sorts {8,5,7,4,0} to {0,4,5,7,8} //list is now {1,2,3,6,9,0,4,5,7,8};

115

How it could work


int list = {6,2,1,3,9,8,5,7,4,0}; selectionSort(list,0,4); //sorts {6,2,1,3,9} to {1,2,3,6,9} //list is now {1,2,3,6,9,8,5,7,4,0}; selectionSort(list,5,9); //sorts {8,5,7,4,0} to {0,4,5,7,8} //list is now {1,2,3,6,9,0,4,5,7,8}; merge(list,0,4,9);

116

How it could work


int list = {6,2,1,3,9,8,5,7,4,0}; selectionSort(list,0,4); //sorts {6,2,1,3,9} to {1,2,3,6,9} //list is now {1,2,3,6,9,8,5,7,4,0}; selectionSort(list,5,9); //sorts {8,5,7,4,0} to {0,4,5,7,8} //list is now {1,2,3,6,9,0,4,5,7,8}; merge(list,0,4,9); //merges {1,2,3,6,9} //and {0,4,5,7,8} //list is now {0,1,2,3,4,5,6,7,8,9}
117

What is the output?


int[] naArray = {4,7,-2,4,1,13,8}; int nFirst = 0; int nLast = naArray.length - 1; int nMid = (nFirst + nLast)/2; //nMid is last position in first part of array selectionSort(naArray,nFirst,nMid); print(naArray); selectionSort(naArray,nMid + 1,nLast); print(naArray); merge(naArray,nFirst,nMid,nLast); print(naArray); void print(int[] naList) { for(int nI = 0; nI < naList.length; nI++) System.out.print(naList[nI] + ", "); System.out.println(); } public void selectionSort(int[] list, int first, int last) //sorts list from positions first to last //code not shown void merge(int[] a, int first, int mid, int last) //code not shown
118

How small should the groups be?

If dividing the numbers to be sorted into 2 groups saves steps, would 4 be better?

119

How small should the groups be?


If dividing the numbers to be sorted into 2 groups saves steps, would 4 be better? YES!

120

How small should the groups be?

If dividing the numbers to be sorted into 4 groups saves steps, would 8 be better?

121

How small should the groups be?


If dividing the numbers to be sorted into 4 groups saves steps, would 8 be better? YES!

122

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.

123

Merge Sort

12 34 18 5 7
12 34 18 5 7

124

Merge Sort
12

12 34 18 5 7
12 34 34 18 5 7 18 57

125

Merge Sort
12

12 34 18 5 7
12 34 34 18 5 7 18 5 57 7

126

Merge Sort
12

12 34 18 5 7
12 34 34 18 5 7 18 5 57 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 back together


127

Merge Sort
12

12 34 18 5 7
12 34 34 18 5 7 18 5 5 7 57 7

12 34

128

Merge Sort
12

12 34 18 5 7
12 34 34 18 5 7 18 5 5 7 5 7 18 57 7

12 34

129

Merge Sort
12

12 34 18 5 7
12 34 34 18 5 7 18 5 5 7 5 7 18 57 7

12 34

5 7 12 18 34
130

Coding Merge Sort

Merge Sort uses two methods:

public void mergeSort(int[] a, int first, int last)


private void merge(int[] a, int first, int mid, int last)

Writing mergeSort is easy merge is a little tougher


131

Coding Merge Sort


public void mergeSort(int[] a, int first, int last) { if( { ?? )

mergeSort(a, ?, ?); mergeSort(a, ?, ?);


} merge(a, ?, ? , ?);

}
132

Javabat mergeTwo
http://www.javabat.com/prob?id=AP1.merg eTwo Start with two arrays of strings, A and B, each with its elements in alphabetical order and without duplicates. Return a new array containing the first N elements from the two arrays. The result array should be in alphabetical order and without duplicates. A and B will both have a length which is N or more.

133

mergeTwo(String[] a, String[] b, int n)

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

Three arguments: String[] a String[] b int n

134

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

The first thing I need is a new String array of the right size String saC[]= new String[3];

135

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

The first thing I need is a new String array of the right size String saC[]= new String[3];

I'll also need three index variables, one for each array int nA=0,nB=0,nC=0;

136

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

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

137

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

"a" , ?? , ?? }

138

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

"a" , "b" , ?? }

139

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

3)

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

and stop!

140

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

?? , ?? , ?? }

141

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

"a" , ?? , ?? }

142

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

"a" , "c" , ?? }
that we incremented the indexes in both arrays!

Notice

143

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

3)

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

and stop!

144

comparing Strings

< and > don't work

145

comparing Strings

use compareTo instead

public String[] mergeTwo( String[] a, String[] b, int n) { //lots of java if(a[nA].compareTo(b[nB]) < 0)

146

comparing Strings

< and > don't work

if(a[nA].compareTo(b[nB]) < 0)

If the element in a at position nA comes before b[nB] alphabetically

147

comparing Strings

< and > don't work

if(a[nA].compareTo(b[nB]) > 0)

If the element in a at position nA comes after b[nB] alphabetically

148

comparing Strings

< and > don't work

if(a[nA].compareTo(b[nB]) == 0)

If the element in a at position nA is the same as b[nB] alphabetically

149

Coding Merge
private void merge(int[] naArray, int nFirst, int nMid, int nLast)

nFirst is the position of the first element in the first (sorted) section of the array nMid is the position of the the last element in the first (sorted) section of the array nLast is the position of the last element in the second (sorted) section of the array

150

Coding Merge
{17,12,5,8,9,3,6,7,2,1} this part of the array, nFirst is 2 nMid is 4 nLast is 7
In

151

Coding Merge
{17,12,5,8,9,3,6,7,2,1}
We'll

start with a temporary array big enough to hold the two sorted sections

int[] naTemp = new int[??];


152

Coding Merge
{17,12,5,8,9,3,6,7,2,1}

Then, we'll compare the numbers at nFirst and nMid + 1, and copy the smaller one to naTemp

153

Coding Merge
{17,12,5,8,9,3,6,7,2,1}
In this case nMid + 1 had the smaller number We'll move to the next value after nMid + 1 after copying it's value to naTemp

154

3 5

Coding Merge
{17,12,5,8,9,3,6,7,2,1}

155

3 5 6

Coding Merge
{17,12,5,8,9,3,6,7,2,1}

156

3 5 6 7

Coding Merge
{17,12,5,8,9,3,6,7,2,1}

157

3 5 6 7 8 9

Coding Merge
{17,12,5,8,9,3,6,7,2,1}

Once we get to the end of one section, we can copy the rest into the temp array

158

"pseudo-code" for merge


declare and initialize nTempIndex to first; declare and initialize nIndex1 to first, nIndex2 to mid + 1 declare naTemp as copy of the array a while nIndex1 <=mid and nIndex2<=last if a[nIndex1] less than a[nIndex2] set naTemp[nTempIndex] to a[nIndex1] increment nIndex1 and nTempIndex else set naTemp[nTempIndex] to a[nIndex2] increment nIndex2 and nTempIndex++ while nIndex1<=mid set naTemp[nTempIndex] to a[nIndex1] increment nTempIndex and nIndex1 while nIndex2<=last set naTemp[nTempIndex] to a[nIndex2] increment nTempIndex and nIndex2 copy naTemp to the array a

159

Copying an array
int[] na1 = {1,2,3,4}; int[] na2 = na1; na2 is NOT a copy of na1 na2 is an ALIAS of na1 {1,2,3,4}
160

Copying an array
int[] na1 = {1,2,3,4}; int[] na2 = new int[na1.length];
for(int nI = 0; nI < na1.length; nI++)

na2[nI] = na1[nI]; Now na2 is a copy of na1

{1,2,3,4} {1,2,3,4}
161

Efficiency in MergeSort

The temporary array in merge sort only needs to hold the values we are currently sorting Remember, MergeSort recursively breaks down the array into smaller and smaller sections You can significantly reduce steps by only copying the values you are working on (from nFirst to nLast) rather than the entire array na2[nI] = na1[nI];
162

for(int nI = nFirst; nI <= nLast; nI++)

Quick Sort

12 34 18 5 7

Quick Sort is a two step process: 1. A helper function partitions the numbers into two groups 2. Quick Sort is recursively called to sort the two partitions

163

Quick Sort
Pivot: 12

12 34 18 5 7

164

Quick Sort
Pivot: 12

12 34 18 5 7

5 7

34 18

165

Quick Sort
Pivot: 12

12 34 18 5 7

5 7

34 18

166

Quick Sort
Pivot: 12

12 34 18 5 7

5 7

12

34 18

167

Quick Sort
Pivot: 5

12 34 18 5 7

5 7

12

34 18

168

Quick Sort
Pivot: 5

12 34 18 5 7

5 7

12

34 18

12

169

Quick Sort
Pivot: 5

12 34 18 5 7

5 7

12

34 18

12

170

Quick Sort
Pivot: 34

12 34 18 5 7

34 18

12

34 18

12

18

171

Quick Sort
Pivot: 34

12 34 18 5 7

5 7

12

34 18

12

18

34

172

Coding Quick Sort


void quicksort(int []naA, int nLow, int nHigh)

{ int nPivot; if ( nHigh > nLow ) { nPivot = partition( naA, nLow, nHigh ); quicksort(naA, nLow, nPivot-1 ); quicksort(naA, nPivot+1, nHigh ); } }

173

Coding Quick Sort


int partition(int []naA, int nLow, int nHigh) { int nPos = nLow; int nPivot = naA[nLow]; while(nLow < nHigh) { while (nLow < naA.length && naA[nLow] <= nPivot) nLow++; while (naA[nHigh] > nPivot) nHigh--; if (nLow < nHigh) //swap { int nTemp = naA[nLow]; naA[nLow] = naA[nHigh]; naA[nHigh] = nTemp; }

} while(nPos < nHigh) { naA[nPos]=naA[nPos+1]; nPos++; } naA[nHigh] = nPivot; return nHigh;

174

What would be the Big O expression for "guess and check" sort?
In "Guess and Check" sort, we randomly arrange an array of numbers, and check to see if it's sorted If it's not sorted, we randomly arrange it again

175

What would be the Big O expression for "guess and check" sort?

Let's say the array had two numbers 13 & 17: how many ways could they be arranged? Let's increase the size of the data by one, adding the number 7. How many ways could they be arranged now?

13, 17 17, 13

7, 13, 17 7, 17, 13 13, 7, 17 13, 17, 7 17, 13, 7 17, 7, 13

176

What would be the Big O expression for "guess and check" sort?
Increasing the size of the data by just one more than doubles the number of steps! This is an example of O(n!)

177

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

178

Here's a sort that's faster than Quick Sort


It's called "Bucket Sort" Let's say I want to sort the numbers {5,9,3,7,2,1} I need to know the largest and smallest possible values: In this case I know the numbers are all between 1 and 10 I then make "buckets" to hold each of the possible values

179

Bucket Sort
5 9 3 7 2 1

10

180

Bucket Sort
5 9 3 7 2 1

10

181

Bucket Sort
5 9 3 7 2 1
5 5

10

182

Bucket Sort
5 9 3 7 2 1
5 5

10

183

Bucket Sort
5 9 3 7 2 1
5 5

4
9 9

10

184

Bucket Sort
5 9 3 7 2 1
5 5

4
9 9

10

185

Bucket Sort
5 9 3 7 2 1
3 3 5 5

4
9 9

10

186

Bucket Sort
5 9 3 7 2 1
3 3 5 5

4
9 9

10

187

Bucket Sort
5 9 3 7 2 1
3 3 5 5

2
7 7

4
9 9

10

188

Bucket Sort
5 9 3 7 2 1
3 3 5 5

2
7 7

4
9 9

10

189

Bucket Sort
5 9 3 7 2 1
2 2 7 7 3 3 5 5

4
9 9

10

190

Bucket Sort
5 9 3 7 2 1
2 2 7 7 3 3 5 5

4
9 9

10

191

Bucket Sort
5 9 3 7 2 1
1 1 2 2 7 7 3 3 5 5

4
9 9

10

192

Bucket Sort
5 9 3 7 2 1
1 1 2 2 7 7 3 3 5 5

4
9 9

10

Now I can just go through the buckets in order


193

Bucket Sort
I only need to make 2 passes through the numbers, once to put them in the buckets and once to take them out For 6 numbers, that's 12 steps How many steps for 12 numbers? What's the Big-O Notation for Bucket Sort? Why isn't Bucket Sort used more commonly than Quick Sort?

194

Javabat: Scores increasing


http://www.javabat.com/prob?id=AP1.score sIncreasing In some ways, it's kind of like sorting: Go through the numbers two at a time If you find two that are out of order, instead of swapping, just return false!

195

Linear Search
Let's say you had an array of 100 numbers in no particular order and you wanted to know if the number 17 was in the array You would have to look at the first number, then the second, and so on until you found 17 or reached the end of the array That's linear search, an O(n) algorithm

196

Binary Search
I'm thinking of a number between 1 and 100 I want you to try and guess it I'll tell you if your guess is too high, too low or if you guessed correctly What would be the first number you would guess? If your answer is 50, you know how Binary Search works

197

Binary Search

If someone correctly uses the Binary Search strategy, how many guesses will they need at most to guess the number?

198

Binary Search
Number of Guesses 0 1 2 3 4 5 6 7 Possible Numbers 100 50 25 13 7 4 2 1
199

Binary Search
Anyone should be able to guess the number in at most 7 guesses How many guesses would we need if we doubled the possibilities to 200 (a number between 1 and 200)? 8, just one more guess Binary Search is an O(log n) algorithm

200

Playing the number guessing game


nHigh 100 nGuess 50 nLow 1

201

Playing the number guessing game


nHigh 100 nGuess 50 nLow 1

Too High

202

Playing the number guessing game


nHigh 100 49 nGuess 50 25 nLow 1

203

Playing the number guessing game


nHigh 100 49 nGuess 50 25 nLow 1

Too Low

204

Playing the number guessing game


nHigh 100 49 nGuess 50 25 37 nLow 1 26

205

Playing the number guessing game


nHigh 100 49 nGuess 50 25 37 nLow 1 26

Too High

206

Playing the number guessing game


nHigh 100 49 36 nGuess 50 25 37 31 nLow 1 26

And so on. . . . Until either we guess the number or there are no more numbers to guess
207

Practice Quiz Questions


Using the Binary Search strategy of the "number guessing game", what elements of the following array would be visited if we were searching for 7? {0,3,4,7,11,13,15,17,21,33} Is binary search always faster than sequential search?

208

{0,3,4,7,11,13,15,17,21,33}
nLow 0 nGuess 4 nHigh 9

209

{0,3,4,7,11,13,15,17,21,33}
nLow 0 nGuess 1 nHigh 3

210

{0,3,4,7,11,13,15,17,21,33}
nLow 2 nGuess 2 nHigh 3

211

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

212

Radix Sort
"Bucket Sort" is very efficient in terms of stepsO(n) But it is very inefficient in terms of memory And it cannot handle duplicate values By changing the "buckets" to linked lists, memory can be handled efficiently as well And any number of items can be stored in a linked list, not just one like a "bucket"

213

radix sort

0 1 2 3 4 5 6 7 8 9

An array of 10 Linked Lists

214

radix

0 sort 1 2 3 4 The first pass will 5 arrange 6 the numbers 7 in order of 8 the ones 9 digit
215

radix sort

0 1 2 3 4 5 6 7 8 9
216

radix sort

0 1 2 3 4 5 6 7 8 9
217

radix sort

0 1 2 3 4 5 6 7 8 9
218

radix sort

0 1 2 3 4 5 6 7 8 9
219

radix sort

0 1 2 3 4 5 6 7 8 9
220

radix sort
Now, we'll repeat the process using the tens digit

0 1 2 3 4 5 6 7 8 9
221

radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
222

radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
223

radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
224

radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
225

radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
226

radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 Once again, this time using the hundreds digit

227

radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
228

radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
229

radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
230

radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
231

radix sort
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
232

radix sort
After three passes, one for each digit, the numbers are in order
0 1 2 3 4 5 6 7 8 9
233

Radix Sort
Three passes, one for each digit, sorted the 5 numbers Basically, that's 15 steps How many steps for 10 numbers with a maximum of 3 digits?

234

Radix Sort
O(n) Faster than QuickSort, MergeSort and other O(n log n) algorithms Less efficient use of memory than other sorting algorithms, though

235

hash tables (AB only)

A hash table is another variation of the bucket sort: Elements are arranged, but not sorted or ordered in a way you would expect Unlike radix sort, we only make one pass through the data Not really a sorting algorithm, but a data structure that can be used to store and retrieve items.
236

hash tables

Let's say I have five numbers, each of which is between 0 and 400: 7 266 399 12 125 I'll make an array of 5 linked liststhe hash table I'll take the number % 5 and use that to decide which of the 5 lists the number will be assigned I'm using % 5 because there are 5 numbers to be sortedto 5 lists In this example, % 5 is the "hash function"
237

hash tables
0
1 2

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

0
1 2

3
4
243

hash tables
0
1 2

3
4
244

Once the table is complete, it's quick and easy to retrieve an item
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.

0
1 2

3
4
245

Once the table is complete, it's quick and easy to retrieve an item
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.

0
1 2

3
4
246

hash tables
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.25 Ideally, 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

0
1 2

3
4
247

hash function: add the digits up and then % 5

0
1 2

3
4
248

hash function: sin(number) *2.5 + 2.5


0
1 2

3
4
249

hash function: .number * 6


0
1 2

3
4
250

hash tables
If there are no collisions, putting a single item in the Hash table is O(1), putting all the items in the Hash Table is O(n) If there are no collisions, searching for a single item is O(1) Just use the "hash function" to find where the number would be stored

251

hashCode()
You don't have to think of your own hashing method Every class inherits hashCode() from Object Uses "Some Formula" we don't know or care about Every Object is associated with an integer value called its hash code equal objects have equal hash codes

252

S-ar putea să vă placă și