You are on page 1of 27

Searching and Sorting Algorithms

Dr S V Rao
Assoc. Professor Department of Computer Science Indian Institute Of Technology Guwahati svrao@iitg.ernet.in

March 22-24, 2011

Dr S V Rao

Searching and Sorting Algorithms

Searching
Linear Search Binary Search Goal: determine whether a particular value is in the array if present return location in the array Example:where is 21? array to be searched: A number of elements: N

We will search A[0], A[1], ... , A[N-1] respectively.


Dr S V Rao Searching and Sorting Algorithms

Linear Search Algorithm


Begin; Input:Name of Araay A, size of Array N and Item to be found Output:Index of Array where Item is present; foreach (i=0 to i N 1) do if (A[i]= Item) then return i; end end return -1; End;

Analysis Compexcity is O(N)


Dr S V Rao Searching and Sorting Algorithms

Binary Search
Array must be sorted choose middle element and decide whether to search the left or right half At each decision point, the space to be searched is cut in half Ex. Find 29 A[10, 13, 14, 29, 37, 40, 45, 57, 64, 75] beg=0, end=9; mid=(0+9)/2=4; A[4]=Item ie 37=29 end=mid-1=4-1=3; mid=(0+3)/2=1; A[1]=Item ie 13=29 beg=mid+1=1+1=2; mid=(2+3)/2=2; A[2]=Item ie 14=29 beg=2+1=3; mid=(3+3)/2=3; A[3]=Item ie 29=29 Now A[mid]=Item thats true then return mid
Dr S V Rao Searching and Sorting Algorithms

Algorithm
Input:Name of Araay A, size of Array N and Item to be found Output:Index of Array where Item is present; int beg=0, end=N-1 and mid; mid=(beg+end)/2 foreach (beg end) and (A[mid] = Item) do if (Item < A[mid]) then end=mid-1; else beg=mid+1; end mid=(beg+end)/2; end if (beg > end) then return -1; else return mid; end
Dr S V Rao Searching and Sorting Algorithms

Analysis Every time we divide the array by half Compexcity is O(LgN)

Dr S V Rao

Searching and Sorting Algorithms

Sorting

sort in ascending or descending order sorting technique dier in eciency Sorting Technique Bubble-sort Insertion-sort Selection-sort Radix-sort Counting-sort

Dr S V Rao

Searching and Sorting Algorithms

Dr S V Rao

Searching and Sorting Algorithms

Dr S V Rao

Searching and Sorting Algorithms

Dr S V Rao

Searching and Sorting Algorithms

Analysis Complexity in any case: O(N 2 )

Dr S V Rao

Searching and Sorting Algorithms

Insertion Sort partition the array into two regions: sorted and unsorted. Take each item from the unsorted region and insert correctly in the sorted region

Dr S V Rao

Searching and Sorting Algorithms

Dr S V Rao

Searching and Sorting Algorithms

Algorithm
InsertionSort(A, N) { for i = 1 to N-1 { key = A[i] j = i - 1; while (j 0) and (A[j] > key) { A[j+1] = A[j] j=j-1 } A[j+1] = key } }
Dr S V Rao Searching and Sorting Algorithms

Analysis Best Case:If A is sorted, O(N) Worst Case: If A is reversed sorted, O(N 2 ) Average Case: If A is randomly sorted, O(N 2 )

Dr S V Rao

Searching and Sorting Algorithms

Selection Sort

nd the smallest element in the array exchange it with the element in the rst position second smallest element and exchange it with the element in the second position and so on

Dr S V Rao

Searching and Sorting Algorithms

Ex. A[4, 56, 34, 24, 67, 13, 2, 50], smallest is 2, replace with 4 A[2, 56, 34, 24, 67, 13, 4, 50] second smallest is 4, replace with 56 A[2, 4, 34, 24, 67, 13, 56, 50] next smallest 13, replace with 34 A[2, 4, 13, 24, 67, 34, 56, 50] next smallest is 24, replace 24 with 24 A[2, 4, 13, 24, 67, 34, 56, 50] A[2, 4, 13, 24, 34, 67, 56, 50] A[2, 4, 13, 24, 34, 50, 56, 67] A[2, 4, 13, 24, 34, 50, 56, 67]
Dr S V Rao Searching and Sorting Algorithms

