用C语言写个完整程序,包括希尔排序和快速排序

包括头文件和主函数

这两段程序我都调试过,没问题。

这是希尔排序C程序,输入整形数组,NUM为数组长度,nStep为步长个数,STEP为步长数组:
/*code by jgao*/
#include<stdio.h>
#define nStep 5 /*num of step*/
#define NUM 30 /*num of toBeSorted array*/

void shellSort(int[], int); /*function*/
void printS(int[], int); /*print array*/

static int times = 0; /*counter*/
int STEP[nStep] = {15, 8, 4, 2, 1}; /*step array*/

int main(){
int s[NUM] = {813, 87, 365, 621, 488, 901, 237, 551, 686, 134,
4, 765, 342, 145, 962, 451, 288, 552, 682, 34,
88, 552, 98, 532, 881, 183, 248, 672, 978, -43}; /*toBeSorted array*/
printf("Input: \n");
printS(s, NUM);
shellSort(s, NUM); /*sort*/
printf("\nOutput: \n");
printS(s, NUM);
return 0;
}
void shellSort(int s[], int n){
int i, j, k;
for(i = 0; i < nStep; i ++){
int step = STEP[i];
printf("\n");
for(j = 0; j < step; j ++){
/* find every array */
printf("%d exchange: (", times ++);
for(k = 0; k < (int)(n/step); k ++){
int l, m, index = j + k*step;
printf("%d ", index);
if(!k)continue;
for(l = 0; l < k; l ++){
int index2 = j + l*step;
if(s[index] < s[index2])
break;
}
if(l >= k)
continue;
else{
int tmp = s[index];
m = k;
while(m > l){
s[j + m*step] = s[j + (m-1)*step];
m --;
};
s[j + l*step]= tmp;
}
}
printf(")\n");
printS(s, n);
}
}
}
void printS(int s[], int n){
int i;
for(i = 0; i < n; i ++){
printf("%d ", s[i]);
}
printf("\n");
}

希尔排序程序运行结果如下:
Input:
813 87 365 621 488 901 237 551 686 134 4 765 342 145 962 451 288 552 682 34 88 552 98 532 881 183 248 672 978 -43

0 exchange: (0 15 )
451 87 365 621 488 901 237 551 686 134 4 765 342 145 962 813 288 552 682 34 88 552 98 532 881 183 248 672 978 -43
1 exchange: (1 16 )
451 87 365 621 488 901 237 551 686 134 4 765 342 145 962 813 288 552 682 34 88 552 98 532 881 183 248 672 978 -43
2 exchange: (2 17 )
451 87 365 621 488 901 237 551 686 134 4 765 342 145 962 813 288 552 682 34 88 552 98 532 881 183 248 672 978 -43
3 exchange: (3 18 )
451 87 365 621 488 901 237 551 686 134 4 765 342 145 962 813 288 552 682 34 88 552 98 532 881 183 248 672 978 -43
4 exchange: (4 19 )
451 87 365 621 34 901 237 551 686 134 4 765 342 145 962 813 288 552 682 488 88 552 98 532 881 183 248 672 978 -43
5 exchange: (5 20 )
451 87 365 621 34 88 237 551 686 134 4 765 342 145 962 813 288 552 682 488 901 552 98 532 881 183 248 672 978 -43
6 exchange: (6 21 )
451 87 365 621 34 88 237 551 686 134 4 765 342 145 962 813 288 552 682 488 901 552 98 532 881 183 248 672 978 -43
7 exchange: (7 22 )
451 87 365 621 34 88 237 98 686 134 4 765 342 145 962 813 288 552 682 488 901 552 551 532 881 183 248 672 978 -43
8 exchange: (8 23 )
451 87 365 621 34 88 237 98 532 134 4 765 342 145 962 813 288 552 682 488 901 552 551 686 881 183 248 672 978 -43
9 exchange: (9 24 )
451 87 365 621 34 88 237 98 532 134 4 765 342 145 962 813 288 552 682 488 901 552 551 686 881 183 248 672 978 -43
10 exchange: (10 25 )
451 87 365 621 34 88 237 98 532 134 4 765 342 145 962 813 288 552 682 488 901 552 551 686 881 183 248 672 978 -43
11 exchange: (11 26 )
451 87 365 621 34 88 237 98 532 134 4 248 342 145 962 813 288 552 682 488 901 552 551 686 881 183 765 672 978 -43
12 exchange: (12 27 )
451 87 365 621 34 88 237 98 532 134 4 248 342 145 962 813 288 552 682 488 901 552 551 686 881 183 765 672 978 -43
13 exchange: (13 28 )
451 87 365 621 34 88 237 98 532 134 4 248 342 145 962 813 288 552 682 488 901 552 551 686 881 183 765 672 978 -43
14 exchange: (14 29 )
451 87 365 621 34 88 237 98 532 134 4 248 342 145 -43 813 288 552 682 488 901 552 551 686 881 183 765 672 978 962

