Array sorting and Recursion -- (Java study note 2)

Source: Internet
Author: User

Array sorting and Recursion -- (Java study note 2)
Ascending: Select sort: select an element, and compare it with the following element at a time. If the comparison element after the selected element is heavy rain, the minimum value and the maximum value appear at the switching position first. 123456789101112131415161718192021222324252627282930 public static void main (String [] args) {int [] arr = {5, 8, 9, 12, 55,565,421,-5,-56}; sortMethod (arr ); p (arr);} // sort the core code private static void sortMethod (int arr []) {for (int I = 0; I <arr. length-1; I ++) {for (int j = I + 1; j <arr. length; j ++) {if (arr [I]> arr [j]) {int temp = arr [j]; arr [j] = arr [I]; arr [I] = temp ;}}// print the array element static void p (I Nt [] arr) {String ret = "["; for (int I = 0; I <arr. length; I ++) {ret + = arr [I]; if (I = arr. length-1) {ret + = "]";} else {ret + = "," ;}} System. out. println ("---------->" + ret); Bubble Sorting: the two adjacent elements first show the maximum value and then the minimum value. Copy the public static void main (String [] args) {int arr [] = {6, 9, 4589,442,458,523 2,-788,7, 545,-44,55,-11 }; sortMethod (arr); p (arr);} private static void p (int [] arr) {String ret = "["; for (int I = 0; I <arr. length; I ++) {ret + = arr [I]; if (I = arr. length-1) {ret + = "]";} else {ret + = "," ;}} System. out. println ("---------->" + ret);} private static void sortMethod (int [] arr) {for (int j = 1; j <Arr. length-1; j ++) {for (int I = 0; I <arr. length-j; I ++) {if (arr [I]> arr [I + 1]) {int temp = arr [I]; arr [I] = arr [I + 1]; arr [I + 1] = temp ;}}copy the code Binary Search Method: first sort the code and then take the median value, compare the specified value with the median value. If the value is smaller than the median value, the search range is changed to the smallest index value to the median index minus 1. If the specified value is greater than the median value, then the search range is changed to the index with the median value plus 1 to the maximum. Advantage: narrow the search scope and improve performance. Copy code 1 // to find the index value of the specified Element in the specified array. 2 public static void main (String [] args) {3 int [] arr = {5, 4, 231,45, 75,45, 11,-11,-21,-45 }; 4 int key = 45; 5 sortMethod (arr); 6 printSort (arr); 7 int index = binarySeach (arr, key, 0, arr. length-1); 8 System. out. println ("-----------> index value:" + index); 9} 10 // Binary Search core code 11 private static int binarySeach (int [] arr, int key, int fromIndex, int toInedx) {12 // minimum index and maximum Index 13 // fromIndex indicates the start position of the query and toIndex indicates the position 14 int minIndex = fromIndex, maxIndex = toInedx; 15 while (maxIndex> = minIndex) {16 // intermediate Index 17 int midIndex = (maxIndex + minIndex)/2; 18 // The value of the intermediate index 19 int midIndexVal = arr [midIndex]; 20 if (key> midIndexVal) {21 minIndex = midIndex + 1; 22} else if (key <midIndexVal) {23 maxIndex = midIndex-1; 24} else {25 return midIndex; 26} 27} 28 return-1; 29} 30 // print the sorted code 31 private static void printSort (int [] arr) {3 2 String ret = "["; 33 for (int I = 0; I <arr. length; I ++) {34 ret + = arr [I]; 35 if (I = arr. length-1) {36 ret + = "]"; 37} else {38 ret + = ","; 39} 40} 41 System. out. println ("-----------> sorting:" + ret); 42} 43 // sort the Code 44 private static void sortMethod (int [] arr) {45 for (int j = 0; j <arr. length-1; j ++) {46 for (int I = 0; I <arr. length-j-1; I ++) {47 if (arr [I]> arr [I + 1]) {48 int temp = arr [I]; 49 arr [I] = arr [I + 1 ]; 50 arr [I + 1] = temp; 51} 52} 53} 54} copy the code array: copy code 1/** 2*3 * @ author Essence 4 * arraycopy (Object src, int srcPos, Object dest, int destPos, int length) 5 * This method receives four parameters 6 * object src: the original array, that is, the copied array 7 * int srcPos: the target position of the original array is the position from which the Object is copied. 8 * Object dest: the destination array is used to store the copied array 9 * int destPos: the starting position in the target array, 10 * int length: number of elements to be copied 11 */12 public static void main (String [] args) {13 String [] arr = {"", "B", "C", "D"}; 14 String [] arr1 = new String [5]; 15 System. out. println (Arrays. toString (arr1); // before copying: [null, null] 16 System. arraycopy (arr, 1, arr1, 2, 3); 17 System. out. println (Arrays. toString (arr1); // After copying: [null, null, B, C, D] 18 19} copy the variable parameters of the Code: copy code 1/** 2 * New features in Java 5 variable parameters 3 * variable parameters must be used as the last parameter of the parameter, the parameter can directly pass 0 to n 4 * which is essentially an array 5 * method (int... arr) 6 * @ author Essence 7*8 */9 Public static void main (String [] args) {10 int [] arr = {,}; 11 System. out. println (getSum (arr); 12/** 13 * Variable Parameter call features: 14 * getSum (,) 15 */16 System. out. println (getSum (,); 17 18} 19 private static int getSum (int... arr) {20 int sum = 0; 21 for (int I = 0; I <arr. length; I ++) {22 sum + = arr [I]; 23} 24 return sum; 25} 26} the variable copy code parameter can be called as an array: getSum (new int [] {1, 2, 3, 4, 5}); you can also directly write a parameter with any number, But the types must be consistent: getSum (,) recursion: the basic idea is to call its own structure: recursive header: defines when the recursion ends and does not call its own method. If no header is defined, it will fall into an endless loop recursive body: when do you need to call your own Method to Solve the factorial problem using recursion: copy code 1 public class Demo {2 public static void main (String [] args) {3 long sum = factorial (10); 4 System. out. println (sum); 5} 6 static long factorial (int n) {7 if (n = 1) {8 return 1; 9} else {10 return n * factorial (n-1); 11} 12} 13} copy two questions in the Java programming ideology: copy the code/**, and 34 *. The two numbers are the third and * Fibonacci series * F (n) = F (n-1) + F (n-2) */Method 1: private static void fibonacci (int n) {int arr [] = new int [n], sum = 0; arr [0] = arr [1] = 1; for (int I = 2; I <arr. length; I ++) {arr [I] = arr [I-1] + arr [I-2]; System. out. println ("arr [" + I + "]" + arr [I]); sum + = arr [I];} System. out. println ("the sum of the Fibonacci series:" + sum);} Method 2: private static int sumFibonacci (int n) {if (n <1) {return 1 ;} else {return sumFibonacci (n-1) + sumFibonacci (n-2) ;}} private static void getFibonacci (int n) {for (int I = 0; I <= n; I ++) {int f = sumFibonacci (I); System. out. print (f + "\ t"); if (I % 3 = 0) {System. out. println () ;}} method 3: private static void maid (int n) {int a = 1, B = 1, c = 0, sum = 0; System. out. println (a + "\ t" + B + "\ t"); for (int I = 1; I <= n; I ++) {c = a + B; a = B; B = c; sum + = c; System. out. print (c + "\ t"); if (I % 3 = 0) {System. out. println () ;}} System. out. println ("the sum of the Fibonacci series:" + sum);} copy the code vampire Number: copy the code/** 1260 = 21*60*1827 = 21*87*2187 = 27*81 */1 private static void vampireNumber1 () {2 for (int I = 1; I <100; I ++) {3 for (int j = 1; j <100; j ++) {4 if (I * j> 1000) {5 String a = I + "" + j; 6 String B = I * j + ""; 7 if (equals (a, B) {8 System. out. println (I + "\ t" + j + "\ t" + I * j); 9} 10} 11} 12} 13} 14 private static boolean equals (String, string B) {15 // TODO Auto-generated method stub16 char [] arrays rays, bArrays; 17 bytes rays =. toCharArray (); 18 bArrays = B. toCharArray (); 19 Arrays. sort (Arrays rays); 20 Arrays. sort (bArrays); 21 if (Arrays. equals (arrays rays, bArrays) {22 return true; 23} 24 return false; 25}

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.