330 likes | 502 Views
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
E N D
HKOI 2010Intermediate 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 • Binary Search
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
Binary Search • Requirement: the list of data is sorted • The idea is based on the elimination of impossible region
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
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 ); }
Binary Search • Complexity: O(log N) • Proof?
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
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
Sorting - Introduction • There are several easy algorithms to sort in O(n2) • There are slightly more complicated O(n log n) sorting algorithms.
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)
Stability • Which of the discussed sorting methods are stable? • It depends on your implementation
Sorting - Introduction • Different Sorting Methods: • Bogosort • Bubble Sort • Insertion Sort • Selection Sort • Quick Sort • Heap Sort • Shell Sort • Merge Sort • Radix Sort
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
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.
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
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])
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
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
Other O(n2) sortings • Selection Sort • Insertion Sort
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
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
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
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;
Merge Sort • Complexity? • O(n log n)
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
Counting Sort • Complexity: O(M+N) • It can be used for small integers and limited by M
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