Algorithm
Selection Sort(A,N) { for i =0 to N - 2 { k=i for j = i + 1 to N-1 {Find the i th smallest element.} { if A[j] <A[k] then k = j } if k= i then interchange A[i] and A[k] } }

Dr S V Rao

Searching and Sorting Algorithms

Analysis Complexity in any case: O(N 2 )

Dr S V Rao

Searching and Sorting Algorithms

Radix Sort
Sorts the keys according to one digit at a time Start from LSB(Least Signicant Bit) x 123 435 396 945 257 394 246 581 763 581 123 763 394 435 945 396 246 257 123 435 945 246 257 763 581 394 396 123 246 257 394 396 435 581 763 945
Dr S V Rao Searching and Sorting Algorithms

Algorithm

RadixSort(int **A, int N,int d) { for(i=1;i<d;i++) StableSortColumn(A,N,i); } The keys are (at most) d digits No. of keys: N .

Dr S V Rao

Searching and Sorting Algorithms

Complexity is d*T(N), where T(N) is complexity of Stable Sort

Dr S V Rao

Searching and Sorting Algorithms

Counting Sort

Sorting in linear time All elements are integers No comparisons between elements Auxiliary storage

Dr S V Rao

Searching and Sorting Algorithms

Input: A[1 . . . n], where A[ j] {1, 2, . . . , k} Output: B[1 . . . n], sorted Auxiliary storage: C[1 . . . k]

Dr S V Rao

Searching and Sorting Algorithms

Algorithm
for i=1 to k do C[i]=0 for j=1 to n do C[A[j]]=C[A[j]]+1 /After this step,C[i] contains num of elements whose key =i/ for i=2 to k do C[i]=C[i] + C[i-1] for j=n downto 1 do x =A[ j ] B[C[x]] =x C[x]=C[x]-1

Dr S V Rao

Searching and Sorting Algorithms

Example
A :[4, 1 ,3, 4, 3]; B:[ , , , , ] C:[ , , , ] After Loop one A: [4, 1 ,3, 4, 3]; B:[ , , , , ] C:[ 0, 0,0 ,0 ] In Loop 2, j=1; A: [4, 1 ,3, 4, 3]; B:[ , , , , ] C:[ 0, 0,0 ,1 ] j=2; A: [4, 1 ,3, 4, 3]; B:[ , , , , ] C:[ 1, 0,0 ,1 ] j=3; A: [4, 1 ,3, 4, 3]; B:[ , , , , ] C:[ 1, 0, 1,1 ] j=4; A: [4, 1 ,3, 4, 3]; B:[ , , , , ] C:[ 1, 0, 1,2 ] j=5; A: [4, 1 ,3, 4, 3]; B:[ , , , , ] C:[ 1, 0, 2,2 ]

Dr S V Rao

Searching and Sorting Algorithms

Example
After loop 3 A:[4, 1 ,3, 4, 3]; B:[ , , , , ] C:[ 1, 1, 3,5 ] in loop 4 j=5; 3 A:[4, 1 ,3, 4, 3]; B:[ , ,3 , , ] C:[ 1, 1, 2,5 ] j=4; A:[4, 1 ,3, 4, 3]; B:[ , ,3 , ,4 ] C:[ 1, 1, 2,4 ] j=3; A:[4, 1 ,3, 4, 3]; B:[ ,3 ,3 , ,4 ] C:[ 1, 1, 1,4 ] j=2; A:[4, 1 ,3, 4, 3]; B:[ 1,3 ,3 , ,4 ] C:[0, 1, 1,4 ] j=1; A:[4, 1 ,3, 4, 3]; B:[ 1,3 ,3 ,4 ,4 ] C:[0, 1, 1,3 ]

Dr S V Rao

Searching and Sorting Algorithms

You might also like