Sunday, May 27, 2012

sorting programs and algorithms


/*Source code of simple quick sort implementation using array ascending order in c programming language*/
#include<stdio.h>
#include<conio.h>

void quicksort(int [10],int,int);

void main()
{
  int size,i;
  int a[]={5,9,7,2,4};
  n=5;
  clrscr();
/*  printf("Enter size of the array: ");
  scanf("%d",&n);

  printf("Enter %d elements: ",size);
  for(i=0;i<n;i++)
    scanf("%d",&a[i]);
  */
  quicksort(x,0,n-1);

  printf("Sorted elements: ");
  for(i=0;i<n;i++)
    printf(" %d",a[i]);
    getch();
}
void swap(int *x, int *y)
{
 int temp;
 temp=*x;
 *x=*y;
 *y=temp;
}

void quicksort(int x[10],int f,int l)
{
 int pivot,j,temp,i;
 if(f<last)
 {
   p=f;
   i=f;
   j=l;
   while(i<j)
   {
     while(x[i]<=x[p]&&i<l)
i++;
     while(x[j]>x[p])
j--;
     if(i<j)
     {
       swap(&x[i],&x[j]);
     }
    }//while
    swap(&x[p],&x[j]);
    quicksort(x,f,j-1);
    quicksort(x,j+1,l);
 }//if
}
/*
Output:
Enter size of the array: 5
Enter 5 elements: 3 8 0 1 2
Sorted elements: 0 1 2 3 8*/

No comments:

Post a Comment