Results 1 to 2 of 2

Thread: C Quicksort

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2001
    Posts
    117

    C Quicksort

    Can anyone give the code for a quicksort in ansi C. Including the code to find the pivot?

    Thanx...


  2. #2
    jim mcnamara
    Guest
    If you code in C you use the qsort() function. This type of sort has been around since the mid- 1960's, so people have gotten the function to work pretty well by swapping pointers rather than actual data.
    Sample
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    in num[10] = {1,3,5,2,4,6,7,9,8,0};
    int compare(const *void, const *void);
    
    int main(in argc,char *argv[]){
      int i;
      printf("Starting array ");
      for (i=0;i<10;i++) printf(" %d ",num[i]);
      printf("\n");
      /* this odes a very efficient quicksort */
      qsort(num, 10, sizeof(int), compare);
      printf("Ending array ");
      for (i=0;i<10;i++) printf(" %d ",num[i]);
      return 0;
    }
    int compare(const * void i, const void * j){
               return *(int *)i - *(int *)j;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width