代码如下:
#include<iostream>
using namespace std;
int change(int arr[], int n, int left, int right,int *count);
void sort(int arr[], int left, int right, int n,int *count);
void exchange(int arr[], int left, int right);
int main()
{
int n, i, arr[100] = { 0 };
int count = 3;
cin >> n;
for (i = 0; i < n; i++)
{
cin >> arr[i];
}
sort(arr, 0, n - 1, n,&count);
return 0;
}
int change(int arr[],int n,int left,int right,int *count)
{
int st, flag,i;
flag = arr[left];
st = left;
while (left < right)
{
if (left == st)
{
if (arr[right] > flag)
{
right--;
}
if (arr[right] < flag)
{
exchange(arr, st, right);
st = right;
left++;
}
}
if (right == st)
{
if (arr[left] > flag)
{
exchange(arr, st, left);
st = left;
right--;
}
if (arr[left] < flag)
{
left++;
}
}
if (left == right)
{
arr[st] = flag;
}
}
if (*count)
{
for (i = 0; i < n; i++)
{
cout << arr[i] << ' ';
}
if(*count>1)
cout << endl;
(*count)--;
}
return left;
}
void sort(int arr[],int left, int right,int n,int *count)
{
if (left < right)
{
int flag = change(arr, n, left, right,count);
sort(arr, left, flag - 1,n,count);
sort(arr, flag + 1, right,n,count);
}
}
void exchange(int arr[],int left, int right)
{
int m;
m = arr[left];
arr[left] = arr[right];
arr[right] = m;
}