麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

十大排序算法

2019-11-06 06:47:45
字體:
來源:轉載
供稿:網友

選擇排序 選擇排序的基本思想是對待排序的記錄序列進行n-1遍的處理,第i遍處理是將L[i..n]中最小者與L[i]交換位置。這樣,經過i遍處理之后,前i個記錄的位置已經是正確的了。 選擇排序是不穩定的。算法復雜度是O(n ^2 )。

class SelectionSorter{ PRivate int min; public void Sort(int[] arr) { for(int i=0;i<arr.Length-1;++i) { min=i; for(int j=i+1;j<arr.Length;++i) { if(arr[j]<arr[min]) min=j; } int t=arr[min]; arr[min]=arr[i]; arr[i]=t; } }}

冒泡排序 冒泡排序方法是最簡單的排序方法。這種方法的基本思想是,將待排序的元素看作是豎著排列的“氣泡”,較小的元素比較輕,從而要往上浮。在冒泡排序算法中我們要對這個“氣泡”序列處理若干遍。所謂一遍處理,就是自底向上檢查一遍這個序列,并時刻注意兩個相鄰的元素的順序是否正確。如果發現兩個相鄰元素的順序不對,即“輕”的元素在下面,就交換它們的位置。顯然,處理一遍之后,“最輕”的元素就浮到了最高位置;處理二遍之后,“次輕”的元素就浮到了次高位置。在作第二遍處理時,由于最高位置上的元素已是“最輕”元素,所以不必檢查。一般地,第i遍處理時,不必檢查第i高位置以上的元素,因為經過前面i-1遍的處理,它們已正確地排好序。冒泡排序是穩定的。算法時間復雜度是O(n ^2)。

