排序算法-------插入排序法

插入法排序
※ 插入法排序原理
利用插入法對無序數組排序時,我們其實是將數組R劃分成兩個子區間R[1..i-1](已排好序的有序區)和R[i..n](當前未排序的部分,可稱無序區)。插入排序的基本操作是將當前無序區的第1個記錄R[i]插人到有序區R[1..i-1]中適當的位置上,使R[1..i]變爲新的有序區。因爲這種方法每次使有序區增加1個記錄,通常稱增量法。
插入排序與打撲克時整理手上的牌非常類似。摸來的第1張牌無須整理,此後每次從桌上的牌(無序區)中摸最上面的1張並插入左手的牌(有序區)中正確的位置上。爲了找到這個正確的位置,須自左向右(或自右向左)將摸來的牌與左手中已有的牌逐一比較。
æå¥æåºæ³å¨æå¾

圖解:根據其原理,我們把該無序數列看成兩個部分,我們不難看出圖中,首先我們把第一位3看成是有序數列,剩下的作爲無序數列,因爲要把後面的無序數列中的數插入到前面有序數列,所以依次把後面的數抽出,在前面找到合適位置插入。如圖,先抽出44,與前面比較,比3大放在3後面,再抽出38,與前面比較,比44小,比3大,所以插入到3後面。依次類推,直到最後的一個數也插入到前面的有序數列中。

å¨è¿éæå¥å¾çæè¿°

實現代碼如下:

/*
 * 插入排序算法:
 * 1、以數組的某一位作爲分隔位,比如index=1,假設左面的都是有序的.
 * 
 * 2、將index位的數據拿出來,放到臨時變量裏,這時index位置就空出來了.
 * 
 * 3、從leftindex=index-1開始將左面的數據與當前index位的數據(即temp)進行比較,如果array[leftindex]>temp,
 * 則將array[leftindex]後移一位,即array[leftindex+1]=array[leftindex],此時leftindex就空出來了.
 * 
 * 4、再用index-2(即leftindex=leftindex-1)位的數據和temp比,重複步驟3,
 * 直到找到<=temp的數據或者比到了最左面(說明temp最小),停止比較,將temp放在當前空的位置上.
 * 
 * 5、index向後挪1,即index=index+1,temp=array[index],重複步驟2-4,直到index=array.length,排序結束,
 * 此時數組中的數據即爲從小到大的順序.
 * 
 * @author bjh
 *
 */
public class InsertSort {
    private int[] array;
    private int length;
    
    public InsertSort(int[] array){
        this.array = array;
        this.length = array.length;
    }
    
    public void display(){        
        for(int a: array){
            System.out.print(a+" ");
        }
        System.out.println();
    }
    
    /*
     * 插入排序方法
     */
    public void doInsertSort(){
        for(int index = 1; index<length; index++){//外層向右的index,即作爲比較對象的數據的index
            int temp = array[index];//用作比較的數據
            int leftindex = index-1;
            while(leftindex>=0 && array[leftindex]>temp){//當比到最左邊或者遇到比temp小的數據時,結束循環
                array[leftindex+1] = array[leftindex];
                leftindex--;
            }
            array[leftindex+1] = temp;//把temp放到空位上
        }
    }
    
    public static void main(String[] args){
        int[] array = {38,65,97,76,13,27,49};
        InsertSort is = new InsertSort(array);
        System.out.println("排序前的數據爲:");
        is.display();
        is.doInsertSort();
        System.out.println("排序後的數據爲:");
        is.display();
    }
}

 加上其他幾種常用排序比較

三種排序彙總:

package com.briup.Abstract;

import java.util.Arrays;

public class A {
  public int[] array;
  public A(int[] array){
	  this.array=array;
  }
  //冒泡排序
public void test1() {
	for(int i=1;i<array.length;i++) {
		for(int j=0;j<array.length-i;j++) {
			if(array[j]>array[j+1]) {
				int temp=array[j];
				array[j]=array[j+1];
				array[j+1]=temp;
			}
		}
	}
	System.out.println(Arrays.toString(array));
}
//選擇排序
public void test2() {
	int i,j,index;
	for(i=0;i<array.length;i++) {
		index=i;
		for(j=i+1;j<array.length;j++) {
			if(array[j]<array[index]) {
				index=j;
			}
		}
		int temp=array[i];
		array[i]=array[index];
		array[index]=temp;
	}
	System.out.println(Arrays.toString(array));
}
//插入排序
public void test3() {
	int i,j,temp;
	for(i=1;i<array.length;i++) {
		temp=array[i];
		for(j=i-1;j>=0;j--) {
			if(temp>array[j]) {
				break;
			}else {
				array[j+1]=array[j];
			}
		}
		array[j+1]=temp;
	}
	System.out.println(Arrays.toString(array));
}
public static void main(String[] args) {
     int[] array={23,34,25,66,21};
     A a=new A(array);
     a.test3();
}
}

 加上kotlin實現

fun insertionSort(input:Array<Int>):Array<Int>{
    val maxindex=input.size-1
    /**
     * 最外層控制次數
     * 內層循環排序的次數檢查
     */
//    for (i in 1..maxindex){
//        for (j in i downTo 1){
//            if (input[j]<input[j-1]){
//                var tem=input[j-1]
//                input[j-1]=input[j]
//                input[j]=tem
//            }
//        }
//    }
    for (i in 1..maxindex){
        var d=input[i]//待插入的元素
        for (j in i-1 downTo 0){
            if (input[j]>d){//從已排好的最大數依次向最小說比較d,如果比d大就將較大數往後移動一位
                input[j+1]=input[j]
                input[j]=d
            }
        }
    }
    return input
}
fun main(args: Array<String>) {
   var  a= arrayOf(9,4,3,7,2,5,8)
    insertionSort(a)
    for (z in a){
        println(z)
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章