Implement program for randomized version of quick sort and compare its performance with normal version of quick sort using steps count on various number of inputs.


RANDOMIZED QUICK SORT:
#include<stdio.h>
#include<conio.h>
#define size 10
unsigned long count=0;
void randomized_quick(int a[],int low,int high)
{
      int pivot;
      count++;
      count++;
      if(low<high)
      {
                  count++;
                  pivot=randomized_part(a,low,high);
                  count++;
                  randomized_quick(a,low,pivot-1);
                  count++;
                  randomized_quick(a,pivot+1,high);
      }
}
int randomized_part(int a[],int low,int high)
{
      int pos,left,right,temp,pivot;
      count++;
      pos=(rand()%(high-low))+low;
      count++;
      temp=a[low];
      count++;
      a[low]=a[pos];
      count++;
      a[pos]=temp;
      count++;
      pivot=a[low];
      count++;
      left=low;
      count++;
      right=high;
      count++;
      while(left<right)
      {
                  count++;
                  while(a[left]<=pivot && left<right)
                  {
                              count++;
                              count++;
                              left++;
                  }
                  count++;
                  while(a[right]>pivot)
                  {
                              count++;
                              count++;
                              right--;
                  }
                  count++;
                  count++;
                  if(left<right)
                  {
                              temp=a[left];
                              count++;
                              a[left]=a[right];
                              count++;
                              a[right]=temp;
                              count++;
                  }
      }
      count++;
      temp=a[right];
      count++;
      a[right]=a[low];
      count++;
      a[low]=temp;
      count++;
      count++;
      return right;
}
void main()
{
      int a[size],i,choice;
      clrscr();
      printf("1. sorted data     2.random data : ");
      scanf("%d",&choice);
      switch(choice)
      {
                  case 1:
                              for(i=0;i<size;i++)
                              {
                                          a[i]=i+1;
                              }
                              randomized_quick(a,0,size-1);
                              break;
                  case 2:
                              for(i=0;i<size;i++)
                              {
                                          a[i]=rand()%size;
                              }
                              randomized_quick(a,0,size-1);
                              break;
      }
      printf("Count= %lu",count);
      getch();
}



NORMAL QUICK SORT:
#include<stdio.h>
#include<conio.h>
#define size 10
unsigned long long count=0;
void quick_sort(int a[],int low,int high)
{
      int pivot;
      count++;
      count++;
      if(low<high)
      {
                  count++;
                  pivot=partition(a,low,high);
                  count++;
                  quick_sort(a,low,pivot-1);
                  count++;
                  quick_sort(a,pivot+1,high);
      }
}

int partition(int a[],int low,int high)
{
      int pos,left,right,temp,pivot;
      count++;
      pivot=a[low];
      count++;
      left=low;
      count++;
      right=high;
      count++;
      while(left<right)
      {
                  count++;
                  while(a[left]<=pivot && left<right)
                  {
                              count++;
                              count++;
                              left++;
                  }
                  count++;
                  while(a[right]>pivot)
                  {
                              count++;
                              count++;
                              right--;
                  }
                  count++;
                  count++;
                  if(left<right)
                  {
                              temp=a[left];
                              count++;
                              a[left]=a[right];
                              count++;
                              a[right]=temp;
                              count++;
                  }
      }
      count++;
      temp=a[right];
      count++;
      a[right]=a[low];
      count++;
      a[low]=temp;
      count++;
      count++;
      return right;
}
void main()
{
      int a[size],i,choice;
      clrscr();
      printf("1. sorted data     2.random data : ");
      scanf("%d",&choice);
      switch(choice)
      {
                  case 1:
                              for(i=0;i<size;i++)
                              {
                                          a[i]=i+1;
                              }
                              quick_sort(a,0,size-1);
                              break;
                  case 2:
                              for(i=0;i<size;i++)
                              {
                                          a[i]=rand()%size;
                              }
                              quick_sort(a,0,size-1);
                              break;
      }
      printf("Count= %llu",count);
      getch();
}

Comments

Popular posts from this blog

Write user defined functions for the following sorting methods and compare their performance by time measurement with random data and Sorted data.

Implement a function of sequential search and count the steps executed by function on various inputs for best case and worst case. Also write complexity in each case and draw a comparative chart.

Implement functions to print nth Fibonacci number using iteration and recursive method.