三大排序演算法(Bubble/Quick/Shell)冒泡、快排、希爾……

來源:互聯網
上載者:User

最近兩天在為找工作複習準備。特此總結了資料結構中內部排序一章中提到的三大排序演算法。做了簡單實現。

// Sort.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <stdio.h>void bubble(int list[], int n){int i,j,temp;for (i=0;i<n;i++){for (j=0;j<n-i-1;j++){if (list[j]>list[j+1]){temp = list[j];list[j] = list[j+1];list[j+1] = temp;}}}}//*************************************************//希爾排序//*************************************************void ShellInsert(int list[], int dk, int len){int i, j;int temp;for(i=dk; i<len;++i){if (list[i]<list[i-dk]){temp=list[i];for (j=i-dk;j>=0&&(temp<list[j]);j-=dk){list[j+dk]=list[j];}list[j+dk] = temp;}}}void ShellSort(int list[], int len){int dk=len;while (dk>0){dk=dk/2;ShellInsert(list,dk,len);}}//**********************************************************//快速排序//**********************************************************int Partition(int list[], int low, int high){int pivotkey;pivotkey = list[low];while (low<high){while ((low<high) && (list[high]>=pivotkey)){high--;}list[low] = list[high];while((low<high) && (list[low]<=pivotkey)){low++;}list[high] = list[low];}list[low] = pivotkey;return low;}void QSort(int list[], int low, int high){int pivotloc;if (low<high){pivotloc = Partition(list, low, high);QSort(list,low,pivotloc-1);QSort(list,pivotloc+1,high);}}void QuickSort(int list[], int len){QSort(list,0,len-1);}int main(int argc, char* argv[]){int list[]={49,38,65,97,76,13,27,49,55,04};// bubble(list,10);/*ShellSort(list,10);*/QuickSort(list, 10);for (int i=0; i<10; i++){printf("%d ", list[i]);}printf("\n");return 0;}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.