c語言排序庫函數(shù)怎么用 c語言中排序函數(shù)的用法

如何利用C語言中的qsort庫函數(shù)實現(xiàn)快速排序

聲明一個字符串指針數(shù)組存放每個字符串的首地址,調(diào)用庫函數(shù)qusort按題目要求對字符串指針排序,不移動源字符串。關(guān)鍵是要設(shè)計一個好的比較函數(shù),精巧地解決“按長度、長度相等時按大小”排序的問題。舉例代碼如下:

我們提供的服務(wù)有:網(wǎng)站制作、做網(wǎng)站、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、恩平ssl等。為成百上千家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的恩平網(wǎng)站制作公司

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

//#include "stdafx.h"http://If the a href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3njb3nhNWuWubrH-bmH-h0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3En1czrjDLn10v" target="_blank" class="baidu-highlight"vc++6.0/a, with this line.

#include "stdio.h"

#include "string.h"

#include "stdlib.h"

#define N 10 //字符串個數(shù)

#define LN 21 //限制字符串長度為20

int mycmp(const void *a,const void *b){//比較函數(shù)

char *pa=*(char **)a,*pb=*(char **)b;

int x=int(strlen(pa)-strlen(pb));//依長度比較

return x ? x : strcmp(pa,pb);//長度相等時依大小比較

}

int main(void){

int i=0,j=0;

char *f[N],w[LN*N];//聲明a href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3njb3nhNWuWubrH-bmH-h0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3En1czrjDLn10v" target="_blank" class="baidu-highlight"指針數(shù)組/af和字符串總空間

printf("Input %d string(s)(length=%d)...\n",N,LN);

while(iN){//輸入并將字符串首址賦給f[i]

if(scanf(" %[1234567890]",f[i]=w+j)0 strlen(f[i])LN)

i++,j+=LN;

else printf("Error, redo: Required length less than %d:",LN);

}

qsort(f,N,sizeof(char *),mycmp);//調(diào)用a href=";tn=44039180_cprfenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1Y3njb3nhNWuWubrH-bmH-h0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3En1czrjDLn10v" target="_blank" class="baidu-highlight"庫函數(shù)/a對字符串指針排序

for(i=0;iN;printf("%s\n",f[i++]));//輸出...

return 0;

}

C語言怎么使用sort函數(shù),求舉個簡單的例子,謝謝

sort()函數(shù)描述:對給定區(qū)間所有元素進行排序。

sort()函數(shù)語法:sort(begin,end),表示一個范圍。

sort()函數(shù)舉例:

int _tmain(int argc, _TCHAR* argv[])

{

int a[20]={2,4,1,23,5,76,0,43,24,65},i;

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

couta[i]endl;

sort(a,a+20);

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

couta[i]endl;

return 0;

}

輸出結(jié)果將是把數(shù)組a按升序排序。

C語言快速排序函數(shù)怎么調(diào)用

你可以看看這個例子:

#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語言怎樣通過函數(shù)調(diào)用實現(xiàn)選擇排序法

c語言通過函數(shù)調(diào)用實現(xiàn)選擇排序法:

1、寫一個簡單選擇排序法的函數(shù)名,包含參數(shù)。int SelectSort(int * ListData,int ListLength);

2、寫兩個循環(huán),在循環(huán)中應(yīng)用簡單選擇插入排序:

int SelectSort(int * ListData,int ListLength)

{

int i , j ;

int length = ListLength;

for(i=0;i=length-2;i++)

{

int k = i;

for(j=i+1;j=length-1;j++)

{

if(ListData[k]ListData[j])

{

k=j;

}

}

if(k!=i)

{

int tmp = ListData[i];

ListData[i] = ListData[k];

ListData[k] = tmp;

}

}

return 0;

}

3、對編好的程序進行測試,得出測試結(jié)果:

int main()

{

int TestData[5] = {34,15,6,89,67};

int i = 0;

printf("排序之前的結(jié)果\n");

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

printf("|%d|",TestData[i]);

int retData = SelectSort(TestData,5);

printf("排序之后的結(jié)果:\n");

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

printf("|%d|",TestData[i]);

return 0;

}

4、簡單選擇排序中,需要移動的記錄次數(shù)比較少,主要的時間消耗在對于數(shù)據(jù)的比較次數(shù)。基本上,在比較的時候,消耗的時間復(fù)雜度為:n*n。

c語言中sort的用法詳解

