算法:排序算法的比较
默認(rèn)為遞增順序;注:一下例子希望自己再次復(fù)習(xí)時(shí),可以用筆在紙上畫畫內(nèi)存圖。
包括有:
- 選擇排序
- 冒泡排序
- 插入排序
1.選擇排序
<--------------------------------------選擇排序--------------------------------------->
1、選擇排序(1):
選擇排序的思想是,每一次從待排序的數(shù)據(jù)元素中選出最小(或最大)的一個元素,存放在序列的起始位置,直到全部待排序的數(shù)據(jù)元素排完。
選擇排序1:
private static void selectionSort(int[] a){for(int i = 0;i<a.length;i++){for(int j = i+1;j<a.length;j++){if( a[j] <a[i]){int temp = a[i];a[i] = a[j];a[j] = temp;}}} }選擇排序2(最優(yōu)):
private static void OptimizeNumSort(int[] a){int k,temp;// k and temp is not need to allocation internal storage everytime.for(int i=0;i<a.length;i++){k = i; //use k record minimum number at present ,hypothesis i is minimum./用k記錄那個最小的數(shù)for(int j = k+1;j<a.length;j++){if(a[j]<a[k]){k = j;}}if(i != k ){temp = a[i];a[i] = a[k];a[k] = temp;}} }?
python實(shí)現(xiàn):
def selectionSort(arr):for i in range(len(arr) - 1):# 記錄最小數(shù)的索引minIndex = ifor j in range(i + 1, len(arr)):if arr[j] < arr[minIndex]:minIndex = j# i 不是最小數(shù)時(shí),將 i 和最小數(shù)進(jìn)行交換if i != minIndex:arr[i], arr[minIndex] = arr[minIndex], arr[i]return arr?
<---------------------------------------冒泡排序-------------------------------------------------->
2、冒泡排序:
?基本思想 :
設(shè)排序表長為n,從后向前或者從前向后兩兩比較相鄰元素的值,如果兩者的相對次序不對(A[i-1] > A[i]),則交換它們,其結(jié)果是將最小的元素交換到待排序序列的第一個位置,我們稱它為一趟冒泡。下一趟冒泡時(shí),前一趟確定的最小元素不再參與比較,待排序序列減少一個元素,每趟冒泡的結(jié)果把序列中最小的元素放到了序列的”最前面”。
比較相鄰的元素。如果第一個比第二個大,就交換他們兩個。
對每一對相鄰元素作同樣的工作,從開始第一對到結(jié)尾的最后一對。在這一點(diǎn),最后的元素應(yīng)該會是最大的數(shù)。
針對所有的元素重復(fù)以上的步驟,除了最后一個。
持續(xù)每次對越來越少的元素重復(fù)上面的步驟,直到?jīng)]有任何一對數(shù)字需要比較。?
冒泡排序是一種簡單的排序算法。它重復(fù)地走訪過要排序的數(shù)列,一次比較兩個元素,如果它們的順序錯誤就把它們交換過來。走訪數(shù)列的工作是重復(fù)地進(jìn)行直到?jīng)]有再需要交換,也就是說該數(shù)列已經(jīng)排序完成。這個算法的名字由來是因?yàn)樵叫〉脑貢?jīng)由交換慢慢“浮”到數(shù)列的頂端。?
?
算法描述
- 比較相鄰的元素。如果第一個比第二個大,就交換它們兩個;
- 對每一對相鄰元素作同樣的工作,從開始第一對到結(jié)尾的最后一對,這樣在最后的元素應(yīng)該會是最大的數(shù);
- 針對所有的元素重復(fù)以上的步驟,除了最后一個;
- 重復(fù)步驟1~3,直到排序完成。
?
private static void bubbleSort(int[] a){int len = a.length;int temp;for(int i = len-1; i>=1;i--){for(int j = 0;j<len-1;j++){if(a[j]>a[j+1]){temp = a[j];a[j] = a[j+1];a[j+1] = temp;}}} }?
來個小練習(xí):對某個類的對象進(jìn)行排序:
?
? ? 對日期(yeda-month-day)進(jìn)行排序:
? ? 使用到條件運(yùn)算符(?:)
public int compare(Date date){return (year>date.year)?1:year<date.year?-1:month>date.month?1:month<date.month?-1:day>date.day?1:day<date.day?-1:0; }詳細(xì)如下:
?
public class Test{public static void main(String[] args) {Date[] days = new Date[5]; days[0] = new Date(2006,5,4);days[1] = new Date(2006,7,4);days[2] = new Date(2008,5,4);days[3] = new Date(2004,5,9);days[4] = new Date(2004,5,4);for(int i=0;i<days.length;i++ ) {System.out.println(days[i]); }System.out.println("\nUp is not sort---------------------------Down after sort:\n");//bubbleSort(days);// call the mothod of bubbleSort().selectionSort(days);// call the mothod of selectionSort().for(int i=0;i<days.length;i++ ) {System.out.println(days[i]); }}/*Bubble sort and return sort a type;learn to bubbleSort' thinking;every sort will get the maximum.*/public static Date[] bubbleSort(Date[] a){int len = a.length;for(int i=len-1;i>=1;i--){for (int j = 0;j<=i-1 ;j++ ) {if(a[j].compare(a[j+1]) > 0){Date temp = a[j];a[j] = a[j+1];a[j+1] = temp;} } }return a;}/*selection sort */public static Date[] selectionSort(Date[] a){int len = a.length;int k;Date temp;for(int i =0 ; i<len;i++){k = i;for(int j = k+1;j<len;j++){if(a[k].compare(a[j]) == 1){k = j;}}if(k != i){temp = a[k];a[k] = a[i];a[i] = temp;}}return a;}}class Date{int year,month,day;Date(int y,int m,int d){year = y;month = m;day = d;}/*method compare() mainly to compare two Date objects' size.if a>b return 1;else if a<b return -1;else a = b return 0;according to return value ,whether to exchange objects'quote*/public int compare(Date date){return (year>date.year)?1:year<date.year?-1:month>date.month?1:month<date.month?-1:day>date.day?1:day<date.day?-1:0;}/*remember: when print an objects'quote ,is equal to call toString() method.so have to overwrite the toString*another:you can query the API/*/public String toString(){return "Year:Month:Day-- " + year + "-" + month + "-" + day;} }?
?
<--------------------------------插入排序-------------------------------->
3、插入排序(Insertion Sort)
插入排序(Insertion-Sort)的算法描述是一種簡單直觀的排序算法。它的工作原理是通過構(gòu)建有序序列,對于未排序數(shù)據(jù),在已排序序列中從后向前掃描,找到相應(yīng)位置并插入。
3.1 算法描述
一般來說,插入排序都采用in-place在數(shù)組上實(shí)現(xiàn)。具體算法描述如下:
- 1、從第一個元素開始,該元素可以認(rèn)為已經(jīng)被排序;
- 2、取出下一個元素,在已經(jīng)排序的元素序列中從后向前掃描;
- 3、如果該元素(已排序)大于新元素,將該元素移到下一位置;
- 4、重復(fù)步驟3,直到找到已排序的元素小于或者等于新元素的位置;
- 5、將新元素插入到該位置后;
- 6、重復(fù)步驟2~5。
?
參考:
(1)http://www.cnblogs.com/wuxinyan/p/8615127.html(python 十大經(jīng)典排序算法)
?
總結(jié)
以上是生活随笔為你收集整理的算法:排序算法的比较的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前端学习(220):伪元素选择器
- 下一篇: session超时以及销毁