Friday, August 3, 2012

QuickSort function Sample Code

//Program to sort names in an array using quicksort
#include <stdio.h
>
#include<conio.h>
#include <stdlib.h>
#include <string.h>

int sort_function( const void *a, const void *b);

char list[5][4] = { "cat", "car", "cab", "cap", "can" };

int main(void)
{
int x;
clrscr();
qsort((void *)list, 5, sizeof(list[0]), sort_function);
for (x = 0; x
< 5; x++)
printf("%s ", list[x]);
getch();
return 0;
}

int sort_function(const void *a,const void *b)
{
return( strcmp((char *)a,(char *)b) );
}

No comments:

Post a Comment