C Tutorial/Search Sort/Quicksort

Материал из C\C++ эксперт
Перейти к: навигация, поиск

The Quicksort

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

  /* Quicksort setup function. */
  void quick(char *items, int count)
  {
    qs(items, 0, count-1);
  }
  int qs(char *items, int left, int right)
  {
    register int i, j;
    char x, y;
    i = left; j = right;
    x = items[(left+right)/2];
    do {
      while((items[i] < x) && (i < right)) i++;
      while((x < items[j]) && (j > left)) j--;
      if(i <= j) {
        y = items[i];
        items[i] = items[j];
        items[j] = y;
        i++; j--;
      }
    } while(i <= j);
    if(left < j)
       qs(items, left, j);
    if(i < right)
       qs(items, i, right);
  }

  int main(void)
  {
    char s[255];
    printf("Enter a string:");
    gets(s);
    quick(s, strlen(s));
    printf("The sorted string is: %s.\n", s);
    return 0;
  }
Enter a string:ewqqwerewqqwer12343211234
The sorted string is: 11122233344eeeeqqqqrrwwww.