赞
踩
#include<iostream> using namespace std; void swap(int &a, int &b) { int temp = a; a = b; b = temp; } void quicksort(int a[],int left, int right) { if (left > right) return; int pivot = a[left]; int i = left; int j = right; while (i != j)//先从左向右排 { while (i < j && a[i] < pivot) { ++i; } while (i<j && a[j]>pivot) { --j; } if (i < j) { swap(a[i], a[j]); } } //a[left] = a[j]; //a[j] = pivot; quicksort(a,left, j - 1); quicksort(a,j+1, right); } int main() { int n; cout << "输入数据个数:" << endl; cin >> n; int* a = new int[n]; cout << "输入数据:" << endl; for (int i= 0; i < n; i++) { cin >> a[i]; } quicksort(a,0, n - 1); for (int j = 0; j < n; j++) { cout << a[j] << " "; } return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。