国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

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

插入排序

2019-11-06 06:15:49
字體:
來源:轉載
供稿:網友

插入排序及希爾排序

插入排序

插入排序就是每一步都將一個待排數據按其大小插入到已經排序的數據中的適當位置,直到全部插入完畢。 插入排序方法分直接插入排序和折半插入排序兩種,這里只介紹直接插入排序,折半插入排序留到“查找”內容中進行。插入排序不是通過交換位置而是通過比較找到合適的位置插入元素來達到排序的目的的。相信大家都有過打撲克牌的經歷,特別是牌數較大的。在分牌時可能要整理自己的牌,牌多的時候怎么整理呢?就是拿到一張牌,找到一個合適的位置插入。這個原理其實和插入排序是一樣的。舉個栗子,對5,3,8,6,4這個無序序列進行簡單插入排序,首先假設第一個數的位置時正確的,想一下在拿到第一張牌的時候,沒必要整理。然后3要插到5前面,把5后移一位,變成3,5,8,6,4.想一下整理牌的時候應該也是這樣吧。然后8不用動,6插在8前面,8后移一位,4插在5前面,從5開始都向后移一位。寫成Comparable<? super T>,而不是Comparable<T>,就可以對任意類型使用Comparable。
public class Insertion_sort {0	PRivate static Integer[] data;	public static void main(String[] args) {		Random ra =new Random();		data=new Integer[30];		for(int i=0;i<30;i++)			data[i]=ra.nextInt(100)+1;		display(data);		insertionSort(data);		display(data);	}	public static void display(Integer[] data2) {		for(int i=0;i<data2.length;i++)			System.out.print(data2[i]+" ");		System.out.println();	}	public static<T extends Comparable<? super T>> void insertionSort(T[] data) {		for (int index = 1;index<data.length; index++) {			T key=data[index];			int position = index;			// data[position - 1].compareTo(key) > 0表示升序。			//data[position - 1].compareTo(key) < 0表示降序			while (position > 0 && data[position - 1].compareTo(key) > 0) {				data[position] = data[position - 1];				position--;			}			data[position] = key;		}	}}測試結果12 31 32 20 11 31 21 68 80 93 32 31 61 60 65 70 96 19 86 84 6 71 92 29 92 27 96 14 85 15 6 11 12 14 15 19 20 21 27 29 31 31 31 32 32 60 61 65 68 70 71 80 84 85 86 92 92 93 96 96 插入排序算法性能插入排序在最好的情況下是O(n),在最壞的情況下是O(n2)的。數組越接近有序,插入排序所需的工作就越少。

希爾排序

希爾排序(Shell Sort)是插入排序的一種,是針對直接插入排序算法的改進。插入排序的增量是1,而希爾是一個數組決定的。簡單的插入排序中,如果待排序列是正序時,時間復雜度是O(n),如果序列是基本有序的,使用直接插入排序效率就非常高。希爾排序就利用了這個特點。基本思想是:先將整個待排記錄序列分割成為若干子序列分別進行直接插入排序,待整個序列中的記錄基本有序時再對全體記錄進行一次直接插入排序。
public class Shell_sort {		private static Integer[] data;	public static void main(String[] args) {		Random ra =new Random();		data=new Integer[30];		for(int i=0;i<30;i++)			data[i]=ra.nextInt(100)+1;		display(data);		shellSort(data,0,data.length-1);		display(data);	}	public static void display(Integer[] data2) {		for(int i=0;i<data2.length;i++)			System.out.print(data2[i]+" ");		System.out.println();	}		public static<T extends Comparable<? super T>> void shellSort(T[] a,int first,int last){		int n=last-first+1;		for(int space=n/2;space>0;space=space/2){//			System.out.println(space+" ");			for(int begin=first;begin<first+space;begin++){				incrementalInsertionSort(a,begin,last,space);			}//			System.out.println();		}	}		private static<T extends Comparable<? super T>> void incrementalInsertionSort(T[] a,int first,int last,int space){		int unsorted,index;		for(unsorted=first+space;unsorted<=last;unsorted=unsorted+space){			T firstUnsorted=a[unsorted];//			System.out.print(firstUnsorted+" ");			for(index=unsorted-space;(index>=first)&&(firstUnsorted.compareTo(a[index])<0);index=index-space)				a[index+space]=a[index];			a[index+space]=firstUnsorted;		}	}}

算法比較

 最好情況平均情況最壞情況
選擇排序O(n2)O(n2)O(n2)
插入排序O(n)O(n2)O(n2)
希爾排序O(n)O(n1.5)O(n2)或O(n1.5)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 五莲县| 璧山县| 三江| 灯塔市| 白玉县| 乾安县| 温州市| 碌曲县| 长垣县| 长宁县| 淮滨县| 合水县| 宁都县| 连州市| 湄潭县| 长泰县| 徐州市| 松溪县| 武安市| 阜南县| 拜泉县| 广平县| 津市市| 东丰县| 黔江区| 新民市| 六盘水市| 绍兴县| 长沙市| 雷山县| 原平市| 西峡县| 商南县| 青海省| 马关县| 聊城市| 玉龙| 自治县| 荆州市| 岑巩县| 辉南县|