My algorithm Learning (1) ---- full array Arrangement

Source: Internet
Author: User

Seeing the beautiful and practical code written by others, I finally made up my mind to study algorithms and record my learning achievements here.

Two days ago, I saw the full array arrangement, so I realized it according to other people's ideas. I felt like I understood it. I was a little happy. I recorded it.

/** Full array arrangement */public class myAllSort {public static void sort (int [] number, int start, int end) {int temp; // if the elements in the array are lost to the last one, the output proves that the elements have met the requirements. if (start = end) {print (number );} // In turn, the first element of the array and the subsequent element are called one by one, and then recursively after the first pair, // the second element of the array and its subsequent elements are called, last run/** 1 2 3 4 --> 1 2 3 4 --> 1 2 3 4*1 4 3 3 3 2 4 --> 1 3 2 4 4*1 3 4 2*1 4 3 2 --> 1 4 3 2*1 4 2 3*2 1 3 4*3 2 1 4*4 2 3 1 **/for (int I = start; I <end; I ++) {temp = number [start]; number [start] = number [I]; number [I] = temp; sort (number, start + 1, end); // print (number); // output the entire sorting process // the original order of the array after the pair is dropped, so that the next time temp = number [start]; number [start] = number [I]; number [I] = temp; //} public static void print (int [] number) {for (int I = 0; I <number. length; I ++) {System. out. print (number [I] + "--");} System. out. println ();} public static void main (String [] args) {int [] number = {1, 2, 4}; sort (number, 0, number. length );}}
There is also an array selection and sorting method. I feel this idea is very useful to me, and I forgot the specific source.

Import java. util. arrayList; import java. util. arrays; import java. util. list;/** depending on how to select a few numbers for sorting * For example: Five to choose three full sorting * algorithm principle: select a number in the array and store it in the Set first, then select other numbers, **/public class ChooseServalFromNumber {public static final int NUM = 4; public static void main (String [] args) {Object [] obj = new String [] {"1", "2", "3", "4"}; List <Object> list = Arrays. asList (obj); chooseNumberSort (list, new ArrayList <Object> ();} public static void chooseNumberSort (List <Object> list, List <Object> target) {// if the number in the target set reaches the number we need, output // if (target. size () = NUM) // System. out. println (target + "--"); if (target. size () = 4) System. out. println (list + "-->" + target + "-- output"); // else // System. out. println (list + "-->" + target); for (int I = 0; I <list. size (); I ++) {List <Object> newList = new ArrayList <Object> (list); List <Object> newTarget = new ArrayList <Object> (target ); newTarget. add (newList. get (I); newList. remove (I); System. out. println (list + "-->" + target); chooseNumberSort (newList, newTarget) ;}}/* output result: [1, 2, 3, 4] --> [] [2, 3, 4] --> [1] [3, 4] --> [1, 2] [4] --> [1, 2, 3] [] --> [1, 2, 3, 4] -- output [3, 4] --> [1, 2] [3] --> [1, 2, 4] [] --> [1, 2, 4, 3] -- output [2, 3, 4] --> [1] [2, 4] --> [1, 3] [4] --> [1, 3, 2] [] --> [1, 3, 2, 4] -- output [2, 4] --> [1, 3] [2] --> [1, 3, 4] [] --> [1, 3, 4, 2] -- output [2, 3, 4] --> [1] [2, 3] --> [1, 4] [3] --> [1, 4, 2] [] --> [1, 4, 2, 3] -- output [2, 3] --> [1, 4] [2] --> [1, 4, 3] [] --> [1, 4, 3, 2] -- output [1, 2, 3, 4] --> [] [1, 3, 4] --> [2] [3, 4] --> [2, 1] [4] --> [2, 1, 3] [] --> [2, 1, 3, 4] -- output [3, 4] --> [2, 1] [3] --> [2, 1, 4] [] --> [2, 1, 4, 3] -- output [1, 3, 4] --> [2] [1, 4] --> [2, 3] [4] --> [2, 3, 1] [] --> [2, 3, 1, 4] -- output [1, 4] --> [2, 3] [1] --> [2, 3, 4] [] --> [2, 3, 4, 1] -- output [1, 3, 4] --> [2] [1, 3] --> [2, 4] [3] --> [2, 4, 1] [] --> [2, 4, 1, 3] -- output [1, 3] --> [2, 4] [1] --> [2, 4, 3] [] --> [2, 4, 3, 1] -- output [1, 2, 3, 4] --> [] [1, 2, 4] --> [3] [2, 4] --> [3, 1] [4] --> [3, 1, 2] [] --> [3, 1, 2, 4] -- output [2, 4] --> [3, 1] [2] --> [3, 1, 4] [] --> [3, 1, 4, 2] -- output [1, 2, 4] --> [3] [1, 4] --> [3, 2] [4] --> [3, 2, 1] [] --> [3, 2, 1, 4] -- output [1, 4] --> [3, 2] [1] --> [3, 2, 4] [] --> [3, 2, 4, 1] -- output [1, 2, 4] --> [3] [1, 2] --> [3, 4] [2] --> [3, 4, 1] [] --> [3, 4, 1, 2] -- output [1, 2] --> [3, 4] [1] --> [3, 4, 2] [] --> [3, 4, 2, 1] -- output [1, 2, 3, 4] --> [] [1, 2, 3] --> [4] [2, 3] --> [4, 1] [3] --> [4, 1, 2] [] --> [4, 1, 2, 3] -- output [2, 3] --> [4, 1] [2] --> [4, 1, 3] [] --> [4, 1, 3, 2] -- output [1, 2, 3] --> [4] [1, 3] --> [4, 2] [3] --> [4, 2, 1] [] --> [4, 2, 1, 3] -- output [1, 3] --> [4, 2] [1] --> [4, 2, 3] [] --> [4, 2, 3, 1] -- output [1, 2, 3] --> [4] [1, 2] --> [4, 3] [2] --> [4, 3, 1] [] --> [4, 3, 1, 2] -- output [1, 2] --> [4, 3] [1] --> [4, 3, 2] [] --> [4, 3, 2, 1] -- output */

Related Article

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.