Dowemo
0 0 0 0

import java.util.ArrayList;import java.util.Arrays;import java.util.List;publicclassPermutation {publicstaticvoidmain(String[] args){
 int nums[] = {12, 2, 3,9,1,15,4,20,10,-1};
 boolean isUsed[] = newboolean[nums.length];
 List<Integer> result = new ArrayList<Integer>(); 
 List<List<Integer>> resultList = new ArrayList<List<Integer>>();//doPermute(nums, isUsed, result, resultList);//System.out.println(resultList.toString()); Object[] aa = result.toArray();
 //result.toArray(a) recMergeSort(nums, 0, nums.length-1);
 }
 publicstaticvoiddoPermute(int[] nums, boolean[] isUsed, List<Integer> result, List<List<Integer>> resultList){
 if(result.size()==nums.length){
 resultList.add(new ArrayList<>(result));
 return;
 }
 for(int i = 0;i<nums.length;i++){
 if(!isUsed[i]){
 result.add(nums[i]);
 isUsed[i] = true;
 doPermute(nums, isUsed, result, resultList);
 result.remove(result.size()-1);
 isUsed[i] = false;
 }
 }
 }
 staticint j =0;
 publicstaticvoidrecMergeSort(int[] arr, int low, int upper){
 List<Integer> list = new ArrayList<Integer>();
 for(int i =low;i<=upper;i++){
 list.add(arr[i]);
 }//System.out.println("sub array:"+Arrays.asList(arr)); System.out.println("sub array:"+(j++)+""+list);
 if(low == upper){
 return;
 }
 else{
 int mid = (low + upper)/2;
 recMergeSort(arr, low, mid);
 recMergeSort(arr, mid+1, upper);
 }
 }
}

The results are as follows:

subarray: 0 [12, 2, 3, 9, 1, 15, 4, 20, 10, -1]subarray: 1 [12, 2, 3, 9, 1]subarray: 2 [12, 2, 3]subarray: 3 [12, 2]subarray: 4 [12]subarray: 5 [2]subarray: 6 [3]subarray: 7 [9, 1]subarray: 8 [9]subarray: 9 [1]subarray: 10 [15, 4, 20, 10, -1]subarray: 11 [15, 4, 20]subarray: 12 [15, 4]subarray: 13 [15]subarray: 14 [4]subarray: 15 [20]subarray: 16 [10, -1]subarray: 17 [10]subarray: 18 [-1]



Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs