Stay hungry, stay foolish !

I've failed over and over and over again in my life. And that is why I succeed.

算法导论学习2.1 插入排序

public class InsertionSort 
{
	public static void main(String[] args)
	{
		
		int[] a = {5,2,4,6,1,3,0};
		

		sort(a);
		
		for(int i = 0; i < a.length; i++)
			{
				System.out.print(a[i] + " ");
			}	
	}
	
	static void sort(int[] x)
	{
		int i, j, key;
		for(j = 1; j < x.length; j++)
		{
			key = x[j];
			i = j - 1;
			while(i >= 0 && x[i] > key)
			{
				x[i + 1] = x[i--];
			}
			x[i + 1] = key;
		}		
	}	
}



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wangbaomi/article/details/7399038
文章标签: 算法 string class
个人分类: 算法导论 JAVA
下一篇算法导论学习2.1-3 查找问题
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