15 exchange: (0 8 16 )
288 87 365 621 34 88 237 98 451 134 4 248 342 145 -43 813 532 552 682 488 901 552 551 686 881 183 765 672 978 962
16 exchange: (1 9 17 )
288 87 365 621 34 88 237 98 451 134 4 248 342 145 -43 813 532 552 682 488 901 552 551 686 881 183 765 672 978 962
17 exchange: (2 10 18 )
288 87 4 621 34 88 237 98 451 134 365 248 342 145 -43 813 532 552 682 488 901 552 551 686 881 183 765 672 978 962
18 exchange: (3 11 19 )
288 87 4 248 34 88 237 98 451 134 365 488 342 145 -43 813 532 552 682 621 901 552 551 686 881 183 765 672 978 962
19 exchange: (4 12 20 )
288 87 4 248 34 88 237 98 451 134 365 488 342 145 -43 813 532 552 682 621 901 552 551 686 881 183 765 672 978 962
20 exchange: (5 13 21 )
288 87 4 248 34 88 237 98 451 134 365 488 342 145 -43 813 532 552 682 621 901 552 551 686 881 183 765 672 978 962
21 exchange: (6 14 22 )
288 87 4 248 34 88 -43 98 451 134 365 488 342 145 237 813 532 552 682 621 901 552 551 686 881 183 765 672 978 962
22 exchange: (7 15 23 )
288 87 4 248 34 88 -43 98 451 134 365 488 342 145 237 686 532 552 682 621 901 552 551 813 881 183 765 672 978 962

23 exchange: (0 4 8 12 16 20 24 )
34 87 4 248 288 88 -43 98 342 134 365 488 451 145 237 686 532 552 682 621 881 552 551 813 901 183 765 672 978 962
24 exchange: (1 5 9 13 17 21 25 )
34 87 4 248 288 88 -43 98 342 134 365 488 451 145 237 686 532 183 682 621 881 552 551 813 901 552 765 672 978 962
25 exchange: (2 6 10 14 18 22 26 )
34 87 -43 248 288 88 4 98 342 134 237 488 451 145 365 686 532 183 551 621 881 552 682 813 901 552 765 672 978 962
26 exchange: (3 7 11 15 19 23 27 )
34 87 -43 98 288 88 4 248 342 134 237 488 451 145 365 621 532 183 551 672 881 552 682 686 901 552 765 813 978 962

27 exchange: (0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 )
-43 87 4 98 34 88 237 248 288 134 342 488 365 145 451 621 532 183 551 672 682 552 765 686 881 552 901 813 978 962
28 exchange: (1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 )
-43 87 4 88 34 98 237 134 288 145 342 183 365 248 451 488 532 552 551 552 682 621 765 672 881 686 901 813 978 962

29 exchange: (0 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 )
-43 4 34 87 88 98 134 145 183 237 248 288 342 365 451 488 532 551 552 552 621 672 682 686 765 813 881 901 962 978

