자바 정렬 삽입 (최소 코드)

package com.cn.sort;

public class InsertSort {
	public void insertSort(int[] array, int first, int last) {
		int temp, i, j;
		for (i = first + 1; i <= last - 1; i++) {//             ,          。
			temp = array[i];
			j = i - 1;
			while (j >= first && array[j] > temp) {//                temp         ,          temp    
				array[j + 1] = array[j];
				j--;
			}
			array[j + 1] = temp;
			//         
			for (int m = 0; m <= array.length - 1; m++) {
				System.out.print(array[m] + "\t");
			}
			System.out.println();
		}
	}

	public static void main(String[] args) {
		InsertSort insertSort = new InsertSort();
		int[] array = { 5, 69, 12, 3, 56, 789, 2, 5648, 23 };
		insertSort.insertSort(array, 0, array.length);//      0-9   0-8
		for (int i = 0; i <= array.length - 1; i++) {
			System.out.print(array[i] + "\t");
		}
	}
}

좋은 웹페이지 즐겨찾기