数据结构-插入排序,数据结构插入排序,package com.
分享于 点击 23187 次 点评:68
数据结构-插入排序,数据结构插入排序,package com.
package com.algorithm.sorting;import com.algorithm.utils.Compare;/* * Insert sortting * time: O(N*N) */public class Insertion<E> { Compare compare = new Compare(); public E[] sort(E[] list) { int len = list.length; int k = 0; for (int i = 1; i < len; i++) { E data = list[i]; for (k = i; k > 0 && (compare.compare(list[k - 1], data) > 0); k--) list[k] = list[k - 1];// swap the position list[k] = data;// insert to the collect position(k=k-1) } return list; } // descending降序 public E[] sort(E[] list,String desc) { int len = list.length; int k = 0; for (int i = 1; i < len; i++) { E entry = list[i]; for (k = i; k > 0 && (compare.compare(list[k - 1], entry) < 0); k--) list[k] = list[k - 1];// swap the position list[k] = entry;// insert to the collect position(k=k-1) } return list; }}//该片段来自于http://byrx.net
用户点评