HKOI 2010 Intermediate Training

Preview:

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

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

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

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

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 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

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

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

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

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

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

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 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;

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'sfor 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

Bubble sort

Selection sort

Insertion sort

Merge sort

Recommended