码迷,mamicode.com
首页 > 编程语言 > 详细

全排序的两种想法

时间:2015-09-07 14:20:02      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

 字典序全排序:

1
import java.util.Arrays; 2 import java.util.Scanner; 3 public class Cao41 { 4 /** 5 * @param 第一行输入个数N,第二行输入序列1-9, 6 * 输出字典序排列
具体做法如下:
首先要将数组a从小到大排序,输出第一个排序,然后进入循环
先用getindex方法找到最靠后的,后面有比啊a[index]大的index,当index不存在设index=-1,结束循环。
然后从 index到end找出比a[index]大的数中最小的啊a[min].
将a[index]与a[min]交换,再将数组a从index到end排序,打印数组
7 */ 8 public static void getAllOrder(int[] a,int N){ 9 int end=N-1; 10 int index=end; 11 sort(a,0,N-1); 12 System.out.println(Arrays.toString(a)); 13 do{ 14 index=getIndex(a,end); 15 if(index==-1)break; 16 int min=getMin(a,index,end); 17 swap(a,index,min); 18 sort(a,index+1,end); 19 System.out.println(Arrays.toString(a)); 20 }while(index!=-1); 21 } 22 /** 23 * @return 找出数组a中后续数字有比a[index]大的数字,且最靠后的index 24 */ 25 public static int getIndex(int[] a,int end){ 26 for(int i=end-1;i>=0;i--){ 27 if(a[i]<a[i+1]) return i; 28 } 29 return -1; 30 } 31 /** 32 * @param 数组a从index开始到end为止找出最小的且比a[index]大的数 33 */ 34 public static int getMin(int[] a,int index,int end){ 35 if(end==index+1)return end; 36 int minindex=index+1; 37 for(int i=index+1;i<=end;i++){ 38 if(a[index]<a[i]&&a[i]<a[minindex])minindex=i; 39 } 40 return minindex; 41 } 42 43 /** 44 * 对数组a从begin到end进行冒泡排序 45 */ 46 public static void sort(int[] a,int begin,int end){ 47 if(begin==end)return; 48 for(int i=begin;i<=end;i++) 49 { 50 for(int j=i;j<=end;j++){ 51 if(a[j]<a[i])swap(a,i,j); 52 } 53 } 54 } 55 56 /** 57 * @将数组 a的a[from]与a[to]交换 58 */ 59 public static void swap(int[] a,int from,int to){ 60 int temp=a[from]; 61 a[from]=a[to]; 62 a[to]=temp; 63 } 64 65 66 public static void main(String[] args) { 67 Scanner cin=new Scanner(System.in); 68 int N=cin.nextInt(); 69 int[] a=new int[N]; 70 for(int i=0;i<N;i++){ 71 a[i]=cin.nextInt(); 72 } 73 74 getAllOrder(a,N); 75 } 76 }

全排列:模拟全排列,依次取各位的数,每取完依次输出一次 具体操作就是n中取1,n-1中取1......注意每次操作完要回溯还原现场
 1 public class Cao13 {
 2 
 3     /**
 4      * @输出数组全排列
 5      */
 6     static int j=0;
 7     static String s="";
 8     public static void mypermutation(int[] a,int begin,int end){    
 9         if(begin==end)
10         {
11             String temp="";
12             for(int i=0;i<a.length;i++)
13             {
14                 temp+=a[i];
15             }
16             if(!s.contains(temp)){
17                 System.out.println(j+"行:"+temp); 
18                 s+=temp+" ";
19             j++;
20             }
21         }
22         else{
23             for(int i=begin;i<=end;i++)
24            {
25             swap(a,begin,i);
26             mypermutation(a,begin+1,end);
27             swap(a,i,begin);
28            }
29         }
30         
31     }
32     public static void swap(int[] a,int from,int to){
33         int temp=a[from];
34         a[from]=a[to];
35         a[to]=temp;
36     }
37     
38     public static void main(String[] args) {
39         int[] b={1,2,2};
40         mypermutation(b,0,2);
41     }
42 }

 



 

全排序的两种想法

标签:

原文地址:http://www.cnblogs.com/guizhongyi/p/4788668.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!