Android interview question Programming

Source: Internet
Author: User

Android interview question Programming

1. Sorting

Package cn. java. suanfa; public class SuanFa {public static void main (String [] args) {int [] arr = {5, 7, 3, 9, 1, 2}; // selectSort (arr ); system. out. println ("-------------------"); // maopaoSort (arr); quickSort (arr, 0, arr. length-1);}/*** Java Sorting Algorithm (6 ): insert sort directly * Insert sort directly. The basic operation is to insert the data elements to be sorted into the previous sequence according to the value of the keyword. * The efficiency of direct insertion is not high. In the worst case, the total number of comparisons for all elements is (0 + 1 +... + N-1) = O (n ^ 2 )*. In other cases, we also need to consider the number of Moving Elements. Therefore, the time complexity is O (n ^ 2). It is very efficient to directly insert Space. * only one cache data unit is required, that is to say, the space complexity is O (1 ). direct insertion and sorting are stable. Insert directly to sort data in a certain order, which is more efficient. However, if there is no rule for data, a large amount of data needs to be moved, and its efficiency is just as bad as that of Bubble Sorting * and selection sorting. * Algorithm Description * for a data sequence with n elements, the n-1 insertion operation is required for sorting: * 1st inserts, insert the first 2nd elements into the first ordered sub-sequence. At this time, there is only one element in front, of course, it is ordered. * Insert the first 2nd elements into the first ordered subsequence. The first two elements are ordered. * N-1 element into the first ordered subsequence. n-1-1 element is ordered. * @ Param data */public static void insertSort (int [] data) {for (int I = 1; I <data. length; I ++) {// The element value at cache I int tmp = data [I]; if (data [I] <data [I-1]) {int j = I-1; // The entire while (j> = 0 & data [j]> tmp) {data [j + 1] = data [j]; j --;} // insert tmp to the appropriate position. data [j + 1] = tmp; print (data) ;}}/ *** Java Sorting Algorithm (5): Fast sorting * fast sorting is a fast exchange sorting algorithm. Its basic idea is very simple, take any data (such as the first * data) from the data sequence to be arranged as the demarcation value. Put all the data elements smaller than it on the left, and put all the data elements larger than it To its right. This sequence forms two subsequences. The values of data elements in the left sequence are smaller than the boundary values, the value of the data element in the right sequence is greater than the value of Division. Next, perform recursive sorting on the left and right subsequences, re-select the central element for the two subsequences, and adjust according to this rule until only one element is left in each element * sub-table, and the sorting is completed. Idea: * 1. Define an I variable. The I variable starts from the first index on the left, finds the index of the element greater than the demarcation value, and records it with I. * 2. Define a j variable. The j variable starts from the first index on the right, finds the index of the element smaller than the demarcation value, and records it with j. * 3. If I
 
  
= J. It can be determined that the data element X on the left of j is less than the demarcation value, the data element on the Right of j is greater than the demarcation value, and finally the Division value can be exchanged with the elements at the j index. The best case of time complexity (always select the Intermediate Value for pivot at each time) T (n) = O (nlogn) Worst Case (always select the smallest or largest element for pivot at each time) for n-1 trip, compare n-I times for each trip, with the maximum number of comparisons: [O (n²)] average time complexity: T (n) = O (nlogn) * @ param data * @ param start * @ param end */public static void quickSort (int [] data, int start, int end) {if (start> = end) {return;} int middleVal = data [start]; int I = start + 1; int j = end; while (true) {while (I <= end & data [I] <middleVal) {I ++;} while (j> start & data [j]> middleVal) {j --;} if (I
  
   
Arr [j]) {int temp = arr [I]; arr [I] = arr [j]; arr [j] = temp; print (arr );}}}} /* ** select sorting * the basic operation for directly selecting sorting is to select the smallest (or largest) element from the data element to be sorted, the sequence is placed at the end of the sorted * series until all the data elements to be sorted are arranged. It needs to be compared by n-1. The algorithm is unstable. The extra space of O (1) is calculated. The time complexity of the comparison * is O (n ^ 2), and the time complexity of the exchange is O (n ), it is not adaptive. It is not recommended in most cases. It can be used only when * you want to reduce the number of exchanges. * @ Param arr */public static void selectSort (int [] arr) {for (int I = 0; I <arr. length-1; I ++) {int minIndex = I; for (int j = I + 1; j <arr. length; j ++) {if (arr [minIndex]> arr [j]) {minIndex = j ;}} if (minIndex! = I) {int temp = arr [I]; arr [I] = arr [minIndex]; arr [minIndex] = temp; print (arr );}}} public static void print (int [] data) {for (int I = 0; I <data. length; I ++) {System. out. print (data [I] + "\ t");} System. out. println ();} private static void swap (int [] data, int I, int j) {int temp = data [I]; data [I] = data [j]; data [j] = temp; print (data );}}
  
 


/*** Create a binary tree and sort the array * @ author libin **/public class BinaryTree {public int value; public BinaryTree left; public BinaryTree right; public BinaryTree (int value) {this. value = value;} public void addValue (int val) {if (val
 
  
Value) {if (right = null) {right = new BinaryTree (val);} else {right. addValue (val) ;}} public boolean find (int val) {if (this. value = val) {return true;} if (val> this. value) {if (right = null) {return false;} else {return right. find (val) ;}} if (val
  
   
2,



Public class CalByteNum {public static void main (String [] args) throws Exception {// corresponding byte 105-25-120-79 121 111 117 50 String str = "I love you2"; int by = calByte (str. getBytes ("GBK"), 3); System. out. println (str. substring (0, by);}/*** truncates a string, which is discarded when half a Chinese character is encountered. * CalByte (str. getBytes ("GBK"), 2) ---> output: I * calByte (str. getBytes ("GBK"), 3) ---> output: I love * @ param buf * @ param cut * @ return */public static int calByte (byte [] buf, int cut) {int resNum = 0; int chineseFlag = 0; for (int I = 0; I <cut; I ++) {if (buf [I] <0) {if (chineseFlag = 1) {chineseFlag = 0; resNum ++;} else {chineseFlag ++ ;}} else {resNum ++ ;}} return resNum ;}}


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.