java 插入排序


package com;


/**
 * @author leon
 *
 */
public class InsertSort {


/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
InsertSort insertSort = new InsertSort();
int arr[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,51};
insertSort.insertSort(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]);
System.out.print(",");
}
}


public void insertSort(int arr[]){

for (int i = 1; i < arr.length; i++) {
int j = i - 1;
int insertedVelue = arr[i];
for (; j >= 0 && insertedVelue < arr[j]; j--) {
arr[j+1] = arr[j];
}
arr[j+1] = insertedVelue;
}
}
}
發佈了28 篇原創文章 · 獲贊 5 · 訪問量 28萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章