A Quicksort for structures of type address : Quick sort « Data Structure Algorithm « C / ANSI-C






A Quicksort for structures of type address

/*
C: The Complete Reference, 4th Ed. (Paperback)
by Herbert Schildt

ISBN: 0072121246
Publisher: McGraw-Hill Osborne Media; 4 edition (April 26, 2000)
*/
struct address {
  char name[40];
  char street[40];
  char city[20];
  char state[3];
  char zip[11];
};

/* A Quicksort for structures of type address. */
void quick_struct(struct address items[], int count)
{
  qs_struct(items,0,count-1);
}

void qs_struct(struct address items[], int left, int right)
{

  register int i, j;
  char *x;
  struct address temp;

  i = left; j = right;
  x = items[(left+right)/2].zip;

  do {
    while((strcmp(items[i].zip,x) < 0) && (i < right)) i++;
    while((strcmp(items[j].zip,x) > 0) && (j > left)) j--;
    if(i <= j) {
      temp = items[i];
      items[i] = items[j];
      items[j] = temp;
      i++; j--;
    }
  } while(i <= j);

  if(left < j) qs_struct(items, left, j);
  if(i < right) qs_struct(items, i, right);
}

           
       








Related examples in the same category

1.The Quicksort
2.A Quicksort for stringsA Quicksort for strings
3.A Quicksort for filesA Quicksort for files
4.How to use sysmtem quick sortHow to use sysmtem quick sort
5.Sort: quicksort: how to use qsort
6.Quick sort on two dimensional string arrayQuick sort on two dimensional string array
7.Use the system quick sort