searching and sorting gary wong. prerequisite time complexity pseudocode (recursion)

45
Searching and Sorting Gary Wong

Upload: rafe-mosley

Post on 31-Dec-2015

229 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Searching and SortingGary Wong

Page 2: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Prerequisite

• Time complexity• Pseudocode• (Recursion)

Page 3: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Agenda

• Searching– Linear (Sequential)

Search– Binary Search

• Sorting– Bubble Sort– Merge Sort– Quick Sort– Counting Sort

Page 4: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Linear SearchOne by one...

Page 5: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Linear Search

• Check every element in the list, until the target is found

• For example, our target is 38:

i 0 1 2 3 4 5

a[i] 25 14 9 38 77 45

Not found!

Found!

Page 6: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Linear Search

1) Initilize an index variable i2) Compare a[i] with target

• If a[i]==target, found• If a[i]!=target,

• If all have checked already, not found• Otherwise, change i into next index and go to step 2

Page 7: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Linear Search

• Time complexity in worst case?– If N is number of elements,– Time complexity = O(N)

• Advantage?• Disadvantage?

Page 8: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Binary SearchChop by half...

Page 9: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Binary Search

• Given a SORTED list:• (Again, our target is 38)

i 0 1 2 3 4 5

a[i] 9 14 25 38 45 77

L R

Smaller! Larger!Found!

Page 10: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Binary Search

• Why always in the middle, but not other positions, say one-third of list?

1) Initialize boundaries L and R2) While L is still on the left of R

• mid = (L+R)/2• If a[mid]>Target, set R be m-1 and go to step 2• If a[mid]<Target, set L be m+1 and go to step 2• If a[mid]==Target, found

Page 11: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Binary Search

• Time complexity in the worst case?– If N is the number of elements,– Time complexity = O(lg N)– Why?

• Advantage?• Disadvantage?

Page 12: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Example: Three Little Pigs

• HKOI 2006 Final Senior Q1• Given three lists, each with M numbers,

choose one number from each list such that their sum is maximum, but not greater than N.

• Constraints:– M ≤ 3000– Time limit: 1s

Page 13: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Example: Three Little Pigs

• How many possible triples are there?• Why not check them all?• Time complexity?• Expected score = 50

Page 14: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Example: Three Little Pigs

• A simpler question: How would you search for the maximum number in ONE SORTED list such that it is not greater than N?

• Binary search!– With slight modification though– How?

Page 15: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Example: Three Little Pigs

• Say, N=37

i 0 1 2 3 4 5

a[i] 9 14 25 38 45 77

L R

Page 16: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Example: Three Little Pigs

• Let’s go back to original problem• If you have chosen two numbers a1[i] and

a2[j] already, how would you search for the third number?

• Recall: How would you search for the maximum number in ONE SORTED list such that it is not greater than N-a1[i]-a2[j]?

Page 17: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Example: Three Little Pigs

• Overall time complexity?• Expected score = 90•

Page 18: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Example: Three Little Pigs

• Slightly harder: Given TWO lists, each with M numbers, choose one number from each list such that their sum is maximum, but not greater than N.

• Linear search?• Sort one list, then binary search!• Time complexity = O(M lg M)– O(M2) if less efficient sorting algorithm is used

• But, can it be better?

Page 19: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Example: Three Little Pigs

• Why is it so slow if we use linear search?• If a1[i] and a2[j] are chosen, and their sum is

smaller than N:– Will you consider any number in a1 that is smaller

than or equal a1[i]?• If a1[i] and a2[j] are chosen, and their sum is

greater than N:– Will you consider any number in a2 that is greater

than or equal to a2[j]?

Page 20: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Example: Three Little Pigs

• Recall: Why is it so slow if we use linear search?– Because you use it for too many times!

• At which number in each list should you begin the linear search?

• Never look back at those we do not consider!• Time complexity?• Expected score = 100

Page 21: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

What can you learn?

• Improve one ‘dimension’ using binary search• Linear search for a few times can be more

efficient than binary search for many times!– DO NOT underestimate linear search!!!

Page 22: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Points to note

• To use binary search, the list MUST BE SORTED (either ascending or decending)– NEVER make assumptions yourself– Problem setters usually do not sort for you

• Sorting is the bottleneck of efficiency

• But... how to sort?

Page 23: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

How to sort?

• For C++: sort()• Time complexity for sort() is O(N lg N)– which is considered as efficient

• HOWEVER...– Problem setters SELDOM test contestants on pure

usage of efficient sorting algorithms– Special properties of sorting algorithms are

essential in problem-solving• So, pay attention!