Output:
-43 4 34 87 88 98 134 145 183 237 248 288 342 365 451 488 532 551 552 552 621 672 682 686 765 813 881 901 962 978
Press any key to continue

这是第二个:快速排序算法C程序:
/*code by jgao,递归快速排序算法,输入为字符数组*/
#include<stdio.h>
void main(){
int quickSort(char vert[], int n, int begin, int end);
char vert[] = "qwertyuiopasdfghjklzxcvbnm";
int n = 26;
quickSort(vert, n, 0, n-1);
}

int quickSort(char vert[], int n, int begin, int end){
int print(char* vert, int n, int base);
int front = begin, back = end;
char base = vert[front];

if(begin >= end)return 0;
else{
do{
while(back > front){
if(vert[back] < base)break;
else back --;
};
if(back > front)vert[front ++] = vert[back];
while(front < back){
if(vert[front] > base)break;
else front ++;
};
if(back > front)vert[back --] = vert[front];
}while(front < back);
vert[back] = base;

print(vert, n, base);
quickSort(vert, n, begin, front - 1);
quickSort(vert, n, back + 1, end);

return 1;
}
}

int print(char* vert, int n, int base){
int i;
printf("(%c):\t", base);
for(i = 0; i < n; i ++){
printf("%c ",vert[i]);
}
printf("\n");
return 0;
}
输出结果如下:
(q): m n e b c l k i o p a j d f g h q s u z x y v t r w
(m): h g e b c l k i f d a j m p o n q s u z x y v t r w
(h): a g e b c d f h i k l j m p o n q s u z x y v t r w
(a): a g e b c d f h i k l j m p o n q s u z x y v t r w
(g): a f e b c d g h i k l j m p o n q s u z x y v t r w
(f): a d e b c f g h i k l j m p o n q s u z x y v t r w
(d): a c b d e f g h i k l j m p o n q s u z x y v t r w
(c): a b c d e f g h i k l j m p o n q s u z x y v t r w
(i): a b c d e f g h i k l j m p o n q s u z x y v t r w
(k): a b c d e f g h i j k l m p o n q s u z x y v t r w
(p): a b c d e f g h i j k l m n o p q s u z x y v t r w
(n): a b c d e f g h i j k l m n o p q s u z x y v t r w
(s): a b c d e f g h i j k l m n o p q r s z x y v t u w
(z): a b c d e f g h i j k l m n o p q r s w x y v t u z
(w): a b c d e f g h i j k l m n o p q r s u t v w y x z
(u): a b c d e f g h i j k l m n o p q r s t u v w y x z
(y): a b c d e f g h i j k l m n o p q r s t u v w x y z
Press any key to continue
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-12-29
(q): m n e b c l k i o p a j d f g h q s u z x y v t r w
(m): h g e b c l k i f d a j m p o n q s u z x y v t r w
(h): a g e b c d f h i k l j m p o n q s u z x y v t r w
(a): a g e b c d f h i k l j m p o n q s u z x y v t r w
(g): a f e b c d g h i k l j m p o n q s u z x y v t r w
(f): a d e b c f g h i k l j m p o n q s u z x y v t r w
(d): a c b d e f g h i k l j m p o n q s u z x y v t r w
(c): a b c d e f g h i k l j m p o n q s u z x y v t r w
(i): a b c d e f g h i k l j m p o n q s u z x y v t r w
(k): a b c d e f g h i j k l m p o n q s u z x y v t r w
(p): a b c d e f g h i j k l m n o p q s u z x y v t r w
(n): a b c d e f g h i j k l m n o p q s u z x y v t r w
(s): a b c d e f g h i j k l m n o p q r s z x y v t u w
(z): a b c d e f g h i j k l m n o p q r s w x y v t u z
(w): a b c d e f g h i j k l m n o p q r s u t v w y x z
(u): a b c d e f g h i j k l m n o p q r s t u v w y x z
(y): a b c d e f g h i j k l m n o p q r s t u v w x y z
Press any key to continue