Friday 18 February 2011

GTU C PROGRAMS | QUICK SORT PROGRAM

C :

/* quick.c */
#include <stdio.h>
#include <stdlib.h>
void quick_sort(int array[], int first, int last)
{
 int temp, low, high, list_separator,i;
 low = first;
 high = last;
 list_separator = array[(first + last) / 2];
 do {
  while (array[low] < list_separator)
   low++;
  while (array[high] > list_separator)
   high--;
  if (low <= high)
  {
   temp = array[low];
   array[low++] = array[high];
   array[high--] = temp;
  }
 } while (low <= high);
 for (i = 0; i < 11; i++)
  printf("%d ", array[i]);
 printf("\n");
 getch();
 if (first < high)
  quick_sort(array, first, high);
 if (low < last)
  quick_sort(array, low, last);
}
void main(void)
{
 int values[]={10,23,64,21,74,95,2,59,44,87,55}, i;
 clrscr();
 for (i = 0; i < 11; i++)
  printf("%d ", values[i]);
 printf("\n");
/* printf("\n Unsorted list is as follows \n");
 for (i = 0; i < 20; i++)
 {
  values[i] = rand() % 100;
  printf(" %d", rand() %100);
 }*/
 quick_sort(values, 0, 10);
 printf("\n Sorted list as follows\n");
 for (i = 0; i < 11; i++)
  printf("%d ", values[i]);
 getch();
}

------------------------------------------------------------------

C++

  // QUICK SORT
  # include<iostream.h>
  # include <stdlib.h>
    class quick
       {
 private: int temp, low, high, pivot;
 public:
 void Q_sort(int *, int , int );
 void display(int *, int );
      };
// sorting function
void quick :: Q_sort(int array[], int first, int last)
 {
   low = first;
   high = last;
   pivot = array[(first + last) / 2];
   do {
     while (array[low] < pivot )
       low++;
     while (array[high] > pivot)
       high--;
     if (low <= high)
      {
 temp = array[low];
 array[low++] = array[high];
 array[high--] = temp;
      }
   } while (low <= high);
  if (first < high)
    Q_sort(array, first, high);
  if (low < last)
    Q_sort(array, low, last);
 }
   void quick :: display(int list[], int n)
     {
       cout<<"\n List after sorting the elements:\n";
       for( int i = 1 ; i <= n ; i++)
       {
  cout<<"  "<<list[i];
       }
     }
 void main(void)
  {
   quick sort;
   int list[100];
   int number ;
   cout<< "\n Input the number of elements in the list:";
   cin>> number;
   for ( int i = 1; i <= number; i++)
    {
     cout<<" Input the value for : "<< i <<" : ";
     cin>>list[i];
    }
  sort.Q_sort(list, 1, number);
  sort.display(list, number);
   }

2 comments:

Anonymous said...

nice work by admin
GTU Study Material

GTUSTUDYNOTES said...

Good work


visit
http://gtustudynotes.blogspot.in/

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Grants For Single Moms