哪位帮忙写下冒泡法和quicksort法的排序代码,谢了
1. 问题描述:将一组无序数列按一定规律排序。
2. 问题的解决方案:
根据问题的描述,可以按照要求的功能采用结构化的设计思想。
(1) 数列的赋值要求用函数实现。
(2) 使用“冒泡法”进行排序,用函数实现并统计排序次数。
(3) 使用“quicksort法”进行排序,用函数实现并统计排序次数。
(4) 比较以上两种排序方法的优劣。
按大小顺序排列,用大一学的知识写,谢谢各位了
2010-05-29 09:02
程序代码:/*qsort用法*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define LEN 10
void fillarray(double a[], int n);
void showarray(double a[], int n);
int mycomp(const void *p1, const void *p2);
int main(void)
{
double a[LEN];
fillarray(a, LEN);
puts("排序前数字清单:");
showarray(a, LEN);
qsort(a, LEN, sizeof(double), mycomp);
puts("排序后数字清单:");
showarray(a, LEN);
return 0;
}
void fillarray(double a[], int n)
{
int index;
srand((unsigned)time(NULL));
for (index = 0; index < n; index++)
{
a[index] = (double)(rand()/(rand()+0.1));
}
}
void showarray(double a[], int n)
{
int index;
for (index = 0; index < n; index++)
{
printf("%12.4f",a[index]);
if (index % 6 == 5)
{
putchar('\n');
}
}
if (index % 6 != 0)
{
putchar('\n');
}
}
int mycomp(const void *p1, const void *p2)
{
const double * a = (const double *)p1;
const double * b = (const double *)p2;
if (*a < *b)
return -1;
else if (*a == *b)
return 0;
else
return 1;
}冒泡就不写了
2010-05-29 09:19
2010-05-29 09:21