InsertSort

来源:互联网 发布:淘宝客服语言技巧 编辑:程序博客网 时间:2024/06/15 08:07
package com.kilophone.classcontent;// Administrator 从数组的第一个元素a[0]开始, // 将其后一个元素a[1]插入到a[0]的前面或者后面,// 接着继续这一过程。每次都是将a[i]插入到已经排序好的public class TestInsertSort {public static void main(String[] args) {int arr[] = { 15, 23, 7, 10, 8 };for (int i = 1; i < arr.length; i++) {for (int j = i - 1; j >= 0; j--) {if (arr[j + 1] < arr[j]) {int temp;temp = arr[j];arr[j] = arr[j + 1];arr[j + 1] = temp;} else {break;}}}for (int a : arr) {System.out.print(a + "\t");}}}