Dark Horse Programmer--java Basic Learning Note 5

Source: Internet
Author: User

Dark Horse Programmer--java Basic Learning Note 6I. Summary of the contents of the notes:array-second definition format, array-common operations-traverse-max-Select sort-bubble sort-sort position permutation code extract, array-sort performance problems, arrays-common features-find-binary lookup, conversion-tabular-integration. second, the introduction of common content:1. Three ways to initialize an array:int[] arr = new int[3];int[] arr = new int[]{1,2,3};int[] arr = { A-i};2. Table-checking method:if the corresponding relationship in the data, and the corresponding party is an ordered number of numbers, and used as a corner label, then you have to think of the use of the array (you can also use the Map collection), then you can store the data in the array, according to the results of the operation as a corner tag directly to the corresponding elements in the array Can, this method is called: Look up the table method. 3. The right movement for >> operation Speed, the following two is equivalent. mid = (max+min)/2; Mid = (max+min) >>1;Iii. Classic Examples and explanations:1. Select the sorting algorithm:
Package com.date5;/** * Choose a sorting idea: (You can sort by determining the maximum value, or you can sort by determining the minimum value) * 1. First, the first element of the array is compared to the order of each element except itself, * If the first element is greater than one of the remaining elements, the content is swapped. * 2. After a round of comparisons, the first element is the smallest element in the array. Then the second element is compared to the first element and beyond itself, if the * two element is greater than the remaining element, the content is swapped. At this point, the second element is the second-lowest element in the number * group. * 3. And so on, until the last element. * * 4. The following Mysort method does not return an int array because the ARR reference variable is a reference to the array passed as a parameter in the Mysort method, and after the Mysort method executes, we can still manipulate the incoming array through ARR reference variable. Therefore, it is not necessary to get through the return value again. */public class Demo3 {private static int count = 0;public static void Main (string[] args) {int[] arr = {89,34,-270,17,3,100 }; System.out.print ("Pre-sorted array:");p Rintarray (arr); Mysort (arr); System.out.print ("sorted array:");p Rintarray (arr); SYSTEM.OUT.PRINTLN ("Total number of swaps:" +count);} Sort an array public static void Mysort (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]) {change (arr,i,j); count++;}}}  Swap function private static void change (int[] Arr,int i,int j) {//using ^ xor operator arr[i] = arr[i] ^ arr[j];arr[j] = Arr[i] ^ arr[j];arr[i] = Arr[i] ^ Arr[j];/*int temp = Arr[i];arr[i]= Arr[j];arr[j] = temp;*/}//print array private static void PrintArray (int[] arr) {for (int i=0;i<arr.length;i++) {if (i== arr.length-1) {System.out.println ("," +arr[i]+ "]"); if (i==0) System.out.print ("[" +arr[i]); ElseSystem.out.print ("," +arr[i]);}}}
2. Select the Sorting algorithm optimization:
Package com.date5;/** * Efficiency Analysis: * Select the sorting algorithm optimization, because the result of selecting a sort round comparison is to get the minimum value *, so we do not need to call multiple Exchange methods in this round comparison, only need to use a variable * to record this round comparison of the most  The decimal value num and its angle index, and finally in comparison, if the minimum value * is itself, then even the need to call the Exchange method is not necessary, so as to reduce the number of exchanges in the way *, theoretically improve efficiency, I define a variable count to record two traditional ways and the efficiency of this way. */public class Demo4 {//defines a global variable to record the number of times the interchange is private static int count = 0;public static void Main (string[] args) {//1. Initialize array int [] arr = {89,34,-270,17,3,100}; System.out.print ("Before sorting:"); Arr_print (arr);//2. Call sorting Method My_sort (arr); System.out.print ("After sorting:");//3. Print Results Arr_print (arr); SYSTEM.OUT.PRINTLN ("Total number of swaps:" +count);}  private static void My_sort (int[] arr) {for (int i=0;i<arr.length-1;i++) {////Record the current comparison's self value and the corner label int num = Arr[i];int index = i;for (int j=i+1;j<arr.length;j++) {//min value and its corner label are recorded if (Num>arr[j]) {num = Arr[j];index = j;}} If the minimum value is itself, the interchange operation is not performed, otherwise if (I!=index) {change (arr,i,index) is executed; count++;}} Interchange function private static void change (int[] arr, int i, int index) {int temp = Arr[i];arr[i] = Arr[index];arr[index] = temp;} print function private static void Arr_print (int[] arr) {SYstem.out.print ("["); for (int i=0;i<arr.length;i++) {if (i!=arr.length-1) {System.out.print (arr[i]+ ",");} Else{system.out.println (arr[i]+ "]");}}}
3. Binary lookup algorithm (also known as binary lookup method)
package com.date5;/** * binary lookup algorithm: (a method of quickly locating elements based on an ordered array) * 1. Set three variables to record the corner label: Min, Max, Mid, Min Initializes a value of 0,max as the maximum angle index for the array, and mid for * (Max+min)/2. * 2. See if the elements of the mid corner are equal to the value to be found, and if they are equal, return the corner mark value directly, and the program terminates execution. * 3. If the value you are looking for is less than the value of the element in the corner labeled Mid, then the position of the element you are looking for may be set between Min and mid-Mark *, reset max = Mid-1,mid = (max+min)/2, repeat steps 1th and 2 * 4. If the value you are looking for is greater than the corner mark is mid Corresponds to the element value of the corner label, the position of the element to be found may be between mid and Max *. Set min = Mid +1,mid = (max+min)/2, repeat steps 1th, 2. * 5. If the element to be found does not exist in the array, then according to the above process, the final min angle will be greater than the max angle value, at which time *-1 is returned. */public class Demo6 {public static void main (string[] args) {int[] arr = {1,2,3,4,5,6,7,8,9};int index = mybinarysearch (AR r,10); System.out.println ("The corresponding angle mark is:" +index);} public static int Mybinarysearch (int[] arr,int x) {int max = Arr.length-1;int min = 0;int mid = (max+min)/2;//defines a return value int te MP = 0;//This loop is executed as long as it is different from the value found (arr[mid]!=x) {//If the number you are looking for is greater than the median if (arr[mid]<x) {min = mid + 1;} If the number you are looking for is smaller than the median value if (arr[mid]>x) {max = mid-1;} Mid = (max+min)/2;//returns -1IF (Min>max) {return-1;} if the element is not in the ordered array. return mid;}} 
4. Get a decimal representation of 2, 8, and 16 of the binary
Package com.date5;/** * Get a decimal representation of 2, 8, 16 binary * NOTE: * If the data has a corresponding relationship, and the corresponding party is an ordered number, and used as a * angle, it is necessary to think of the application of the array. * Idea: * 1. First, if the incoming decimal number is 0, then its 2, 8, 16 is 0, directly return * 0, no need to execute the rest of the program. * 2. As shown in the following example, to convert a decimal number to a hexadecimal number: * 60 and 15 are performed with the operation, and the value is the lowest digit of the hexadecimal of 60. * Then the 60 unsigned right shift 4 bit, and then 15 with the operation, the value is 60 of the hexadecimal second-to-last. * 3. As can be concluded from the above example, the conversion of a decimal number to 16 binary is: * The decimal number and 15 phase, the result is stored to the lowest bit of an array. * Then the decimal number is shifted to the right 4 bits, and then with 15 and the operation, the value is the number corresponding to the hexadecimal reciprocal * second bit. * Then move right 4 for, with 15 and until the phase result is 0. * 4. In turn, it can be inferred that the law of 10 conversion to 2 and 8 is similar to converting to 16, except that the offset * amount differs from the number of phases. * 10 binary is converted to 2 with an offset of 1 and a phase number of 1. * 10 binary is converted to 8 with an offset of 3 and a phase number of 7. */public class Demo7 {public static void main (string[] args) {Tohex ($); ToBin (-6);} public static void toBin (int num) {//decimal is converted to binary and digit 1, offset 1trans (num,1,1);} public static void Tohex (int num) {trans (num,15,4);} Universal conversion method public static void trans (int num, int base, int offset) {if (num = = 0) {System.out.println ("0"); return;} Char[] CHS = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '};//create an array to hold the converted result Char[] arr = new char[32];//records the corner mark that the current POS is moved to, int pos = arr.length;    while (num!=0) {int temp = num & base;    Starting from the highest bit to save arr[--pos] = Chs[temp];    unsigned right shift num = num >>> offset;    } System.out.println ("pos=" +pos);    for (int x = pos;x<arr.length;x++) {System.out.print (arr[x]); } System.out.println ();}}





Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Dark Horse Programmer--java Basic Learning Note 5

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.