You are on page 1of 23

# Sorting and Searching

## • Sorting is arranging of data either in ascending

or descending order
• Searching for data is made easier
• Types of Sorting
– Internal Sorting
• Takes place in main memory
• Applied when small amount of data is to be sorted
– External Sorting
• Requires secondary memory
• Applied when large amount of data is to be sorted
Sorting Methods
Internal External

quick sort

## insertion sort polyphase sort

selection sort

shell sort
Bubble sort – O(n2)
• It is called Bubble sort, because with each
iteration the smaller element in the list
bubbles up towards the first place, just like a
water bubble rises up to the water surface.
Void bubblesort(int a[], int n)
{for(i=0;i<n-1;i++)
{for(j=0;j<n-i-1;j++)
{ if(a[j]>a[j+1])
{ t=a[j];
a[j]=a[j+1];
a[j+1]=t; }}}}
Insertion sort - O(n2)
• Simple
• Efficient for small data compared to bubble
and selection
• Adaptive - reduces total number of steps if
given a partially sorted list, hence it increases
its efficiency.
• Stable - does not change the relative order of
elements with equal keys
• Best case – O(n)
void insertionsort (int a[], int n)
{for(i=1;i<=n-1;i++)
{ j=i;
x=a[i];
while(a[j-1]>x && j>0)
{ a[j]=a[j-1];
j=j-1; }
a[j]=x; }
}
Selection sort - O(n2)
• Simplest
void selectionsort (int elements[], int n)
{for (i=0;i<n-1; i++)
{min = i;
for (j = i+1;j<n; j++)
{if (elements[j] < elements[min])
min = j;}
temp = elements[i];
elements[i] = elements[min];
elements[min] = temp;}}
Shell Sort – O(n ) 2

## • Proposed by Donal Shell

• Diminishing increment sort
• Improved version of insertion sort
• Best Case – O(nlogn)
void shellsort(int a[], int n)
{
for(k=n/2;k>0;k=k/2)
{for(i=k;i<n;i++)
{tmp=a[i];
for(j=i;j>=k&&a[j-k]>tmp;j=j-k)
{a[j]=a[j-k];}
a[j]=tmp;}
}
}
• Non comparison sort
• Digit by digit sort
• Passes – i/p element with max no. of digits
• Θ(d(n+k))
– d – no. of digits
– n – no. of i/p elements
– k - no. of buckets
• Two classifications of radix sorts
– least significant digit (LSD) radix sorts (Sorting numbers)
– most significant digit (MSD) radix sorts (Sorting
alphabets)
{largest=a;
for(i=0;i<n;i++)//finds largest no.
{ if(a[i]>largest)
largest=a[i];}
while(largest>0)//decides no.of passes
{nop++;
largest/=10;}
for(passno=0;passno<nop;passno++)
{for(k=0;k<10;k++)//bucket creation
{ buckcount[k]=0;
for(j=0;j<10;j++)
bucket[k][j]=0; }
for(i=0;i<n;i++)
{ r=(a[i]/divisor)%10;//determines to which bucket element is to be added, initially divisor=1
buckcount[r]++; }
i=0;
for(k=0;k<10;k++)
{for(j=0;j<buckcount[k];j++)
a[i++]=bucket[k][j];}//elements in the bucket are copied to original array
divisor*=10;}}//keeps holds of decimal places.
Quick Sort – O(nlogn)
• Fastest among the sorting methods
• Example of Divide and conquer technique
• Chooses an element called pivot
– Randomly
– First element
– Median (widely used)
• Steps
1. Pivot =lef
2. i=lef+1
3. j=right
4. Increment i until an element>pivot is found
5. Decrement j until an element<pivot is found
6. If i<j, swap elements at i and j.
7. If i>=j, swap element at j and pivot.
void qsort(int lef, int right)
{if(lef<right) // initial values
{ i=lef+1;
j=right;
pivot=lef;
for(;;)
{
while(a[pivot]>=a[i]) //scanning to right for larger element than pivot
i=i+1;
while(a[pivot]<a[j]) //scanning to lef for smaller element than pivot
j=j-1;
if(i<j) //swapping elements of i & j
{ temp=a[i];
a[i]=a[j];
a[j]=temp; }
else
break; } //if i>=j for loop breaks
temp=a[pivot]; //swapping pivot and element at j
a[pivot]=a[j];
a[j]=temp;
qsort(lef,j-1); //repeating quick sort for lef half of array
qsort(j+1,right); //repeating quick sort for right part
}}
Heap Sort – O(nlogn)
• Delete min (descending)
• Delete max (ascending)
• Array interpreted as binary tree
• Two phases
– Phase 1 :
• structure property (tree construction)
• Heap order property
– Phase 2:sorting
void HeapSort(int a[])
{
int i,temp;
BuildMaxHeap(a);
for (i = heapSize; i > 0; i--)
{
temp = a;
a = a[heapSize - 1];
a[heapSize - 1] = temp;
heapSize = heapSize - 1;
heapify(a, 0);
}
}
void heapify(int a[], int i)
{
int l = lef(i), great,temp;
int r = right(i);
if ( (a[l] > a[i]) && (l < heapSize))
{
great = l;
}
else
{
great = i;
}
if ( (a[r] > a[great]) && (r < heapSize))
{
great = r;
}
if (great != i)
{
temp = a[i];
a[i] = a[great];
a[great] = temp;
heapify(a, great);
}
}
void BuildMaxHeap(int a[])
{
int i;
for (i = (heapSize - 1) / 2; i >= 0; i--)
{
heapify(a, i);
}
}
Merge Sort – External Sort- O(nlogn)
//Routine for partitioning the array
void mergesort(int k[],int low,int high)
{

int mid,i;
if(low<high)
{
mid=(low+high)/2;
printf("\nCalling MS low & mid with low=%d mid=%d",low,mid);
mergesort(k,low,mid);
printf("\nCalling MS mid + 1 & high with mid+1=%d high=%d",mid+1,high);
mergesort(k,mid+1,high);
for(i=low;i<=high;i++)
{
printf("%d \t",k[i]);
}
printf("\nCalling Merge fn with low=%d mid=%d high=%d\n\n",low,mid,high);
merge(k,low,mid,high);
}
}
//Routine for arranging in sorted order
void merge(int k[],int low,int mid,int high)
{ int i,j,temp,h;
i=low;
j=mid+1;
h=low;
while(i<=mid && j<=high)
{ if(k[i]<=k[j])
{temp[h]=k[i];
i=i+1; }
else
{temp[h]=k[j];
j=j+1; }
h++; }
if(i>mid)
{ while(j<=high)
{ temp[h]=k[j];
j=j+1;
h=h+1; } }
else
{ while(i<=mid)
{ temp[h]=k[i];
i=i+1;
h=h+1; }}
for(i=low;i<=high;i++)
{ k[i]=temp[i];}
}
Searching – Linear search O(n)

• Linear search
void linearsearch(int a[], int x, int n)
{int flag=0;
for(i=0;i<n;i++)
{
if(a[i]==x)
{ flag=1;
break;}}
if(flag==1)
print("\n The element is found at position %d\n",i+1);
else
print("\nElement notfound");
}
Binary Search
• Divide and Conquer technique
• Input elements should be in sorted order
Binary Search – O(logn)

## void binarysearch( int a[], int x, int n)

{ low=0;
high=n-1;
mid= (low+high)/2;
while(low<high && a[mid]!=x)
{ if(x<a[mid])
high=mid-1;
if(x>a[mid])
low=mid+1;

mid=(low+high)/2;}
if(a[mid]==x)
print("Element found at position %d",mid+1);
else