Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179
Deprecated: Creation of dynamic property PPDStack::$accum is deprecated in /var/www/cppe.ru/www/includes/parser/Preprocessor_DOM.php on line 803
Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/includes/exception/MWExceptionHandler.php on line 179
Deprecated: Constant E_STRICT is deprecated in /var/www/cppe.ru/www/vendor/mediawiki/at-ease/src/Functions.php on line 48
Материал из 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.