插入法排序
※ 插入法排序原理
利用插入法对无序数组排序时,我们其实是将数组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)
}
}