c語言的學(xué)習(xí)很多是比較復(fù)雜的,那么c語言中sort的用法的用法你知道嗎?下面我就跟你們詳細介紹下c語言中sort的用法的用法,希望對你們有用。

c語言中sort的用法的用法

sort是STL中提供的算法,頭文件為#includealgorithm以及using namespace std; 函數(shù)原型如下:

?

1

2

3

4

5

template class RandomAccessIterator

void sort ( RandomAccessIterator first, RandomAccessIterator last );

template class RandomAccessIterator, class Compare

void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );

使用第一個版本是對[first,last)進行升序排序,默認操作符為"",第二個版本使用comp函數(shù)進行排序控制,comp包含兩個在[first,last)中對應(yīng)的值,如果使用""則為升序排序,如果使用""則為降序排序,分別對int、float、char以及結(jié)構(gòu)體排序例子如下:

?

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

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

#includestdio.h

#includealgorithm

#includestring

using namespace std;

struct product{

char name[16];

float price;

};

int array_int[5]={4,1,2,5,3};

char array_char[5]={'a','c','b','e','d'};

double array_double[5]={1.2,2.3,5.2,4.6,3.5};

//結(jié)構(gòu)比較函數(shù)(按照結(jié)構(gòu)中的浮點數(shù)值進行排序)

bool compare_struct_float(const product a,const product b){

return a.priceb.price;

}

//結(jié)構(gòu)比較函數(shù)(按照結(jié)構(gòu)中的字符串進行排序)

bool compare_struct_str(const product a,const product b){

return string(a.name)string(b.name);

}

//打印函數(shù)

void print_int(const int* a,int length){

printf("升序排序后的int數(shù)組:\n");

for(int i=0; ilength-1; i++)

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

printf("%d\n",a[length-1]);

}

void print_char(const char* a,int length){

printf("升序排序后的char數(shù)組:\n");

for(int i=0; ilength-1; i++)

printf("%c ",a[i]);

printf("%c\n",a[length-1]);

}

void print_double(const double* a,int length){

printf("升序排序后的dobule數(shù)組:\n");

for(int i=0; ilength-1; i++)

printf("%.2f ",a[i]);

printf("%.2f\n",a[length-1]);

}

void print_struct_array(struct product *array, int length)

{

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

printf("[ name: %s \t price: $%.2f ]\n", array[i].name, array[i].price);

puts("--");

}

void main()

{

struct product structs[] = {{"mp3 player", 299.0f}, {"plasma tv", 2200.0f},

{"notebook", 1300.0f}, {"smartphone", 499.99f},

{"dvd player", 150.0f}, {"matches", 0.2f }};

//整數(shù)排序

sort(array_int,array_int+5);

print_int(array_int,5);

//字符排序

sort(array_char,array_char+5);

print_char(array_char,5);

//浮點排序

sort(array_double,array_double+5);

print_double(array_double,5);

//結(jié)構(gòu)中浮點排序

int len = sizeof(structs)/sizeof(struct product);

sort(structs,structs+len,compare_struct_float);

printf("按結(jié)構(gòu)中float升序排序后的struct數(shù)組:\n");

print_struct_array(structs, len);

//結(jié)構(gòu)中字符串排序

sort(structs,structs+len,compare_struct_str);

printf("按結(jié)構(gòu)中字符串升序排序后的struct數(shù)組:\n");

print_struct_array(structs, len);

}

sort函數(shù)的用法

做ACM題的時候,排序是一種經(jīng)常要用到的操作。如果每次都自己寫個冒泡之類的O(n^2)排序,不但程序容易超時,而且浪費寶貴的比賽時間,還很有可能寫錯。STL里面有個sort函數(shù),可以直接對數(shù)組排序,復(fù)雜度為n*log2(n)。使用這個函數(shù),需要包含頭文件。

這個函數(shù)可以傳兩個參數(shù)或三個參數(shù)。第一個參數(shù)是要排序的區(qū)間首地址,第二個參數(shù)是區(qū)間尾地址的下一地址。也就是說,排序的區(qū)間是[a,b)。簡單來說,有一個數(shù)組int a[100],要對從a[0]到a[99]的元素進行排序,只要寫sort(a,a+100)就行了,默認的排序方式是升序。

拿我出的“AC的策略”這題來說,需要對數(shù)組t的第0到len-1的元素排序,就寫sort(t,t+len);

對向量v排序也差不多,sort(v.begin(),v.end());

排序的數(shù)據(jù)類型不局限于整數(shù),只要是定義了小于運算的類型都可以,比如字符串類string。

