Problem one: Find the maximum (maximum or minimum)
Method One: With an intermediate variable (default is a[0]) compared to each value of the array, the big one is assigned to the middle variable
Method Two: Find the maximum subscript, with a subscript variable (the default ' 0 ') and the array of each value comparison, large to assign the value of the subscript to the variable
Problem two: Array lookup operations. (Application of dichotomy: for ordered sequences)
Exercise: There is an ordered array, and you want to insert an element into the array,
Also make sure that the array is ordered. How to get the position of the element in the array.
That is, in [1,3,6,8,9], insert a 7, and keep it in order
1 //using the dichotomy method to find out2 //Dichotomy : Focus on the middle value to start the comparison, key big words with the median after a start to find, the opposite analogy3 //key Small to the middle value of the first place to start looking for4 Public Static intHalfsearch (int[]arr,intkey)5 { 6 //Set the subscript for the first and the end of the range you want to query7 intS=0,e=arr.length-1;8 //set an intermediate value subscript, Default-19 inth;Ten //when the first is less than the end of the cycle, there is no number of cycles required. So use while One while(s<=e) A { - //Intermediate variable subscript, -H= (s+e)/1; the if(arr[h]<key) - { -s=h+1;//must be +1, starting with this - //System.out.println (h); + } - Else if(arr[h]>key) + { AE=h-1;//must-1, as the end point at } - Else if(arr[h]==key) - { - //in the third case, the two are equal, then jump out of the loop - returnh; - } in //System.out.println ("DD"); - } to //This must be s, not H, because one case in the while loop is Arr[h]==key, and the other is the following + returns; -}
Question two: sort (from small to large)
Java sorting method: 1. Bubble sort. 2. Select Sort. 3. Insert Sort. 4. Quick Sort.
1. Bubble Sorting Method:
1 //Bubble Sort method: Starting from a[0], each adjacent number is compared, larger, both exchanged. 2 //The first time you swap the maximum value to the last, the second to the second, and so on3 //here to sort from small to large4 Public Static voidBubblesort (int[]arr]5 {6 //must be two times for loop first7 for(inti=1;i<arr.length;i++)8 {9 for(intk=0;k<arr.length-i;k++)Ten { One //swap to use the middle variable A inttmp; - if(arr[k]>arr[k+1]) - { theTmp=arr[k+1]; -arr[k+1]=Arr[k]; -arr[k]=tmp; - } + } - } +}
2. Select sort
1 //Choose the sorting method: Find the maximum, put in the last one, and then find the maximum value from the remaining number, and so on2 //The first loop, from 0 to N, uses the subscript to find the minimum value, and then swaps with the last one (n),3 //The second cycle, from 0 to N-1, finds the minimum value and then the (n-1) bit Exchange, and so on4 //first think two times for the loop!!! 5 //here is the arrangement from the big to the small,6 Public Static voidSelectsort (int[]arr]7 {8 for(inti=1;i<arr.length;i++)9 {Ten //The default subscript is 0, each time starting from 0 to compare with the back One intMin=0; A - for(intk=0;k<arr.length-i;k++) - { the //find the minimum value - if(arr[k+1]<Arr[min]) -Min=k+1; - } + //The lowest value subscript is found after the cycle is complete. - //swap with the last one!! (Exchange must think with intermediate variables!!) ) + //System.out.print (arr[min]+ "\ t"); A //intermediate variables are used to exchange at inttmp; -tmp=arr[arr.length-i]; -arr[arr.length-i]=Arr[min]; -arr[min]=tmp; - } -}
3. Insert Sort
1 //Insert Sort method: Take the first number as the starting point, compare the second number in the back, and insert the larger one behind it,2 //Otherwise, the third number begins to compare to the previous second, until it is swapped to a larger number than the previous one .3 //here the insert is actually the Exchange value (position)4 //here from small to large sort of5 Public Static voidInsertsort (int[]arr]6 {7 //to Exchange n-1 times.8 for(inti=1;i<arr.length;i++)9 { Ten //Compare insert from First and second start One //increase the number of comparisons sequentially A inttmp; - for(intk=i;k>0;k--) - { the //here is the one from the very back, that is, I began with the previous comparison - if(arr[i]<arr[k-1]) - { - //Implementing an Exchange +tmp=Arr[i]; -Arr[i]=arr[k-1]; +arr[k-1]=tmp; A //Note that after the exchange, I is the subscript to change the position after the exchange of the subscript namely K-1 atI=k-1; - } - //if the last one is greater than the penultimate one, you do not have to loop - Else Break; - } - } in}
4. Quick Sort
// Quick Sorting Method (pit Fill + divide-and-conquer method): Public Static void quiksort (int []arr) { }
java-array-additions and deletions