`

直接插入排序代码实现

J# 
阅读更多
package sort;

/**
 * @author linjia
 * 直接插入排序
 */
public class StraightInsertSort {
	
	public int[] sort(int r[],int n)
	{
		for(int i=1;i<n;i++)
		{
			int temp=r[i];
			int j=i-1;
			while(j>=0 && temp<r[j])
			{
				r[j+1]=r[j];
				j--;
			}
			r[j+1]=temp;
		}
		return r;
	}
	
	public static void main(String[] args) {
		int[] test={3,9,8,55,97,33,6,1};
		
		test=new StraightInsertSort().sort(test,test.length);
		
		System.out.println(test.length);
		
		for(int i:test)
		{
			System.out.println(i);
		}
	}
	
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics