sort函數(shù)在C語(yǔ)言中的作用是啥?
1、sort()函數(shù)描述:對(duì)給定區(qū)間所有元素進(jìn)行排序。
sort()函數(shù)語(yǔ)法:sort(begin,end),表示一個(gè)范圍。
2、sort()函數(shù)舉例:
#include <algorithm>
#include <iostream>
using namespace std;
main()
{
int a[11]={2,4,8,5,7,1,10,6,9,3};//a的長(zhǎng)度=待排數(shù)據(jù)個(gè)數(shù)+1
sort(a,a+10);//對(duì)[a,a+10)排序
for(int i=0;i<10;++i) cout<<a[i]<<endl;
}
樓上兩位好像都有問題,這個(gè)函數(shù)傳的是數(shù)組首地址,所以不需要返回值,for循環(huán)是需要大括號(hào)的。
改了一下:
void
sort(int
array[],int
n)
{
int
i,j,k,temp;
for(i=0;i
array[j])
k=j;
temp=array[i];
array[i]=array[k];
array[k]=temp;
}
}
排序(sort)
語(yǔ)法:
void sort();
void sort( Comp compfunction );
sort()函數(shù)為鏈表排序,默認(rèn)是升序。如果指定compfunction的話,就采用指定函數(shù)來判定兩個(gè)元素的大小
相關(guān)評(píng)說:
富寧縣大齒: ______ sort不是C語(yǔ)言的關(guān)鍵字,系統(tǒng)帶的函數(shù)起名字sort,一般用它表示其代碼具有對(duì)數(shù)據(jù)排序的功能.
富寧縣大齒: ______ template<class RanIt> void sort(RanIt first, RanIt last); //--> 1)template<class RanIt, class Pred> void sort(RanIt first, RanIt last, Pred pr); //--> 2) 頭文件: #include <algorithm> using namespace std; 1.默認(rèn)的sort函數(shù)是按升序排.對(duì)應(yīng)于1) sort(a,a+n...
富寧縣大齒: ______ sort()函數(shù)需要一個(gè)函數(shù)指針做參數(shù),sort排序時(shí)調(diào)用傳入的函數(shù)指針?biāo)赶虻暮瘮?shù)做為比較兩個(gè)數(shù)大小的依據(jù);cmp就是傳入sort()的函數(shù)指針;
富寧縣大齒: ______ LZ你好,直接選擇排序法提供給你. #include <stdio.h> sort(int a[],int n) { int i,j,k,x; for(i=0;i<n-1;i++) {k=i; for(j=i+1;j<n;j++) if(a[j]>a[k]) k=j; if(k!=i) {x=a[i];a[i]=a[k];a[k]=x;} } } main() { int i; int array[10]; printf("Enter data :\n"); for( i=0; i<10; i++) scanf(...
富寧縣大齒: ______ cmp 就是比較函數(shù),用于確定兩個(gè)對(duì)象的大小關(guān)系 這是需要你自己定義的
富寧縣大齒: ______ 可以實(shí)現(xiàn)很多功能啊,只是他不需要于使用者進(jìn)行互動(dòng)而已.例如 int add(int a,int b) return a+b;這些需要和用戶交互信息的.而void display(void) printf("Hello Wo...
富寧縣大齒: ______ sort函數(shù)用法例如:int cmp( const int &a, const int &b ){ if( a > b )return 1; else return 0; } sort(a,a+n,cmp); 是對(duì)數(shù)組a降序排序 又如:int cmp( const POINT &a, const POINT &b ){ if( a.x < b.x ) return 1; else if( a.x == b.x ){ if( a.y < b.y ) return 1; else ...
富寧縣大齒: ______ template<class RanIt> void sort(RanIt first, RanIt last); //--> 1)template<class RanIt, class Pred> void sort(RanIt first, RanIt last, Pred pr); //--> 2) 頭文件: #include <algorithm> using namespace std; 1.默認(rèn)的sort函數(shù)是按升序排...
富寧縣大齒: ______ 1:一個(gè)二維數(shù)組定義的時(shí)候應(yīng)該怎么寫呢? 如下 sort(int a[][3],int n) 數(shù)組作為指針是傳遞的是數(shù)組的首地址,二維數(shù)組的橫數(shù)可以不寫,而后的列數(shù)必須說明清楚