如果是沒有定義小于運算的數(shù)據(jù)類型,或者想改變排序的順序,就要用到第三參數(shù)——比較函數(shù)。比較函數(shù)是一個自己定義的函數(shù),返回值是bool型,它規(guī)定了什么樣的關(guān)系才是“小于”。想把剛才的整數(shù)數(shù)組按降序排列,可以先定義一個比較函數(shù)cmp

?

1

2

3

4

bool cmp(int a,int b)

{

return ab;

}

排序的時候就寫sort(a,a+100,cmp);

假設(shè)自己定義了一個結(jié)構(gòu)體node

?

1

2

3

4

5

struct node{

int a;

int b;

double c;

}

有一個node類型的數(shù)組node arr[100],想對它進行排序:先按a值升序排列,如果a值相同,再按b值降序排列,如果b還相同,就按c降序排列。就可以寫這樣一個比較函數(shù):

以下是代碼片段:

?

1

2

3

4

5

6

bool cmp(node x,node y)

{

if(x.a!=y.a) return x.a

if(x.b!=y.b) return x.by.b;

return return x.cy.c;

}

排序時寫sort(arr,a+100,cmp);

?

1

2

3

4

5

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

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

{

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

}

sort函數(shù)的用法:對int類型數(shù)組排序

?

1

2

3

4

5

6

7

int num[100];

Sample:

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

{

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

}

qsort(num,100,sizeof(num[0]),cmp);

sort函數(shù)的用法:對char類型數(shù)組排序(同int類型)

?

1

2

3

4

5

6

7

char word[100];

Sample:

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

{

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

}

qsort(word,100,sizeof(word[0]),cmp);

sort函數(shù)的用法:對double類型數(shù)組排序(特別要注意)

?

1

2

3

4

5

6

double in[100];

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

{

return *(double *)a *(double *)b ? 1 : -1;

}

qsort(in,100,sizeof(in[0]),cmp);

sort函數(shù)的用法:對結(jié)構(gòu)體一級排序

?

1

2

3

4

5

6

7

8

9

10

11

struct In

{

double data;

int other;

}s[100]

//按照data的值從小到大將結(jié)構(gòu)體排序,關(guān)于結(jié)構(gòu)體內(nèi)的排序關(guān)鍵數(shù)據(jù)data的類型可以很多種,參考上面的例子寫

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

{

return ((In *)a)-data - ((In *)b)-data ;

}

qsort(s,100,sizeof(s[0]),cmp);

sort函數(shù)的用法:對結(jié)構(gòu)體

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

struct In

{

int x;

int y;

}s[100];

//按照x從小到大排序,當x相等時按照y從大到小排序

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

{

struct In *c = (In *)a;

struct In *d = (In *)b;

if(c-x != d-x) return c-x - d-x;

else return d-y - c-y;

}

qsort(s,100,sizeof(s[0]),cmp);

sort函數(shù)的用法:對字符串進行排序

?

1

2

3

4

5

6

7

8

9

10

11

struct In

{

int data;

char str[100];

}s[100];

//按照結(jié)構(gòu)體中字符串str的字典順序排序

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

{

return strcmp( ((In *)a)-str , ((In *)b)-str );

}

qsort(s,100,sizeof(s[0]),cmp);

sort函數(shù)的用法:計算幾何中求凸包的cmp

?

1

2

3

4

5

6

7

8

9

int cmp(const void *a,const void *b) //重點cmp函數(shù),把除了1點外的所有點,旋轉(zhuǎn)角度排序

{

struct point *c=(point *)a;

struct point *d=(point *)b;

if( calc(*c,*d,p[1]) 0) return 1;

else if( !calc(*c,*d,p[1]) dis(c-x,c-y,p[1].x,p[1].y) dis(d-x,d-y,p[1].x,p[1].y)) //如果在一條直線上,則把遠的放在前面

return 1;

else return -1;

}

猜你喜歡:

1. c中的用法

2. c語言中邏輯或的用法

3. c語言strcmp的用法

4. c語言中free的用法

5. c語言pow的用法

6. c語言中putchar的用法

新聞名稱:c語言排序庫函數(shù)怎么用 c語言中排序函數(shù)的用法
本文路徑:http://www.muchs.cn/article14/hjeede.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、網(wǎng)站維護、企業(yè)網(wǎng)站制作、網(wǎng)站內(nèi)鏈、面包屑導(dǎo)航、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)