RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
c语言快速排列函数 c语言实现快速排序算法

c语言快速排序qsort函数

你这个是c程序还是c++

创新互联专业为企业提供平城网站建设、平城做网站、平城网站设计、平城网站制作等企业网站建设、网页设计与制作、平城企业网站模板建站服务,10多年平城做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

c++的话类型要求比较严格

比较函数接受的参数类型应该是const void*

c的话你main里函数原型的声明也不对啊,这样改一下

#includestdlib.h

#includestdio.h

#include ctype.h

int num[]={125,-26,53,12,-6,95,46,85,-45,785};/*定义全局数组*/

void main()

{

int i,comp1(const void *i,const void *j),comp2(const void *i,const void *j);

system("cls");/*清屏*/

printf("the original array is:\n");

for(i=0;i10;i++)/*将数组按原序输出*/

printf("%10d",num[i]);

qsort(num,10,sizeof(int),comp1);

printf("\n The accending sorted array is:\n");

for(i=0;i10;i++)/*将数组按升序输出*/

printf("%10d",num[i]);

qsort(num,10,sizeof(int),comp2);

printf("\n The decending sorted array is:\n");

for(i=0;i10;i++)/*将数组按降序输出*/

printf("%10d",num[i]);

getchar();

}

comp1(const void *i,const void *j)

{

return *(const int*)i-*(const int*)j;

}

comp2(const void *i,const void *j)

{

return *(const int*)j-*(const int*)i;

}

C语言快速排序函数怎么调用

你可以看看这个例子:

#include stdio.h

#include stdlib.h

int list[5] = {7,5,9,2,6};

int sort_function( const void *a, const void *b);

int main(void)

{

int x;

qsort((void *)list, 5, sizeof(int), sort_function);

for (x = 0; x 5; x++)

printf("%d\\n", list[x]);

return 0;

}

int sort_function( const void *a, const void *b)

{

if(*(int*)a*(int*)b)

return 1;

else if(*(int*)a*(int*)b)

return -1;

else

return 0;

}

C语言sort函数如何使用

C语言中没有预置的sort函数。如果在C语言中,遇到有调用sort函数,就是自定义的一个函数,功能一般用于排序。

一、可以编写自己的sort函数。

如下函数为将整型数组从小到大排序。

void sort(int *a, int l)//a为数组地址,l为数组长度。

{

int i, j;

int v;

//排序主体

for(i = 0; i  l - 1; i ++)

for(j = i+1; j  l; j ++)

{

if(a[i]  a[j])//如前面的比后面的大,则交换。

{

v = a[i];

a[i] = a[j];

a[j] = v;

}

}}

对于这样的自定义sort函数,可以按照定义的规范来调用。

二、C语言有自有的qsort函数。

功 能: 使用快速排序例程进行排序

头文件:stdlib.h

原型: void qsort(void *base,int nelem,int width,int (*fcmp)(const void *,const void *));

参数:

1 待排序数组首地址

2 数组中待排序元素数量

3 各元素的占用空间大小

4 指向函数的指针,用于确定排序的顺序

这个函数必须要自己写比较函数,即使要排序的元素是int,float一类的C语言基础类型。

以下是qsort的一个例子:

#includestdio.h

#includestdlib.h

int comp(const void*a,const void*b)//用来做比较的函数。

{

return *(int*)a-*(int*)b;

}

int main()

{

int a[10] = {2,4,1,5,5,3,7,4,1,5};//乱序的数组。

int i;

qsort(a,n,sizeof(int),comp);//调用qsort排序

for(i=0;i10;i++)//输出排序后的数组

{

printf("%d\t",array[i]);

}

return 0;

}

扩展资料:

sort函数的用法(C++排序库函数的调用)

对数组进行排序,在c++中有库函数帮我们实现,这们就不需要我们自己来编程进行排序了。

(一)为什么要用c++标准库里的排序函数

