全排列

import java.util.*;
class TestSortDemo
{
	public static void main(String[] args)
	{
		Scanner sc =new Scanner(System.in);
		System.out.println("请输入数组的成员,over代表结束:");
		int [] a = new int [10];
		int k = 0;
		while(true)
		{
			String s = sc.next();
			if("over".equals(s))break;
			a[k++] = Integer.parseInt(s);
		}
		sortFull(a,0,k-1);
	}

	private static void sortFull(int [] a,int k,int m)
	{
		if(k<=m)
		{
			for(int i=k;i<=m;i++)
			{
				swap(a,k,i);
				sortFull(a,k+1,m);
				swap(a,k,i);
			}
		}
		else
		{
			for(int j =0;j<=m;j++)
				System.out.print(a[j] + " ");
			System.out.println();
		}
	}

	private static void swap(int []a,int i,int j)
	{
		int temp = a[i];
		a[i] = a[j];
		a[j] = temp;
	}
}

posted @ 2015-01-23 13:48  静以养身 俭以养德  阅读(120)  评论(0编辑  收藏  举报