1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
// Project 5-1
/* QSDemo.cpp
Program to demonstrate the QuickSort function
*/
#include <iostream>
#include <cstring>
using namespace std;
void quicksort(char *items, int len);
void qs(char *items, int left, int right);
int main () {
char *StrArray = "abcdefghijk";
int Length = strlen(StrArray);
quicksort(StrArray, Length);
return 0;
}
void quicksort(char *items, int len)
{
qs(items, 0, len-1);
}
void qs(char *items, int left, int right)
{
int i, j;
char x;
i = left; j = right;
x = items[((left + right)/2-2)];
do {
while((items[i] < x) && (i < right)) i++;
while((x < items[j]) && (j > left)) j--;
if(i<=j){
char y=items[i];
items[i]=items[j]; // [it breaks here (EDIT: pulled 'cout's *& step-debuged)]
items[j]=y;
i++;
j--;
}
} while(i<=j);
if(left < j) qs(items, left, j);
if(i < right) qs(items, i, right);
}
|