快排

#include "assert.h"
#include <iostream>
using namespace std;
void Sort(int *a,int n){
    int i=0,j=0,k=n-1;
    while(j<=k){
        if(a[j]==2){
            j++;
        }
        else if(a[j]==1){
            if(i!=j)
                cout<<i<<"<-->"<<j<<endl;
            int temp;
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
            i++;j++;
        }
        else if(a[j]==3){
            if(j!=k)
                cout<<j<<"<-->"<<k<<endl;
            int temp;
            temp=a[j];
            a[j]=a[k];
            a[k]=temp;
            k--;
        }
    }
    for(int i=0;i<n;i++){
        cout<<a[i];
    }
    cout<<endl;
}
int main() {
	int n;
	cin >> n;
	int * a= new int[n];
	for (int i=0;i<n;i++)
		cin>>a[i];
	Sort(a,n);
}

原文地址:https://www.cnblogs.com/wangjianupc/p/10587204.html