题目描述

已知快速排序的部分代码如下,勿改动,请补充实现快速排序函数:void QuickSort(int first,int end); //quickSort 要求:输出每趟排序结果
#include
using namespace std;
const int MaxSize=100;
class List
{
private:
int r[MaxSize+1];
int n;
public:
List(){n=0;} //empty list
void InsertR(int k) //表尾插入
{ r[++n]=k;}
void Display(); //display
void QuickSort(int first,int end); //quickSort
void QuickSort()
{
QuickSort(1,n);
}
};

void List::Display()
{
for(int i=1;i<=n;i++)
cout<cout<<”\n”;
}
int main()
{
List L;
while(1)
{
int k;
cin>>k;
if(!k) break;
L.InsertR(k);
}
//L.Display();
L.QuickSort();
//L.Display();
return 0;
}

输入

输出

样例输入

12 21 32 2 4 24 21 432 23 9 0

样例输出

9 4 2 12 32 24 21 432 23 21
2 4 9 12 32 24 21 432 23 21
2 4 9 12 32 24 21 432 23 21
2 4 9 12 21 24 21 23 32 432
2 4 9 12 21 24 21 23 32 432
2 4 9 12 21 23 21 24 32 432
2 4 9 12 21 21 23 24 32 432

提示

来源

提交

  1. import java.util.Scanner;
  2. class Ilist{
  3. int[] r;
  4. int n;
  5. int MaxSize = 100;
  6. public Ilist() {
  7. n = 0;
  8. r = new int[MaxSize];
  9. }
  10. void InsertR(int k){
  11. r[++n] = k;
  12. }
  13. void Display(){
  14. // System.out.print("Data:");
  15. for (int i = 1; i <= n; i++) {
  16. System.out.print(r[i]+" ");
  17. }
  18. System.out.println();
  19. }
  20. void QuickSort(){
  21. QuickSort(1,n);
  22. }
  23. void QuickSort(int first,int end){
  24. if (first < end) {
  25. int pivot = Partition(r, first, end);
  26. Display();
  27. QuickSort(first, pivot - 1);
  28. QuickSort(pivot + 1, end);
  29. }
  30. }
  31. int Partition(int r[], int first, int end) {
  32. int i = first, j = end;
  33. while (i < j) {
  34. while (i < j && r[i] <= r[j])j--;
  35. if (i < j) {
  36. int temp = r[j];
  37. r[j] = r[i];
  38. r[i] = temp;
  39. i++;
  40. }
  41. while (i < j && r[i] <= r[j])i++;
  42. if (i < j) {
  43. int temp = r[j];
  44. r[j] = r[i];
  45. r[i] = temp;
  46. j--;
  47. }
  48. }
  49. return i;
  50. }
  51. }
  52. public class Main {
  53. public static void main(String[] args) {
  54. Ilist ilist = new Ilist();
  55. Scanner scanner = new Scanner(System.in);
  56. int key;
  57. while(true){
  58. int x = scanner.nextInt();
  59. if(x == 0)break;
  60. ilist.InsertR(x);
  61. }
  62. ilist.QuickSort();
  63. }
  64. }