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

作者&投稿:钮郝 (若有异议请与网页底部的电邮联系)
c语言中,希尔排序程序中的问题,运行不对,是什么原因?~

printf("
排完序后的数字顺序:");
for(i=1;i<=10;i++);
应该是上面这语句多了一个分号。
求采纳

以下是完整的代码:希望对楼主有所帮助~~
#include
#include

int Partition (int a[], int low, int high)
{
int key;
key = a[low];
while(low < high)
{
while(low = key)
--high;
a[low] = a[high];
while(low < high && a[low] <= key)
++low;
a[high] = a[low];
}
a[low] = key;
return low;
}

void QSort(int a[], int low, int high)
{
if( low < high)
{
int i;
i = Partition( a, low, high);
QSort( a, low, i-1);
QSort( a, i+1, high);
}
}

int main(void)
{
int i;
static int a[12]={13,19,9,5,12,8,7,4,11,2,6,21};

printf(

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

这是希尔排序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

(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


编写一个c语言程序,要求输入圆的半径r,圆柱高h,求圆的周长、面积、体积...
可以先定义圆周率pi为3.1415926,再定义双精度变量半径r、高h、周长、面积、体积,输入相关数据后计算输出结果即可,实现该功能程序多样并不唯一,具体程序如下。include <stdio.h> void main(){ double pi=3.1415926;double r,h;double c,area,v;printf("输入圆的半径及圆柱的高:");scanf("%lf%...

求一个C语言的程序代码。完整的
刚编了一个:把12枚银币编号,1,2,3,...12,每次称重的时候,按照程序提示进行,输入称重结果,就可以了。运行结果:5、6、7、8 比 1、2、3、4:(输入:0等,1轻,2重)?2 3、4、6 比 1、2、5:(输入0等,1轻,2重)?1 5、4 比 11、12 (输入0:等,1轻,2重)?2 假币5重 源程序...

C语言编写一个程序,实现如下功能:从键盘输入一个三位数,求各位数字之...
include "stdio.h"void main(){ int n,sum=0;printf("请输入一个三位数:");scanf("%d",&n);sum=n\/100+n%100\/10+n%10;\/\/百位数+十位数+个位数 printf("这个三位数各位数字之和是%d\\n",sum);} 结果:

用c语言编写一个程序。
“该单词的后面紧跟着再次出现自己本身”怎么理解?大体说说思路,你得有个算法判断什么样的字符串算一个“单词”,可根据ASCII码,单词中可以有大小写字母,空格(\\r,\\t,\\n),其他字符(&、*、……)都可以分隔单词。输入一个字符串,程序算法切割为“单词”,存入链表或者数据库,再读入之后查询...

谁能用C语言编个完整的程序求表达式的值,例如3*(7-2)。很急!!!谢谢了...
include <stdio.h> include <stdlib.h> include <string.h> define TRUE 1 define FALSE 0 define LEN 10\/\/输入数字不得超过10位 define MAXSIZE 40\/\/数字和运算符总个数不得超过40个 typedef struct { char data[MAXSIZE][LEN];\/\/栈区为二维数组 int top;}seqstack;seqstack *init...

求几个简单的C语言小程序
return (a*b)\/c;} void main(){ int a,b;cout<<"请按从大到小的顺序输入2个要求值的数"<<endl;cin>>a>>b;cout<<"两个数的最大公约数是"<<yue(a,b)<<endl;cout<<"两个数的最小公倍数是"<<bei(a,b,yue(a,b))<<endl;} \/\/求最大公约数程序2 include <stdio.h> int ...

帮忙写个简单初级大学C语言程序!!谢谢
include <stdio.h> define N 26 int main(){ int n=N+1;while(n>N) scanf("%d",&n);for(int i=0,j=0;i<n;i++,j++){ int k=0;char ch='A';for(;k<n;k++){ if(k>=n-j) printf("%c",ch++);else printf(" ");} for(;k<n*2;k++){ if(k<=n+j) printf(...

开发一个c语言程序要经过哪四个步骤
开发一个C语言程序需要经过的四个步骤:编辑、编译、连接、运行。C语言程序可以使用在任意架构的处理器上,只要那种架构的处理器具有对应的C语言编译器和库,然后将C源代码编译、连接成目标二进制文件之后即可运行。1、预处理:输入源程序并保存(.C文件)。2、编译:将源程序翻译为目标文件(.OBJ文件)。...

请写出一个完整的C语言程序,该程序运行后,可以在屏幕上把你的名字显示1...
include <stdio.h> include<string.h> main(){ char name[20];int i;printf("请输入你的名字:\\n");gets(name);for(i=0;i<1000;i++)\/\/循环打印100次名字 printf("%s\\n",name);return 0;}

C语言 编写一个程序,输入一个正整数,求出它是几位数。
求一个正整数n的位数可以先定义一个变量num,并初始化为0,依次把该整数n除以10,直到其为0为止,并且每除一次10,变量num的个数就自加1,最后num的值就是该整数n的位数。include <stdio.h> int main(){ int n,num=0;scanf("%d",&n);while(n){ num++;n\/=10;} printf("%d\\n",num)...

锦州市17369065721: 用C语言写希尔排序
滕南单硝: #include<stdio.h>#include<conio.h>void main(){int a[30],i=0,j,x,n,gap;printf("希尔排序法,请输入数据,以-1结束\n");for(i=0;i<30;i++){scanf("%d",a+i);if(a[i]==-1) break;}n=i;gap=n/2;while(gap>0){for(i=gap;i<n;i++){j=i-gap;while(j>=0){if(a[j]>...

锦州市17369065721: 求希尔排序用C语言实现
滕南单硝:#include<stdio.h>void shellsort(int a[],int n){int i;int b; int d; for(d=5;d>=1;d--) //我的增量是5,4,3,2,1,你可以自己改 {for(i=0;i+d<n;i=i+d) if(a[i]>a[i+d]) {b=a[i]; a[i]=a[i+d]; a[i+d]=b;} } }void main() { int i; int a[]={5,6,2,1,3,4,7}; shellsort(a,7); for(i=0;i<7;...

锦州市17369065721: 急求希尔排序完整程序C语言版的 -
滕南单硝: #include <stdio.h> void swap(int a[], int i, int j) { int t = a[i]; a[i] = a[j]; a[j] = t; } void selsort(int a[], int n, int incr) { int i, j; for (i = incr; i < n; i += incr) for (j = i; (j >= incr)&& (a[j] < a[j-incr]); j -= incr) swap(a, j, j-incr); } void shellsort(int a[], int n) { int i, j; for (i =...

锦州市17369065721: 希尔排序如何用C语言算法实现呢?谢谢@ -
滕南单硝: void ShellSort(double *a, int n)//希尔排序 { int i = n, j; bool s; while (i > 1) { i = i / 2; do { for ( s = false, j = 0; j < n - i; j ++ ) { if (a[ j + i ] < a[ j ]) { Swap(a[ j ], a[ j + i ]); s = true; } } } while ( s ); } }

锦州市17369065721: 数据结构的完整程序(C语言版),包含希尔排序和快速排序 -
滕南单硝: /* 源程序的头文件 sort.h */ void getrandat(Data ary[],int count) /* 产生一批随机数,准备排序 */ { long int a=100001; int i; for(i=0;i<count;i++){ a=(a*125)%2796203; ary[i].key=(int)a; } } /* getrandat */ void prdata(Data ary[],int count) /* 输出数据的函数 ...

锦州市17369065721: 用c语言编写一个希尔排序程序,新手,最好能给注释下!谢谢 -
滕南单硝: 网友wang1992092对希尔排序的理解有些错误,希尔排序对每个子序列进行的是直接插入排序,而不是如他所给出的选择排序.你可以先百度一下希尔排序的定义.我这里给一个C源代码,你可以试试.直接插入排序的思路是:将待排表分成两...

锦州市17369065721: 求算法高手,用希尔排序,插入,选择,冒泡四种排序方法编写成一个程序,用c 语言描述的 -
滕南单硝: #include<stdio.h>#include<time.h>#include<math.h>#include<malloc.h> void BubbleSort(int *L,int N) { //冒泡int i,j; int t; for(i=1;i<=N;i++) { for(j=N;j>i;j--) if(L[j]<L[j-1]) { t=L[j]; L[j]=L[j-1]; L[j-1]=t; } } } int SelectMinKey(int *L,int N,int n) { int i,min=n; for(i=n...

锦州市17369065721: c语言希尔排序 -
滕南单硝: void ShellInsertSort(int a[], int n, int dk) {for(int i= dk; i if(a[i]int j = i-dk;int x = a[i]; //复制为哨兵,即存储待排序元素a[i] = a[i-dk]; //首先后移一个元素while(xa[j+dk] = a[j];j -= dk; //元素后移}a[j+dk] = x; //插入到正确位置}} } /*** 先按增...

锦州市17369065721: 谁能帮我用C语言写个程序,分别使用希尔排序,快速排序和堆排序对一组数进行排序. -
滕南单硝: #include<iostream> using namespace std;#include<cstdlib>#include<ctime>#include<windows.h>#include<iomanip> template<class T> void Bubblesort(T *Array,int n) { bool Noswap; T temp; for(int i=0;i<n;i++) { Noswap=true; for(int j=n-1;j>i;j--) { if(...

锦州市17369065721: 这是用C语言实现的希尔排序,求大神指教? -
滕南单硝: while 与 do的关系很乱,觉得你这样的编程习惯很不好哦

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网