以下は、クイックソート手法で考えられるコードです。それが正しいかどうかはわかりませんが、私の論理によれば、うまくいくはずです。ただし、このコードを 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;
}