class EbullitionSorter{ public void Sort(int[] arr) { int i,j,temp; bool done=false; j=1; while((j<arr.Length)&&(!done)) //判斷長度 { done=true; for(i=0;i<arr.Length-j;i++) { if(arr[i]>arr[i+1]) { done=false; temp=arr[i]; arr[i]=arr[i+1]; //交換數據 } } j++; } }}這里寫代碼片

快速排序 快速排序是對冒泡排序的一種本質改進。它的基本思想是通過一趟掃描后,使得排序序列的長度能大幅度地減少。在冒泡排序中,一次掃描只能確保最大數值的數移到正確位置,而待排序序列的長度可能只減少1。快速排序通過一趟掃描,就能確保某個數(以它為基準點吧)的左邊各數都比它小,右邊各數都比它大。然后又用同樣的方法處理它左右兩邊的數,直到基準點的左右只有一個元素為止。 快速排序是不穩定的。最理想情況算法時間復雜度O(nlog2n),最壞O(n ^2)。

class QuickSorter{ private void swap(ref int l,ref int r) { int temp; temp=l; l=r; r=temp; } public void Sor(int[] list,int low,int high) { int pivot; //存儲分支點 int l,r; int mid; if(high<=low) return; else if(high==low+1) { if(list[low]>list[high]) swap(ref list[low],ref list[high]); return; } mid=(low+high)>>1; pivot=list[mid]; swap(ref list[low],ref list[mid]); l=low+1; r=high; do { while(l<=r&&list[l]pivot) l++; while(list[r]>=pivot) r--; if(l<r) swap(ref list[l],ref list[r]); }while(l<r); list[low]=list[r]; list[r]=pivot; if(low+1<r) Sort(list,low,r-1); if(r+1<high) Sort(list,r+1,high); }}

插入排序 插入排序的基本思想是,經過i-1遍處理后,L[1..i-1]己排好序。第i遍處理僅將L[i]插入L[1..i-1]的適當位置,使得L[1..i]又是排好序的序列。要達到這個目的,我們可以用順序比較的方法。首先比較L[i]和L[i-1],如果L[i-1]≤ L[i],則L[1..i]已排好序,第i遍處理就結束了;否則交換L[i]與L[i-1]的位置,繼續比較L[i-1]和L[i-2],直到找到某一個位置j(1≤j≤i-1),使得L[j] ≤L[j+1]時為止。直接插入排序是穩定的。算法時間復雜度是O(n ^2) 。

public class InsertionSorter{ public void Sort(int[] arr) { for(int i=1;i<arr.Lenngth;i++) { int t=arr[i]; int j=i; while((j>0)&&(arr[j-1]>t)) { arr[j]=arr[j-1]; //交換順序 --j; } arr[j]=t; } }}這里寫代碼片

希爾排序 希爾排序基本思想: 先取一個小于n的整數d1作為第一個增量,把文件的全部記錄分成d1個組。所有距離為dl的倍數的記錄放在同一個組中。先在各組內進行直接插入排序;然后,取第二個增量d2

public class ShellSorter{ public void Sort(int[] arr) { int inc; for(int=1;inc<=arr.Length/9;inc=38inc+1); for(;inc>0;inc/=3) { for(int i=inc+1;i<=arr.Length;i+=inc) { int t=arr[i-1]; int j=i; while(j>inc)&&(arr[j-inc-1]>t) { arr[j-1]=arr[j-inc-1]; //交換數據 j-=inc; } arr[j-1]=t; } } }}

歸并排序 設有兩個有序(升序)序列存儲在同一數組中相鄰的位置上,不妨設為A[l..m],A[m+1..h],將它們歸并為一個有序數列,并存儲在A[l..h]。 其時間復雜度無論是在最好情況下還是在最壞情況下均是O(nlog2n)。

int[] Sort(int[] data){ //取數組中間下標 int middle=data.Length/2; //初始化臨時數組left,right,并定義result作為最終有序數組 int[] left=new int[middle],right=new int[middle],result=new int[data.Length]; if(data.Length%2!=0) //若數組元素有奇數個,重新初始化右臨時數組 { right=new int[middle+1]; } if(data.Length<=1) //只剩下1或0個元素,返回,不排序 { return data; } int i=0,j=0; foreach(int x,int data) //開始排序 { if(i<middle) //填充左數組 { left[i]=x; i++; } else //填充右數組 { right[j]=x; j++; } } left=Sort(left); //遞歸左數組 right=Sort(right); //遞歸右數組 result=Merge(left,right); //開始排序 return result; int[] Merge(int[]a,int[]b) { //定義結果數組,用來存儲最終結果 int[] result=new int[a.Length+b.Length]; int i=0;j=0;k=0; while(i<a.Length&&j<b.Length) { if(a[i]<b[j]) //左數組中元素小于右數組中元素 { result[k++]=a[i++]; //將小的那個放到結果數組 } else //左數組中元素大于右數組中元素 { result[k++]=b[j++]; //將小的那個放到結果數組 } } while(i<a.Length) //這里其實還有左元素,但沒有右元素 { result[k++]=a[i++]; } while(j<b.Length) //有右元素,無左元素 { result[k++]=b[j++]; } return result; //返回結果數組 }}

基數排序 基數排序法又稱“桶子法”(bucket sort)或bin sort,顧名思義,它是透過鍵值的部份資訊,將要排序的元素分配至某些“桶”中,藉以達到排序的作用,基數排序法是屬于穩定性的排序,其時間復雜度為O (nlog(r)m),其中r為所采取的基數,而m為堆數,在某些時候,基數排序法的效率高于其它的比較性排序法。

public int[] RadixSort(int[] ArrayToSort, int digit){//low to high digitfor (int k = 1; k <= digit; k++){//temp array to store the sort result inside digitint[] tmpArray = new int[ArrayToSort.Length];//temp array for countingsortint[] tmpCountingSortArray = new int[10]{0,0,0,0,0,0,0,0,0,0};//CountingSortfor (int i = 0; i < ArrayToSort.Length; i++){//split the specified digit from the elementint tmpSplitDigit = ArrayToSort[i]/(int)Math.Pow(10,k-1) - (ArrayToSort[i]/(int)Math.Pow(10,k))*10;tmpCountingSortArray[tmpSplitDigit] += 1;}for (int m = 1; m < 10; m++){tmpCountingSortArray[m] += tmpCountingSortArray[m - 1];}//output the value to resultfor (int n = ArrayToSort.Length - 1; n >= 0; n–){int tmpSplitDigit = ArrayToSort[n] / (int)Math.Pow(10,k – 1) – (ArrayToSort[n]/(int)Math.Pow(10,k)) * 10;tmpArray[tmpCountingSortArray[tmpSplitDigit]-1] = ArrayToSort[n];tmpCountingSortArray[tmpSplitDigit] -= 1;}//copy the digit-inside sort result to source arrayfor (int p = 0; p < ArrayToSort.Length; p++){ArrayToSort[p] = tmpArray[p];}}return ArrayToSort;}

記數排序 數排序, 基數排序, 桶排序等非比較排序算法,平均時間復雜度都是O(n). 這些排序因為其待排序元素本身就含有了定位特征,因而不需要比較就可以確定其前后位置,從而可以突破比較排序算法時間復雜度O(nlgn)的理論下限. 計數排序是最簡單的特例,它要 求待排序元素是位于0到k之間的正整數 , 因而它是很特殊的情況,基本上沒有特別的應用價值; 但是另一方面, 它又是基數排序的基礎,或者說是一部分,所以簡單的描述一下: 輸入數組 A : 元素特征是 0-k的正整數,可以有重復值; 輸出數組 B : 輸出A的一個非減序列 中間數組 C : 大小是k, 它的i( 0<= i <= k)索引位置存儲的是A元素集合中值是k的元素的個數有關. 算法的基本思想是: 統計A中元素的值的集合, 以A中元素的值為索引, 將值的個數填寫到中間數組C的對應處. 對C從頭開始自累加, 這樣C中存儲的就是, 當輸入數組A中的值為當前索引時, 它前面的元素數量(包含重復元素). 將C依次輸出到輸出數組B中.

////// counting sort/// ///input array ///the value arrange in input array /// public int[] CountingSort(int[] arrayA, int arrange){//array to store the sorted result,//size is the same with input array.int[] arrayResult = new int[arrayA.Length];//array to store the direct value in sorting process//include index 0;//size is arrange+1;int[] arrayTemp = new int[arrange+1];//clear up the temp arrayfor(int i = 0; i <= arrange; i++){arrayTemp[i] = 0;}//now temp array stores the count of value equalfor(int j = 0; j < arrayA.Length; j++){arrayTemp[arrayA[j]] += 1;}//now temp array stores the count of value lower and equalfor(int k = 1; k <= arrange; k++){arrayTemp[k] += arrayTemp[k - 1];}//output the value to resultfor (int m = arrayA.Length-1; m >= 0; m–){arrayResult[arrayTemp[arrayA[m]] – 1] = arrayA[m];arrayTemp[arrayA[m]] -= 1;}return arrayResult;}

根堆排序 堆排序是一種樹形選擇排序,在排序過程中,將A[n]看成是完全二叉樹的順序存儲結構,利用完全二叉樹中雙親結點和孩子結點之間的內在關系來選擇最小的元素。 算法時間復雜度O(nlog n)。

private void HeapSort(ref double[] dblArray){for (int i = dblArray.Length – 1; i >= 0; i–){if (2 * i + 1 < dblArray.Length){int MinChildrenIndex = 2 * i + 1;//比較左子樹和右子樹,記錄最小值的Indexif (2 * i + 2 < dblArray.Length){if (dblArray[2 * i + 1] > dblArray[2 * i + 2])MinChildrenIndex = 2 * i + 2;}if (dblArray[i] > dblArray[MinChildrenIndex]){ExchageValue(ref dblArray[i], ref dblArray[MinChildrenIndex]);NodeSort(ref dblArray, MinChildrenIndex);}}}}////// 節點排序/// /// /// private void NodeSort(ref double[] dblArray, int StartIndex){while (2 * StartIndex + 1 < dblArray.Length){int MinChildrenIndex = 2 * StartIndex + 1;if (2 * StartIndex + 2 < dblArray.Length){if (dblArray[2 * StartIndex + 1] > dblArray[2 * StartIndex + 2]){MinChildrenIndex = 2 * StartIndex + 2;}}if (dblArray[StartIndex] > dblArray[MinChildrenIndex]){ExchageValue(ref dblArray[StartIndex], ref dblArray[MinChildrenIndex]);StartIndex = MinChildrenIndex;}}}////// 交換值/// /// /// private void ExchageValue(ref double A, ref double B){double Temp = A;A = B;B = Temp;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 韩国精品一区二区三区四区五区 | 蜜桃精品视频在线观看 | 亚洲国产视频网 | 日韩视频在线不卡 | 黄视频免费在线观看 | 成人在线免费视频播放 | 国产亚洲综合一区二区 | 亚洲性生活免费视频 | 91九色视频在线播放 | xxxx18韩国护士hd老师 | 91看片免费版 | 国产一区二区三区撒尿在线 | 精品国产91久久久久久浪潮蜜月 | 黄色a级片视频 | 国产一区二区三区视频观看 | 一级做a爱片久久 | 午夜视频在线观看免费视频 | 成人免费乱码大片a毛片视频网站 | 国产中出在线观看 | 毛片视频在线免费观看 | 亚州综合一区 | 亚洲免费视 | 成人 日韩 | 国产999精品久久久久 | 亚洲一级电影在线观看 | 一级观看免费完整版视频 | 欧美中文字幕一区二区 | 中文字幕在线免费播放 | 成人 精品 | free japan xxxxhdsex69| 欧美精品一区自拍a毛片在线视频 | 精品一区二区三区中文字幕老牛 | 午夜神马福利视频 | 日本不卡一区二区三区在线观看 | 久久久久一本一区二区青青蜜月 | 毛片区 | 草草视频在线播放 | 亚洲字幕av| 国产精品美女久久久久久不卡 | 国产亚洲欧美视频 | 久久线视频 |