qsort

王朝百科·作者佚名  2010-02-14  
宽屏版  字体: |||超大  

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

用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *));

各参数:1 待排序数组首地址 2 数组中待排序元素数量 3 各元素的占用空间大小 4 指向函数的指针,用于确定排序的顺序

程序例:

#include <iostream>

using namespace std;

#include <stdlib.h>

#include <string.h>

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

char * list[5]= {"cat","car","cab","cap","can"};

int main()

pascal 例程

program quicksort;

const

max = 100000;

max = 1000;

type

tlist = array[1..max] of longint;

var

data : tlist;

i : longint;

procedure qsort(var a : tlist);

procedure sort(l,r: longint);

var i,j,x,y: longint;

begin

i:=l; j:=r;

x:=a[(l+r) div 2];

repeat

while a[i]<x do inc(i);

while x<a[j] do dec(j);

if i<=j then

begin

y:=a[i];a[i]:=a[j];a[j]:=y;

inc(i);dec(j);

end;

until i>j;

if l<j then sort(l,j);

if i<r then sort(i,r);

end;

begin

sort(1,max);

end;

begin

write('Creating ',Max,' random numbers between 1 and 500000');

randomize;

for i:=1 to max do

data:=random(500000);

writeln;

writeln('Sorting...');

qsort(data);

writeln;

for i:=1 to max do

begin

write(data:7);

if (i mod 10)=0 then

writeln;

end;

end.

c/c++

c函数qsort()和bsearch()的用法

使用qsort()排序 并 用 bsearch()搜索是一个比较常用的组合,使用方便快捷。

qsort 的函数原型是void __cdecl qsort ( void *base, size_t num, size_t width, int (__cdecl *comp)(const void *, const void* ) )

其中base是排序的一个集合数组,num是这个数组元素的个数,width是一个元素的大小,comp是一个比较函数。

比如:对一个长为1000的数组进行排序时,int a[1000]; 那么base应为a,num应为 1000,width应为 sizeof(int),comp函数随自己的命名。

qsort(a,1000,sizeof(int ),comp);

其中comp函数应写为:

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

{

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

}

是对一个二维数组的进行排序:

int a[1000][2]; 其中按照a[0]的大小进行一个整体的排序,其中a[1]必须和a[0]一起移动交换。

qsort(a,1000,sizeof(int)*2,comp);

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

{

return ((int *)a)[0]-((int *)b)[0];

}

对字符串进行一个排序:

char a[1000][20];

qsort(a,1000,sizeof(char)*20,comp);

int comp(const void *a,const void *b

{

return strcmp((char *)a,(char *)b);

}

对一个结构体进行排序:

typedef struct str

{

char str1[11];

char str2[11];

}str,*stri;

str strin[100001]=;

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

{

return strcmp( ((str*)a)->str2 , ((str*)b)->str2 );

}

qsort(strin,total,sizeof(str),compare);

程序例:

#include<iostream.h>

#include<stdlib.h>

#include<string.h>

#define N 8

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

void main()

{

char s[8][10]={"January","February","March","April","May","June","July","September"};

int i;

qsort(s,8,sizeof(char)*10,compare);

for(i=0;i<N;i++)

{

for(j=0;j<10;j++)

cout<<s[j];

cout<<endl;

}

}

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

{

if(strlen((char *)a)!=strlen((char *)b))

return strlen((char *)a)-strlen((char*)b);

return (strcmp((char *)a,(char *)b));

}//vc++ 6.0

// VS2008编译通过,具有代表性的例子

#include <stdlib.h>

#include <stdio.h>

#include <string.h>

int compare(const void *arg1,const void *arg2);

int main(int argc,char **argv)

{

int i;

argv++;

argc--;

qsort((void *)argv,(size_t)argc,sizeof(char *),compare);

for(i=0;i<argc;++i)

{

printf("%s ",argv);

printf("

");

}

}

int compare(const void *arg1,const void *arg2)

{

return _stricmp(*(char **)arg1,*(char **)arg2);

}

在运行输入cmd,在qsort.exe 参数1 参数2

将会排序

下面讲解下Pascal的快排代码

program kuaipai;

var

save:array[-1..10000000]of longint;//保存数字的数组

n,i:longint;

procedure qsort(x,y:longint);

var

a,b,c,em,d,mid,e,i,j,k,l:longint;

begin

i:=x;//i代表第一个数字的数组坐标,下面叫“左指针”

j:=y;//j代表第二个数字的数组坐标 叫"右指针"

mid:=save[(x+y)div 2];//取,这2个数字中间的数组坐标(二分)

repeat

while save[i]<mid do inc(i); //在中间这个数字的左边,找比中间数大的数字

while save[j]>mid do dec(j);//在中间数右边,找比中间数小的数字

if i<=j//如果左指针在右指针左边

then begin

em:=save[i];//交换2个数字的值,这个你会冒泡排序,或者选择排序任意一个,应该明白

save[i]:=save[j];

save[j]:=em;

inc(i);

dec(j);

end;

until i>j;//左指针跑到右指针右边了。。。

if i<y then qsort(i,y);//如果左指针,没到界限,那么 从左指针到界限进行上述排序

if j>x then qsort(x,j);//如果右指针没跑到,左界限,那么从右指针到左界限排序

end;

begin

randomize;//优化程序用的,暂时你不用会

readln(n);//读入,表示有N个数字

for i:=1 to n do//读入这N个数字

read(save[i]);

qsort(1,n);//从第一个数字,到最后一个数字排序

for i:=1 to n do//输出

write(save[i],' ');

end.

 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝百科 版权所有