New Android apps UniqueKey

Friday, 20 February 2015

Quick sort on String


/* A c program to perform quick sort on String */

#include<stdio.h>
#include<string.h>
#include<ctype.h>

void quick(char [],int,int);
main()
{
 int first,last,i,j,n;
 char a[50];
 
 printf("enter string\n");
 gets(a);
 n=strlen(a);
 
 quick(a,0,n-1);
 printf("new string\n");
 printf("%s",a);
}
void quick(char a[],int first,int last)
{
 int i,j,pivot,temp;
 
 if(first<last)
 {
  pivot=first;
  i=first;
  j=last;
  
  while(i<j)
  {
   while(tolower(a[i])<=tolower(a[pivot])&&i<last)
   i++;
   while(tolower(a[j])>tolower(a[pivot]))
   j--;
   
   if(i<j)
   {
    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
   }
  }
  temp=a[pivot];
  a[pivot]=a[j];
  a[j]=temp;
  quick(a,first,j-1);
  quick(a,j+1,last);
 }
}

No comments:

Post a Comment