四种排序方法 C 实现
插入法
1
#include "stdio.h"
2
#include "stdlib.h"
3
#include "time.h"
4
5
#define LEN 10
6
7
int RandNumber();
8
void main(){
9
int a[LEN],n,j,t;
10
srand(time(NULL));
11
for(n = 0; n < LEN; n++)
12
{
13
a[n] = RandNumber();
14
printf("%d,", a[n]);
15
}
16
printf("\n");
17
for(n = 1; n < LEN; n++)
18
{
19
j = n;
20
while(j >= 1 && a[j] < a[j - 1]){
21
t = a[j];
22
a[j] = a[j - 1];
23
a[j - 1] = t;
24
j--;
25
}
26
}
27
for(n = 0; n < LEN; n++)
28
printf("%d,", a[n]);
29
30
getchar();
31
}
32
int RandNumber(){
33
return rand()*10000/RAND_MAX;
#include "stdio.h"2
#include "stdlib.h"3
#include "time.h"4

5
#define LEN 106

7
int RandNumber();8
void main(){9
int a[LEN],n,j,t;10
srand(time(NULL));11
for(n = 0; n < LEN; n++)12
{13
a[n] = RandNumber();14
printf("%d,", a[n]);15
}16
printf("\n");17
for(n = 1; n < LEN; n++)18
{19
j = n;20
while(j >= 1 && a[j] < a[j - 1]){21
t = a[j];22
a[j] = a[j - 1];23
a[j - 1] = t;24
j--;25
}26
}27
for(n = 0; n < LEN; n++)28
printf("%d,", a[n]);29

30
getchar();31
}32
int RandNumber(){33
return rand()*10000/RAND_MAX;34
}
冒泡法
1
#include "stdio.h"
2
#include "stdlib.h"
3
#include "time.h"
4
5
#define LEN 10
6
7
int RandNumber();
8
void main(){
9
int a[LEN],n,j,t;
10
srand(time(NULL));
11
for(n = 0; n < LEN; n++)
12
{
13
a[n] = RandNumber();
14
printf("%d,", a[n]);
15
}
16
printf("\n");
17
for(n = 0; n < LEN - 1; n++)
18
{
19
for(j = LEN - 1; j >= 1; j--)
20
{
21
if(a[j] < a[j - 1])
22
{
23
t = a[j];
24
a[j] = a[j - 1];
25
a[j - 1] = t;
26
}
27
}
28
}
29
for(n = 0; n < LEN; n++)
30
printf("%d,", a[n]);
31
32
getchar();
33
}
34
int RandNumber(){
35
return rand()*10000/RAND_MAX;
36
}
37
#include "stdio.h"2
#include "stdlib.h"3
#include "time.h"4

5
#define LEN 106

7
int RandNumber();8
void main(){9
int a[LEN],n,j,t;10
srand(time(NULL));11
for(n = 0; n < LEN; n++)12
{13
a[n] = RandNumber();14
printf("%d,", a[n]);15
}16
printf("\n");17
for(n = 0; n < LEN - 1; n++)18
{19
for(j = LEN - 1; j >= 1; j--)20
{21
if(a[j] < a[j - 1])22
{23
t = a[j];24
a[j] = a[j - 1];25
a[j - 1] = t;26
}27
}28
}29
for(n = 0; n < LEN; n++)30
printf("%d,", a[n]);31

32
getchar();33
}34
int RandNumber(){35
return rand()*10000/RAND_MAX;36
}37

选择法
1
#include "stdio.h"
2
#include "stdlib.h"
3
#include "time.h"
4
5
#define LEN 10
6
7
int RandNumber();
8
void main(){
9
int a[LEN],n,j,t,theMinIndex;
10
srand(time(NULL));
11
for(n = 0; n < LEN; n++)
12
{
13
a[n] = RandNumber();
14
printf("%d,", a[n]);
15
}
16
printf("\n");
17
for(n = 0; n < LEN - 1; n++)
18
{
19
theMinIndex = n;
20
for(j = LEN - 1; j > n; j--)
21
if(a[j] < a[theMinIndex])
22
theMinIndex = j;
23
if(theMinIndex != n)
24
{
25
t = a[n];
26
a[n] = a[theMinIndex];
27
a[theMinIndex] = t;
28
}
29
}
30
for(n = 0; n < LEN; n++)
31
printf("%d,", a[n]);
32
33
getchar();
34
}
35
int RandNumber(){
36
return rand()*10000/RAND_MAX;
37
}
38
#include "stdio.h"2
#include "stdlib.h"3
#include "time.h"4

5
#define LEN 106

7
int RandNumber();8
void main(){9
int a[LEN],n,j,t,theMinIndex;10
srand(time(NULL));11
for(n = 0; n < LEN; n++)12
{13
a[n] = RandNumber();14
printf("%d,", a[n]);15
}16
printf("\n");17
for(n = 0; n < LEN - 1; n++)18
{19
theMinIndex = n;20
for(j = LEN - 1; j > n; j--)21
if(a[j] < a[theMinIndex])22
theMinIndex = j;23
if(theMinIndex != n)24
{25
t = a[n];26
a[n] = a[theMinIndex];27
a[theMinIndex] = t;28
}29
}30
for(n = 0; n < LEN; n++)31
printf("%d,", a[n]);32

33
getchar();34
}35
int RandNumber(){36
return rand()*10000/RAND_MAX;37
}38

希尔排序
1
#include "stdio.h"
2
#include "stdlib.h"
3
#include "time.h"
4
5
#define LEN 15
6
7
int RandNumber();
8
void main(){
9
int a[LEN],n,j,t,s,b;
10
srand(time(NULL));
11
for(n = 0; n < LEN; n++)
12
{
13
a[n] = RandNumber();
14
printf("%d,", a[n]);
15
}
16
printf("\n");
17
for(n = LEN/2; n >= 1; n = n /2)
18
{
19
for(j = 0; j < n; j++)
20
{
21
for(s = n; s < LEN; s += n)
22
{
23
b = s;
24
while(b >= n && a[b] < a[b - n])
25
{
26
t = a[b];
27
a[b] = a[b - n];
28
a[b - n] = t;
29
b -= n;
30
}
31
}
32
}
33
}
34
for(n = 0; n < LEN; n++)
35
printf("%d,", a[n]);
36
37
getchar();
38
}
39
int RandNumber(){
40
return rand()*10000/RAND_MAX;
41
}
42
#include "stdio.h"2
#include "stdlib.h"3
#include "time.h"4

5
#define LEN 156

7
int RandNumber();8
void main(){9
int a[LEN],n,j,t,s,b;10
srand(time(NULL));11
for(n = 0; n < LEN; n++)12
{13
a[n] = RandNumber();14
printf("%d,", a[n]);15
}16
printf("\n");17
for(n = LEN/2; n >= 1; n = n /2)18
{19
for(j = 0; j < n; j++)20
{21
for(s = n; s < LEN; s += n)22
{23
b = s;24
while(b >= n && a[b] < a[b - n])25
{26
t = a[b];27
a[b] = a[b - n];28
a[b - n] = t;29
b -= n;30
}31
}32
}33
}34
for(n = 0; n < LEN; n++)35
printf("%d,", a[n]);36

37
getchar();38
}39
int RandNumber(){40
return rand()*10000/RAND_MAX;41
}42

来自张子阳大哥的教程..此为学习笔记
http://www.cnblogs.com/JimmyZhang/archive/2008/10/02/1303137.html


浙公网安备 33010602011771号