Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
59 lines (49 loc) · 1.09 KB

File metadata and controls

59 lines (49 loc) · 1.09 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
using namespace std;
//快速排序
//快排也是采用分治法
static void swap(int *a, int *b)//a,b都是地址
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
static int partition(int arr[], int left, int right)//将大于基数的放在基数右边,将小于基数的放在基数左边
{
int i = left;
int j = right;
int pivot = arr[left];//将左边的选为基数
while(i < j){
while(i < j && arr[j] >= pivot){
j--;
}
while(i < j && arr[i] <= pivot){
i++;
}
if(i < j){
swap(&arr[i], &arr[j]);//&符号取地址
}
}
swap(&arr[left], &arr[i]);
return i;//返回下标
}
static void quick_sort(int arr[], int left, int right)
{
int pivot_pos;//将数组分开处的下标
if(left < right){
pivot_pos = partition(arr, left, right);//交换后的下标
quick_sort(arr, left, pivot_pos);
quick_sort(arr, pivot_pos + 1, right);
}
}
int main(){
int arr[] = {9, 8, 9, 10, 4, 1, 6, 7};
int L = 0;
int R = 7;
int i;
quick_sort(arr, L, R);
for(i = 0; i <= R; i++){
std::cout << arr[i] << '\n';
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.