1. 程式人生 > >java 插入排序

java 插入排序

package com.minjun.cn;


import java.awt.*;
import java.awt.event.*;


public class Adapter extends Frame {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Sort s = new Sort();
s.show();
}


}


//inser sort
class Sort{
private int j =2;
private int i =0;
private int key=0;
private int[] test= {1,2,2,15,85,35,24};
public  Sort(){ //直接寫在建構函式裡面,自執行,有些像js裡面的閉包
for(j=2;j<test.length;j++){
key =test[j];
i =j-1;
while(i>0&&test[i]>key){
test[i+1] = test[i];
i--;
}
test[i+1] = key;
//System.out.println("<--------"+test[j]);
}
}

public void show(){
for(j=0;j<test.length;j++){
System.out.println("-------->"+test[j]);

}
}
}