#include <iostream> #include <cstdio> using namespace std; int a[100] = {4,2,1,7,2,5,2,1}; int swap(int *a,int *b){ int *t; t = a; a = b; b = t; } void quicksort(int left,int right) { if (left > right) { return; } int i = left; int j = right; int tem = a[i]; while (true) { while (a[j] >= tem && j > i) { j--; } while (a[i] <= tem && i < j) { i++; } if (i != j) { swap(a[i], a[j]); } else { swap(a[left], a[i]); break; } } quicksort(left,i - 1); quicksort(i+1,right); } int main() { quicksort(0,7); for(int i = 0;i < 8;i++){ cout << a[i] << " "; } }
算法初步P1.快速排序
最新推荐文章于 2024-10-05 21:10:28 发布