# 快速排序c++

0
9

``` 1 #include <iostream>
2 using namespace std;
3
4 void quickSort(double *q ,int n)   //一个double型数组还有一个代表这个数组的位数。
5 {
6
7     double *left,*right;
8     left = &q[0];
9     right = &q[n-1];
10     double middle = q[0];
11 //    cout<<"left指向数组第一位，值为"<<*left<<endl;
12 //    cout<<"right指向数组最右一位，值为"<<*right<<endl;
13     while(left != right)
14     {
15         if (*right < middle)
16         {
17             *left = *right;
18             while (*left < middle)
19             {
20                 left++;
21                 if (left == right)
22                 {
23                     break;
24                 }
25             }
26             *right = *left;
27         } else {
28             right--;
29         }
30
31     }
32     //左右指针指向一致时把middle给这个位置
33     *left = middle;
34
35     //接下来取得*left和*right指向数组的位数
36     int count = 0;   //count1表示有count1个数在middle左边，最小为0
37     for(;q[count]<middle;count++) { }
38     //
39     //处理middle的左边
40     if (count>1)
41     {
42         double *qq = new double[count];
43         qq = q;
44         quickSort(qq,count);
45     }
46
47     //处理middle的右边
48     int count2 = n-count-1; //count2表示有count2个数在middle右边，最小为0
49     if (count2 > 1)
50     {
51         double *ww = new double[count2];
52         ww = left+1;
53         quickSort(ww,count2);
54     }
55
56 }
57
58
59 int main() {
60     cout<<"请输入数组长度";
61     int n;
62     cin>>n;
63     double *p = new double[n];
64     for (int i = 0;i<n;i++)
65     {
66         cin>>p[i];
67     }
68
69     quickSort(p,n);
70
71     for (int xxx = 0;xxx<n;xxx++)
72     {
73         cout<<p[xxx]<<"    ";
74     }
75     return 0;
76 }```

<