33
HKOI 2010 Intermediate Training Searching and Sorting 6/3/2010

HKOI 2010 Intermediate Training

  • Upload
    harmon

  • View
    39

  • Download
    0

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: HKOI 2010 Intermediate Training

HKOI 2010Intermediate Training

Searching and Sorting

6/3/2010

Page 2: HKOI 2010 Intermediate Training

Introduction Searching and Sorting are the most

popular problems Merge algorithm required sorted list

Page 3: HKOI 2010 Intermediate Training

Searching Task of searching for a key in a list or

in an array Searching algorithms will be

discussed: Linear Search Binary Search

Page 4: HKOI 2010 Intermediate Training

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

Page 5: HKOI 2010 Intermediate Training

Binary Search Requirement: the list of data is sorted The idea is based on the elimination of

impossible region

Page 6: HKOI 2010 Intermediate Training

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 keyIf key>A[midpoint], then A[low] to A[midpoint] does

not contain the keyRepeat the above process until the key is found

Page 7: HKOI 2010 Intermediate Training

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

Page 8: HKOI 2010 Intermediate Training

Binary Search Complexity: O(log N) Proof?

Page 9: HKOI 2010 Intermediate Training

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

Page 10: HKOI 2010 Intermediate Training

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

Page 11: HKOI 2010 Intermediate Training

Sorting - Introduction There are several easy algorithms to

sort in O(n2) There are slightly more complicated

O(n log n) sorting algorithms.

Page 12: HKOI 2010 Intermediate Training

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)

Page 13: HKOI 2010 Intermediate Training

Stability Which of the discussed sorting

methods are stable? It depends on your implementation

Page 14: HKOI 2010 Intermediate Training

Sorting - Introduction Different Sorting Methods:

Bogosort Bubble Sort Insertion Sort Selection Sort Quick Sort Heap Sort Shell Sort Merge Sort Radix Sort

Page 15: HKOI 2010 Intermediate Training

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

Page 16: HKOI 2010 Intermediate Training

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.

Page 17: HKOI 2010 Intermediate Training

Bubble SortExampleOriginal 34 8 64 51 32 21 # of swaps----------------------------------------After p = 1, 8 34 51 32 21 64 4After p = 2, 8 34 32 21 51 64 2After p = 3, 8 32 21 34 51 64 2After p = 4, 8 21 32 34 51 64 1After p = 5, 8 21 32 34 51 64 0After p = 6, 8 21 32 34 51 64 0

Page 18: HKOI 2010 Intermediate Training

Algorithm:for i = 1 to N-1 do

for j = 1 to N-i doif A[j].key > A[j+1].key then

swap(A[j],A[j+1])

Bubble Sort

Page 19: HKOI 2010 Intermediate Training

Bubble Sort Improved version:for i = 1 to N-1 do begin

for j = 1 to N-i doif A[j].key > A[j+1].key then

swap(A[j],A[j+1])if no swapping was done, then stop here

end

Page 20: HKOI 2010 Intermediate Training

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

Page 21: HKOI 2010 Intermediate Training

Other O(n2) sortings

Selection Sort Insertion Sort

Page 22: HKOI 2010 Intermediate Training

Merge Sort Merge 2 sorted list:

1 13

24

26 2 1

527

38

Aptr Bptr Cptr

1 13 24 26 2 15 27 38 1

1 13

24

26 2 1

527

38 1 2

1 13

24

26 2 1

527

38 1 2 1

3

Page 23: HKOI 2010 Intermediate Training

Merge Sort1 1

324

26 2 1

527

38 1 2 1

315

1 13

24

26 2 1

527

38 1 2 1

315

24

1 13

24

26 2 1

527

38 1 2 1

315

24

26

1 13

24

26 2 1

527

38 1 2 1

315

24

26

27

38

Array A is exhausted!

Copy remainder of array B

Page 24: HKOI 2010 Intermediate Training

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

Page 25: HKOI 2010 Intermediate Training

Merge Sort1) procedure ms(l,h:longint);2) var mid:longint;

3) begin4) if h>l then begin5) mid:=(h+l) div 2;6) ms(l,mid);7) ms(mid+1,h);8) merge(l,mid,h);9) end;10) end;

Page 26: HKOI 2010 Intermediate Training

Merge Sort Complexity? O(n log n)

Page 27: HKOI 2010 Intermediate Training

Counting Sort Assume 0<A[i]<=M Algorithm:

initialize an array Count of size M to all 0'sfor i=1 to N

inc(Count[A[i]])Scan the Count array, and printout the sorted list

Page 28: HKOI 2010 Intermediate Training

Counting Sort Complexity: O(M+N)

It can be used for small integers and limited by M

Page 29: HKOI 2010 Intermediate Training

Bubble sort

Page 30: HKOI 2010 Intermediate Training

Selection sort

Page 31: HKOI 2010 Intermediate Training

Insertion sort

Page 32: HKOI 2010 Intermediate Training

Merge sort