時(shí)間復(fù)雜度:O(n^2)
void InsertSort(int a[], int n){// 數(shù)據(jù)基本有序的情況下時(shí)間復(fù)雜度低
for(int i = 1; i< n; i++){
if(a[i]< a[i-1]){
a[0] = a[i];// 找到無序的第一個(gè)值,設(shè)置哨兵
for(int j = i - 1; a[j] >a[0]; j--){
a[j+1] = a[j];
a[j+1] = a[0];
}
}
}
}
二、希爾排序時(shí)間復(fù)雜度:O(n^(3/2))
給定增量d,數(shù)據(jù)分為d組
void ShellSort(int a[], int n, int d){
for(int i = 1 + d; i<= n; i++){
if(a[i]< a[i-d]){
a[0] = a[i];
for(int j = i - d; a[j] >a[0]; j -= d){
a[j+d] = a[j];
a[j+d] = a[0];
}
}
}
}
快速排序時(shí)間復(fù)雜度:O(n)
C++ 詳解快速排序代碼_snowman22的博客-博客_快速排序c++代碼
練習(xí):洛谷 P1923
歸并排序?時(shí)間復(fù)雜度:nlogn
// 歸并排序:穩(wěn)定,nlogn
int a[N], c[N];// c數(shù)組為臨時(shí)數(shù)組
void msort(int s, int t){
if(s == t) return ;
// 先分再合
int mid = s + t >>1;
msort(s, mid);
msort(mid + 1, t);
int i = s, j = mid + 1, k = s;
while(i<= mid && j<= t){
if(a[i]<= a[j]){
c[k++] = a[i++];
}
else c[k++] = a[j++];
}
while(i<= mid) c[k++] = a[i++];
while(j<= t) c[k++] = a[j++];
for(int i = s; i<= t; i++){
a[i] = c[i];// 將臨時(shí)數(shù)組的數(shù)據(jù)復(fù)制回來
}
}
**應(yīng)用:求序列中的逆序?qū)?/p>
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購,新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧
本文標(biāo)題:排序算法知識(shí)點(diǎn)-創(chuàng)新互聯(lián)
網(wǎng)頁URL:http://chinadenli.net/article6/dggjig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、網(wǎng)站改版、動(dòng)態(tài)網(wǎng)站、網(wǎng)站排名、靜態(tài)網(wǎng)站、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容
移動(dòng)網(wǎng)站建設(shè)知識(shí)