1 / 33

HKOI 2010 Intermediate Training

HKOI 2010 Intermediate Training. Searching and Sorting 6/3/2010. Introduction. Searching and Sorting are the most popular problems Merge algorithm required sorted list. Searching. Task of searching for a key in a list or in an array Searching algorithms will be discussed: Linear Search

harmon
Download Presentation

HKOI 2010 Intermediate Training

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. HKOI 2010Intermediate Training Searching and Sorting 6/3/2010

  2. Introduction • Searching and Sorting are the most popular problems • Merge algorithm required sorted list

  3. Searching • Task of searching for a key in a list or in an array • Searching algorithms will be discussed: • Linear Search • Binary Search

  4. Linear Search • Or called Sequential Search • checking every element of a list sequentially until a match is found • Complexity: O(N) • On average, N/2 comparisons is needed • Best case: the first element searched is the value we want • Worst case: the last element searched is the value we want

  5. Binary Search • Requirement: the list of data is sorted • The idea is based on the elimination of impossible region

  6. Binary Search • Algorithm: Let the current region is from A[low] to A[high] Compare key with A[midpoint] If key<A[midpoint], then A[midpoint] to A[high] does not contain the key If key>A[midpoint], then A[low] to A[midpoint] does not contain the key Repeat the above process until the key is found

  7. Binary Search int search( key, r ) typekey key; dataarray r; { int high, i, low; for ( low=(-1), high=n; high-low > 1; ) { i = (high+low) / 2; if ( key <= r[i].k ) high = i; else low = i; } if ( key==r[high].k ) return( high ); else return( -1 ); }

  8. Binary Search • Complexity: O(log N) • Proof?

  9. Binary Search • N items • if N is odd => both sub-intervals contain (N - 1)/2 elements. • If N is even => the two sub-intervals contain N/2 - 1 and N/2 elements. • N => N/2 => N/4 => N/8….I • The worst case : the value is not in the list • ⌊log2(N) + 1⌋ iterations

  10. Sorting - Introduction • Sorting is the ordering of your data in a consistent manner. • Each element is usually part of a collection of data called a record. • Each record contains a key, which is the value to be sorted

  11. Sorting - Introduction • There are several easy algorithms to sort in O(n2) • There are slightly more complicated O(n log n) sorting algorithms.

  12. Stability • A stable sorting algorithm is any sorting algorithm that preserves the relative ordering of items with equal values • Before: (a,3),(b,5),(c,2),(d,5),(e,4) • After: (c,2),(a,3),(e,4),(b,5),(d,5)

  13. Stability • Which of the discussed sorting methods are stable? • It depends on your implementation

  14. Sorting - Introduction • Different Sorting Methods: • Bogosort • Bubble Sort • Insertion Sort • Selection Sort • Quick Sort • Heap Sort • Shell Sort • Merge Sort • Radix Sort

  15. Sorting - Introduction • Ordering--We require the existence of the “<“ and “>” operators, which can be used to place a consistent ordering on the input. • Eg. Sort the elements: Dog, Cat, Mouse, Tiger, Turtoise

  16. Bubble Sort • This is probably the simplest way sort an array of objects. The basic idea is to compare two neighbouring objects, and to swap them if they are in the wrong order • In each pass, the largest key in the list will be bubbled to the end, but the earlier keys may still be out of order.

  17. Bubble Sort Example Original 34 8 64 51 32 21 # of swaps ---------------------------------------- After p = 1, 8 34 51 32 21 64 4 After p = 2, 8 34 32 21 51 64 2 After p = 3, 8 32 21 34 51 64 2 After p = 4, 8 21 32 34 51 64 1 After p = 5, 8 21 32 34 51 64 0 After p = 6, 8 21 32 34 51 64 0

  18. Bubble Sort Algorithm: for i = 1 to N-1 do for j = 1 to N-i do if A[j].key > A[j+1].key then swap(A[j],A[j+1])

  19. Bubble Sort • Improved version: for i = 1 to N-1 do begin for j = 1 to N-i do if A[j].key > A[j+1].key then swap(A[j],A[j+1]) if no swapping was done, then stop here end

  20. Bubble Sort • Complexity? • O(n2) • If the file is initially sorted in the reverse order, the total number of comparisons is (n-1) + (n-2) + ... + 2 + 1 = (n-1) * n/2

  21. Other O(n2) sortings • Selection Sort • Insertion Sort

  22. 15 38 24 13 1 27 2 15 2 26 24 1 26 2 26 27 13 13 38 38 24 13 24 26 2 1 27 38 15 27 15 1 Merge Sort • Merge 2 sorted list: Cptr Bptr Aptr 1 1 2 1 2 13

  23. 24 15 2 1 24 26 2 15 27 38 38 27 15 2 1 13 13 26 26 27 13 24 1 1 38 24 26 38 27 15 2 13 Merge Sort 1 2 13 15 1 2 13 15 24 1 2 13 15 24 26 Array A is exhausted! 1 2 13 15 24 26 27 38 Copy remainder of array B

  24. Merge Sort • Merge sort follows the divide-and-conquer approach • Divide: Divide the n-element sequence into two (n/2)-element subsequences • Conquer: Sort the two subsequences recursively • Combine: Merge the two sorted subsequence to produce the answer

  25. Merge Sort • procedure ms(l,h:longint); • var mid:longint; • begin • if h>l then begin • mid:=(h+l) div 2; • ms(l,mid); • ms(mid+1,h); • merge(l,mid,h); • end; • end;

  26. Merge Sort • Complexity? • O(n log n)

  27. Counting Sort • Assume 0<A[i]<=M • Algorithm: initialize an array Count of size M to all 0's for i=1 to N inc(Count[A[i]]) Scan the Count array, and printout the sorted list

  28. Counting Sort • Complexity: O(M+N) • It can be used for small integers and limited by M

  29. Bubble sort

  30. Selection sort

  31. Insertion sort

  32. Merge sort

  33. Task • You should know how to do to following! • 1022 Ranklist • 1023 Number Searching • 1068 Moliu Sorting I • 2020 Date Sorting • 6963 Sorting a Three-Valued Sequence • This one may be a bit tricky • 1112 Cut the Trees

More Related