插入排序之java实现

package com.cb.java.algorithms.datastructure.yanweimindatastructure.sort;


/**
 * 插入排序
 * 
 * @author 36184
 *
 */
public class InsertSort<T extends Comparable<T>> {


/**
* 插入排序

* @param arr
*/
public void insertSort(T[] arr) {
for(int i=0;i<arr.length-1;i++)
{
for(int j=i+1;j>0;j--)
{
T temp=arr[j];
if(temp.compareTo(arr[j-1])<0)
{
arr[j]=arr[j-1];
arr[j-1]=temp;
}
}
}
}
public static void main(String[] args) {
InsertSort<Integer>bubb=new InsertSort<>();
Integer[]arr=new Integer[]{3,2,10,5,35,27,1000,453,34,57,89};
bubb.insertSort(arr);
for(Integer i:arr)
{
System.out.print(i+" ");
}
}
}

猜你喜欢

转载自blog.csdn.net/u013230189/article/details/80676893
今日推荐