The interview question: The programming realizes all arranges, chooses arranges the Java algorithm to talk about __ajax

Source: Internet
Author: User

There are a number of ways to do this, and here's an algorithm that's easiest to understand, but it's a little bit harder.

Import java.util.ArrayList;   
Import Java.util.Arrays;   
   

Import java.util.List;   
   
    public class Fullsort {//num set to the length of the array to be arranged is the choice to arrange private static int NUM = 3; /** * Recursive algorithm: divides the data into two parts, recursively moves the data from the left to the right to implement the choice arrangement * * @param datas * @param target/private static V   
                OID sort (list datas, list target) {if (target.size () = NUM) {for (Object obj:target)   
            System.out.print (obj);   
            System.out.println ();   
        Return   
            for (int i = 0; i < datas.size (); i++) {List Newdatas = new ArrayList (datas);   
            List newtarget = new ArrayList (target);   
            Newtarget.add (Newdatas.get (i));   
            Newdatas.remove (i);   
        Sort (Newdatas, newtarget); } public static void Main (string[] args) {string[] datas = new string[] {"A", "B", "C", "   
        D "}; Sort (arrays.aslist(datas), New ArrayList ()); }   
   
}


This code inside the return use is very powerful, everybody oneself slowly realize ~

Here is a full array of algorithms, but also very clever simple ~

public class allsort{public  
    static void Main (string[] args) {  
        char buf[]={' A ', ' B ', ' C '};  
        Perm (buf,0,buf.length-1);  
    }  
    public static void Perm (char[] buf,int Start,int end) {  
        if (start==end) {//when only one letter in an array is required to be fully arranged, it can be output as long as it is specified (special case) for  
            (int i=0;i<=end;i++) {  
                System.out.print (BUF);  
            }  
            System.out.println ();     
        }  
        else{//multiple alphabetic (Universal) 
            for (int i=start;i<=end;i++) {//(let pointer start point to each number) 
                char temp=buf[start];// Exchange array the first element buf[start]=buf with the subsequent elements  
                ;  
                Buf=temp;  
                  
                Perm (buf,start+1,end);//Subsequent elements recursive full arrangement  
                  
                temp=buf[start];//restore the swapped array to  
                buf[start]=buf;  
                Buf=temp}}}}  



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.