下面给出的是我能想到的快速排序技术的代码。我不确定它是否正确,但按照我的逻辑,我猜它应该可以正常工作。但是,我认为我做得太过火了,因为当我尝试在 DevC++ 上运行此代码时,它会崩溃并关闭程序。并非每个程序都会发生这种情况,因此显然只有此代码存在一些问题。
#include<iostream.h>
#include<conio.h>
int quick(int, int);
int split(int beg, int end);
int a[7] = { 43, 6, 235, 76, 23, 65, 29 };
int main() {
quick(0, 6);
getch();
return 1;
}
int quick(int beg, int end) {
//trial for self coding
int loc = split(beg, end);
quick(beg, loc - 1);//first half
quick(loc + 1, end);//second half
//end of coding
cout << "\nThe sorted array is :\t";
for (int i = 0; i < 7; i++)
cout << a[i] << "\t";
return 0;
}
//SPLIT FUNC STARTS
int split(int beg, int end) {
int temp, loc, left, right, count = 1;
left = beg;
right = end;
loc = beg;
while (left != right) {
if ((count % 2) != 0) {
while (a[loc] <= a[right]) {
right--;
}
if (loc == right)
return loc;
if (a[loc] > a[right]) {
temp = a[loc];
a[loc] = a[right];
a[right] = temp;
loc = right;
left++;
count++;
continue;
}
}// end of count%2 if
else {
while (a[left] <= a[loc])
left++;
if (loc == left)
return loc;
if (a[loc] < a[left]) {
temp = a[loc];
a[loc] = a[right];
a[right] = temp;
loc = left;
right--;
count++;
continue;
}
}//end of else
}// end of while
return loc;
}