Upload
neranjenvajravelu
View
226
Download
0
Embed Size (px)
Citation preview
8/12/2019 Searching and Sorting Techniques
1/19
8/12/2019 Searching and Sorting Techniques
2/19
Linear search:
Serching the element sequentially(one by one).
Binary search: Serching the element with the condition of binary
search tree(ordered array).
If (k=middle element) then return the position
Else if(k
8/12/2019 Searching and Sorting Techniques
3/19
Linear Search
int sequentialsearch(int k,int a[],int n){int i=0;
while(i
8/12/2019 Searching and Sorting Techniques
4/19
Binary Search
int binarysearch(int k,int a[],int n)
{
Int lower=0,upper=n-1,i;
While(upper>=lower){
mid=(upper+lower)/2;
if(k==a[mid])
{
i=mid;
break;
}
else
{
if(k
8/12/2019 Searching and Sorting Techniques
5/19
8/12/2019 Searching and Sorting Techniques
6/19
Selection sort
Original Array
After Pass1
After Pass2sorted unsorted
After Pass3
After Pass4
After Pass5
23 78 45 8 32 56
8 78 45 23 32 56
8 23 45 78 32 56
8 23 32 78 45 56
8 23 32 45 78 56
8 23 32 45 56 78
8/12/2019 Searching and Sorting Techniques
7/19
8/12/2019 Searching and Sorting Techniques
8/19
Bubble sortFirst pass:
( 514 2 8 )( 154 2 8 ),( 1 542 8 )( 1 452 8 ), Swap since 5 > 4( 1 4 528 )( 1 4 258 ), Swap since 5 > 2( 1 4 2 58)( 1 4 2 58).
Second Pass:
( 142 5 8 )
( 142 5 8 )( 1 425 8 )( 1 245 8 ), Swap since 4 > 2( 1 2 458 )( 1 2 458 )( 1 2 4 58)( 1 2 4 58)
Third Pass:
( 124 5 8 )( 124 5 8 )( 1 245 8 )( 1 245 8 )( 1 2 458 )( 1 2 458 )( 1 2 4 58)( 1 2 4 58)
8/12/2019 Searching and Sorting Techniques
9/19
8/12/2019 Searching and Sorting Techniques
10/19
Insertion sort Eg:playing cards.
Inserting the elemnt in their correct positions.
A sorting algorithm that inserts each item in theproper place into an initially empty list by comparing itwith each item in the list
8/12/2019 Searching and Sorting Techniques
11/19
Insertion sortVoid insertionsort(int a[],int n){
Int i=0,j,temp;While(i
8/12/2019 Searching and Sorting Techniques
12/19
8/12/2019 Searching and Sorting Techniques
13/19
Routine for conquer part
Void merge(int A[],int lower1,int upper1,int lower2,intupper2)
{
int p,q,j,n;
int d[100];
p=lower1;q=lower2;n=0;
while((p
8/12/2019 Searching and Sorting Techniques
14/19
Heap Sort Heap?
Structure property
Heap order propertyApply rule2 from right most non leaf node at bottom
level for heap sort.
Bring down the root node and delete it to add into an
output array.
8/12/2019 Searching and Sorting Techniques
15/19
Routine for heap sortint n;int a[100];void heapsort(){int I,t,m;for(i=n/2,i>=1,i--)
restoreheapdown(i);m=n;while(n>1){t=a[1];a[1]=a[n];
a[n]=t;n=n-1;restoreheapdown(1);}n=m;}
8/12/2019 Searching and Sorting Techniques
16/19
QUICK SORT By picking a pivot element.
Left array must be less than pivot and right array must
be greater than pivot(divide). Recursively solve.(conquer)
Then merge.
void qsort(int a[] int left int right)
8/12/2019 Searching and Sorting Techniques
17/19
void qsort(int a[],int left,int right){
int pivot,temp,i,j;pivot=left;if(left
8/12/2019 Searching and Sorting Techniques
18/19
RADIX SORTdef: sorts data with integer keys by grouping keys by theindividual digits which share the same significant positionand value.
LSD radix sort (comparing LSD) MSD radix sort (comparing MSD)Eg: 170, 45, 75, 90, 802, 2, 24, 66Sorting by least significant digit (1s place) gives:170, 90, 802, 2, 24, 45, 75, 66
Sorting by next digit (10s place) gives:802, 2, 24, 45, 66, 170, 75, 90Sorting by most significant digit (100s place) gives:2, 24, 45, 66, 75, 90, 170, 802
8/12/2019 Searching and Sorting Techniques
19/19
CASE ANANLYSIS
sorting method best average worst
Quick sort nlogn nlogn n^2
Merge sort nlogn nlogn nlogn Heap sort nlogn nlogn nlogn
Insertion sort n^2 n^2 n^2
Selection n^2 n^2 n^2
Bubble n n^2 n^2