# 【算法】快速排序法(一)（二）（三）

Algorithm Gossip: 快速排序法(一)

2

[41] 24 76* 11 45 64 21 69 19 36*

[41] 24 36 11 45* 64 21 69 19* 76

[41] 24 36 11 19 64* 21* 69 45 76

[41] 24 36 11 19 21 64 69 45 76

21 24 36 11 19 [41] 64 69 45 76

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define MAX 10

#define SWAP(x,y) {int t; t = x; x = y; y = t;}

void quicksort(int[], int, int);

int main(void) {

int number[MAX] = {0};

int i, num;

srand(time(NULL));

printf("排序前:");

for(i = 0; i < MAX; i++) {

number[i] =rand() % 100;

printf("%d ", number[i]);

}

quicksort(number, 0, MAX-1);

printf("\n排序后:");

for(i = 0; i < MAX; i++)

printf("%d ", number[i]);

printf("\n");

return 0;

}

void quicksort(int number[], int left, int right) {

int i, j, s;

if(left < right) {

s =number[left];

i = left;

j = right +1;

while(1) {

// 向右找

while(i + 1 < number.length && number[++i] < s) ;

// 向左找

while(j -1 > -1 && number[--j] > s) ;

if(i >= j)

break;

SWAP(number[i], number[j]);

}

number[left] = number[j];

number[j] = s;

quicksort(number, left, j-1);// 对左边进行递回

quicksort(number, j+1, right);// 对右边进行递回

}

}

Algorithm Gossip: 快速排序法(二)

41 24 76 11 45 64 21 69 19 36

45,您往右找比45大的,往左找比45小的进行交换:

41 24 76* 11 [45] 64 21 69 19 *36

41 24 36 11 45* 64 21 69 19* 76

41 24 36 11 19 64* 21* 69 45 76

[41 24 36 11 19 21] [64 69 45 76]

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define MAX 10

#define SWAP(x,y) {int t; t = x; x = y; y = t;}

void quicksort(int[], int, int);

int main(void) {

int number[MAX] = {0};

int i, num;

srand(time(NULL));

printf("排序前:");

for(i = 0; i < MAX; i++) {

number[i] =rand() % 100;

printf("%d ", number[i]);

}

quicksort(number, 0, MAX-1);

printf("\n排序后:");

for(i = 0; i < MAX; i++)

printf("%d ", number[i]);

printf("\n");

return 0;

}

void quicksort(int number[], int left, int right) {

int i, j, s;

if(left < right) {

s =number[(left+right)/2];

i = left -1;

j = right +1;

while(1) {

while(number[++i] < s) ; // 向右找

while(number[--j] > s) ; // 向左找

if(i >= j)

break;

SWAP(number[i], number[j]);

}

quicksort(number, left, i-1); // 对左边进行递回

quicksort(number, j+1, right); // 对右边进行递回

}

}

Algorithm Gossip: 快速排序法(三)

QUICKSORT(A, p, r)

if p < r

then q <-PARTITION(A, p, r)

QUICKSORT(A, p, q-1)

QUICKSORT(A, q+1, r)

end QUICKSORT

PARTITION(A, p, r)

x <- A[r]

i <- p-1

for j <- p to r-1

do ifA[j] <= x

then i <- i+1

exchange A[i]<->A[j]

exchange A[i+1]<->A[r]

return i+1

end PARTITION

#include <stdio.h>

#include <stdlib.h>

#include <time.h>

#define MAX 10

#define SWAP(x,y) {int t; t = x; x = y; y = t;}

int partition(int[], int, int);

void quicksort(int[], int, int);

int main(void) {

int number[MAX] = {0};

int i, num;

srand(time(NULL));

printf("排序前:");

for(i = 0; i < MAX; i++) {

number[i] = rand() % 100;

printf("%d ", number[i]);

}

quicksort(number, 0, MAX-1);

printf("\n排序后:");

for(i = 0; i < MAX; i++)

printf("%d ", number[i]);

printf("\n");

return 0;

}

int partition(int number[], int left, int right) {

int i, j, s;

s = number[right];

i = left - 1;

for(j = left; j < right; j++) {

if(number[j] <= s) {

i++;

SWAP(number[i], number[j]);

}

}

SWAP(number[i+1], number[right]);

return i+1;

}

void quicksort(int number[], int left, int right) {

int q;

if(left < right) {

q =partition(number, left, right);

quicksort(number, left, q-1);

quicksort(number, q+1, right);

}

}

122 篇文章55 人订阅

0 条评论

## 相关文章

29680

### 索引优先队列-IndexedPrirotyQueue的原理及实现（源码）

1.索引优先队列的意义 索引优先队列是一个比较抽象的概念，它是一个优先队列，又带有索引，这个索引是用来干什么的呢？ 在正常的队列中，我们只能访问队列头元素，整个...

47980

38660

24460

22700

19100

9210

37350

435100

### 【批处理学习笔记】第二十一课：数值计算

批处理里面的数值计算功能较弱，只能够进行整型计算，忽略浮点数的小数部分；同时数值计算的范围也受限于系统位数，对于目前较为常见的32位机来说，数值计算能处...

29340