C Tutorial/Search Sort/Quicksort — различия между версиями

Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
(нет различий)

Версия 17:21, 25 мая 2010

The Quicksort

<source lang="cpp">#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;
 }</source>
Enter a string:ewqqwerewqqwer12343211234
The sorted string is: 11122233344eeeeqqqqrrwwww.