总结下各种排序,首先是插入排序。注意三点:
1.插入排序在数据量比较小或者基本有序时,是最好的选择,常会被用在较复杂的排序算法的最后阶段
2.插入排序是稳定的
3.属于插入类排序
package merge;
import java.util.Arrays;
/**
* 插入排序在数据量比较小或者基本有序时,是最好的选择,常会被用在较复杂的排序算法的最后阶段
* 插入排序是稳定的
* 属于插入类排序
* @author Administrator
*
*/
public class InsertionSort {
private int[] items;
private int size;
public InsertionSort(int[] arrays) {
size = arrays.length;
items = Arrays.copyOf(arrays,size);
}
public int[] sort() {
for(int i = 1; i < size;i++) {
int key = items[i];
int j = i-1;
for(; j > -1 && items[j]>key; j--)
{
items[j+1] = items[j];
}
items[j+1]=key;
}
return items;
}
public static void main(String[] args)
{
int[] a= new int[] {1,0,9,8,7,6,5,4,3,3,2,1,11};
InsertionSort m = new InsertionSort(a);
a = m.sort();
for(int item : a)
{
System.out.print(item + " ");
}
}
}
使用泛型实现版
package merge;
import java.util.Arrays;
/**
* 使用泛型实现的插入算法
* @author Administrator
*
* @param <T>
*/
public class InsertionSort2<T extends Comparable<T>> {
private T[] items;
private int size;
public InsertionSort2(T[] arrays) {
size = arrays.length;
items = Arrays.copyOf(arrays,size);
}
public T[] sort() {
for(int i = 1; i < size;i++) {
T key = items[i];
int j = i-1;
for(; j >=0 && (items[j].compareTo(key) >0); j--)
{
items[j+1] = items[j];
}
items[j+1]=key;
}
return items;
}
public static void main(String[] args)
{
//Integer[] a= new Integer[] {1,0,9,8,7,6,5,4,3,3,2,1,11};
Character[] a = new Character[] {'a','c','e','d','b','a'};
InsertionSort2 m = new InsertionSort2(a);
// a = (Integer[])m.sort();
a = (Character[])m.sort();
for(Character item : a)
{
System.out.print(item + " ");
}
}
}
1.插入排序在数据量比较小或者基本有序时,是最好的选择,常会被用在较复杂的排序算法的最后阶段
2.插入排序是稳定的
3.属于插入类排序
package merge;
import java.util.Arrays;
/**
* 插入排序在数据量比较小或者基本有序时,是最好的选择,常会被用在较复杂的排序算法的最后阶段
* 插入排序是稳定的
* 属于插入类排序
* @author Administrator
*
*/
public class InsertionSort {
private int[] items;
private int size;
public InsertionSort(int[] arrays) {
size = arrays.length;
items = Arrays.copyOf(arrays,size);
}
public int[] sort() {
for(int i = 1; i < size;i++) {
int key = items[i];
int j = i-1;
for(; j > -1 && items[j]>key; j--)
{
items[j+1] = items[j];
}
items[j+1]=key;
}
return items;
}
public static void main(String[] args)
{
int[] a= new int[] {1,0,9,8,7,6,5,4,3,3,2,1,11};
InsertionSort m = new InsertionSort(a);
a = m.sort();
for(int item : a)
{
System.out.print(item + " ");
}
}
}
使用泛型实现版
package merge;
import java.util.Arrays;
/**
* 使用泛型实现的插入算法
* @author Administrator
*
* @param <T>
*/
public class InsertionSort2<T extends Comparable<T>> {
private T[] items;
private int size;
public InsertionSort2(T[] arrays) {
size = arrays.length;
items = Arrays.copyOf(arrays,size);
}
public T[] sort() {
for(int i = 1; i < size;i++) {
T key = items[i];
int j = i-1;
for(; j >=0 && (items[j].compareTo(key) >0); j--)
{
items[j+1] = items[j];
}
items[j+1]=key;
}
return items;
}
public static void main(String[] args)
{
//Integer[] a= new Integer[] {1,0,9,8,7,6,5,4,3,3,2,1,11};
Character[] a = new Character[] {'a','c','e','d','b','a'};
InsertionSort2 m = new InsertionSort2(a);
// a = (Integer[])m.sort();
a = (Character[])m.sort();
for(Character item : a)
{
System.out.print(item + " ");
}
}
}