Sort()函数是c++一种排序方法之一,学会了这种方法也打消我学习c++以来使用的冒泡排序和选择排序所带来的执行效率不高的问题!因为它使用的排序方法是类似于快排的方法,时间复杂度为n*log2(n),执行效率较高!

(二)c++标准库里的排序函数的使用方法

I)Sort函数包含在头文件为#includealgorithm的c++标准库中,调用标准库里的排序方法可以不必知道其内部是如何实现的,只要出现我们想要的结果即可!

II)Sort函数有三个参数:

(1)第一个是要排序的数组的起始地址。

(2)第二个是结束的地址(最后一位要排序的地址的下一地址)

(3)第三个参数是排序的方法,可以是从大到小也可是从小到大,还可以不写第三个参数,此时默认的排序方法是从小到大排序。

Sort函数使用模板:

Sort(start,end,排序方法)

下面就具体使用sort()函数结合对数组里的十个数进行排序做一个说明!

例一:sort函数没有第三个参数,实现的是从小到大

#includeiostream

#includealgorithm

using namespace std;

int main()

{

int a[10]={9,6,3,8,5,2,7,4,1,0};

for(int i=0;i10;i++)

couta[i]endl;

sort(a,a+11);

for(int i=0;i10;i++)

couta[i]endl;

return 0;

}

编译器

GCC,GNU组织开发的开源免费的编译器

MinGW,Windows操作系统下的GCC

Clang,开源的BSD协议的基于LLVM的编译器

Visual C++ :: cl.exe,Microsoft VC++自带的编译器

集成开发环境

CodeBlocks,开源免费的C/C++ IDE

CodeLite,开源、跨平台的C/C++集成开发环境

Orwell Dev-C++,可移植的C/C++IDE

C-Free

Light Table

Visual Studio系列

Hello World

参考资料:百度百科-sort函数

C语言字符串快速排序函数

#include stdio.h

#includestdlib.h

#includestring.h

int comp(char *a,char *b)

{

while(*a==*b*a*b){a++;b++;}

return (int)*a-(int)*b;

}

int main(void)

{

char s[1000][20];

int i,n;

scanf("%d\n",n);

for(i=0;in;i++)

gets(s[i]);

qsort(s,n,sizeof(s[0]),comp);

printf("\n");

for(i=0;in;i++)

puts(s[i]);

system("pause");

return 0;

}

用c语言编写函数QuickSort()来实现快速排序

#include stdlib.h

#include stdio.h

#define MAXN 8

#define MOD 1024

void QuickSort(int *arr, int low, int high)

{

if (low = high) return;

//保存排序区间的 起始位置和终点位置

int left = low, right = high;

//默认 左边第一个元素 为标志

int key = arr[low];

while (low  high)

{

while (low  high  arr[high] = key) --high;

arr[low] = arr[high];

while (low  high  arr[low] = key) ++low;

arr[high] = arr[low];

}

arr[low] = key;

//每次排序后都分成两部分[left, low) (low, right]

//arr[low]的位置是一定是有序的

QuickSort(arr, left, low - 1);

QuickSort(arr, low + 1, right);

return;

}

int main(void)

{

int n;

scanf("%d", n);

int arr[MAXN] = {0};

int i;

for (i = 0; i  n; ++i)

scanf("%d", arr[i]);

//输入是默认为生活中习惯的数组左边第一个为:编号1

int s, m;

scanf("%d %d", s, m);

//转成计算机数组第一个为:编号0

s--; m--;

//快排

QuickSort(arr, s, m);

//输出

for (i = s; i = m; ++i)

{

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

}

return 0;

}

//测试数据

//8

//1 2 3 4 5 6 7 8

//2 6

输出 6 5 4 3 2

C语言 快排函数

函数kuaipai1 进入了无限死循环。

递归函数没有一个节点判定递归结束,导致进入死循环

系统堆栈用完,程序崩溃。

程序调试报告有无限死循环危险,运行后就直接崩溃,导致栈溢出。


网站标题:c语言快速排列函数 c语言实现快速排序算法
分享路径:http://scyingshan.cn/article/dodjisp.html