Page 24: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Bubble SortSmaller? Float! Larger? Sink!

Page 25: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

• Suppose we need to sort in ascending order...• Repeatedly check adjacent pairs sequentially,

swap if not in correct order• Example:

• The last number is always the largest

Bubble Sort

9 20 1118 4577

Incorrect order, swap!

Correct order, pass!

Page 26: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Bubble Sort

• Fix the last number, do the same procedures for first N-1 numbers again...

• Fix the last 2 numbers, do the same procedures for first N-2 numbers again...

• ...• Fix the last N-2 numbers, do the same

procedures for first 2 numbers again...

Page 27: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Bubble Sort

for i -> 1 to n-1for j -> 1 to n-i

if a[j]>a[j+1], swap them

• How to swap?

Page 28: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Merge Sort & Quick SortMany a little makes a mickle...

Page 29: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Merge Sort

• Now given two SORTED list, how would you ‘merge’ them to form ONE SORTED list?

148 22

10 13 29 65

List 1:

List 2:

Temporary list: 8 10 13 14 22 29 65

Combined list:

8 10 13 14 22 29 65

Page 30: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Merge Sort

Merge1) While both lists have numbers still not yet considered

• Compare the current first number in two lists• Put the smaller number into temporary list, then discard it

2) If list 1 is not empty, add them into temporary list3) If list 2 is not empty, add them into temporary list4) Put the numbers in temporary list back to the desired list

Page 31: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Merge Sort

• Suppose you are given a ‘function’ called ‘mergesort(L,R)’, which can sort the left half and right half of list from L to R:

• How to sort the whole list?• Merge them!• How can we sort the left and right half?• Why not making use of ‘mergesort(L,R)’?

148 2210 13 29 65L R(L+R)/2 (L+R)/2+1

Page 32: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Merge Sort

mergesort(L,R){If L is equal to R, done;Otherwise,

m=(L+R)/2;mergesort(L,M);mergesort(M+1,R);Merge the lists [L,M] and [M+1,R];

}

Page 33: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Merge Sort

14 8 2210 132965

mergesort(0,6)

mergesort(0,3)

mergesort(0,1)

mergesort(0,0) mergesort(1,1)

mergesort(2,3)

mergesort(2,2) mergesort(3,3)

mergesort(4,6)

mergesort(4,5) mergesort(6,6)

mergesort(4,4) mergesort(5,5)

10 65 291310 13 6529 148 148 2210 13 6529148 22

Page 34: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Merge Sort

• Time complexity?• O(N lg N)• Why?

Page 35: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Quick Sort

• Choose a number as a ‘pivot’• Put all numbers smaller than ‘pivot’ on its left

side• Put all numbers greater than (or equal to)

‘pivot’ on its right side

148 2210 13 29 65

6522 2910 13 8 14

Page 36: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Quick Sort• How?

• y shifts to right by 1 unit in every round• Check if a[y] < pivot– If yes, shift x to right by 1 unit and swap a[x] and a[y]

• If y is at 2nd last position, swap a[x+1] and pivot• Time complexity?

148 2210 13 29 65

x y

a[y] < pivot! shift x, swap!

Page 37: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Quick Sort

• Use similar idea as in merge sort• If we have a function called ‘quicksort(L,R)’...– Make use of ‘quicksort(L,R)’ to sort the two

halves!

6522 2910 13 8 14

Page 38: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Quick Sort

quicksort(L,R){If L is equal to R, done;Otherwise,

Choose a number as a pivot, say a[p];Perform pivoting action;quicksort(L,p-1);quicksort(p+1,R);

}

Page 39: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Quick Sort

• Time complexity in worst case?• O(N2) • What is the worst case?• Preventions:– Choose a random number as pivot– Pick 3 numbers, choose their median

• Under randomization, expected time complexity is O(N lg N)

Page 40: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Counting SortNo more comparison...

Page 41: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Counting Sort

• Create a list, then tally!• Count the occurences of elements• Example:– Sort {2,6,1,4,2,1,9,6,4,1} in ascending order– A list from 1 to 9 (or upper bound of input no.)– Three ‘1’s, two ‘2’s, two ‘4’s, two ‘6’s and one ‘9’– List them all!

Page 42: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Counting Sort

• Time complexity?• O(range * N)• Good for numbers with small range

Page 43: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

More...If we have time...

Page 44: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

More...

• Lower bound of sorting efficiency?!• Guess the answer by binary search• Count the number of ‘inversions’• Find the kth largest number• Other sorting algorithms

Page 45: Searching and Sorting Gary Wong. Prerequisite Time complexity Pseudocode (Recursion)

Time for lunchYummy!