1. Implement the bubble sorting algorithm, compare the adjacent elements, if the condition is satisfied to call the element value, the smaller elements moved to the front of the array, the larger elements moved to the back of the array, so that the smaller elements gradually rise from the bottom to the top, to the front.
Public classZuoye14_maopaopaixu { Public Static voidMain (string[] args) {int[] Array =New int[]{12,45,21,35,65,24,43}; for(inti=0;i<array.length;i++){ for(intj=0;j<array.length-1;j++){ if(array[i]<Array[j]) { inttemp=Array[i]; Array[i]=Array[j]; ARRAY[J]=temp; } } } for(inti=0;i<array.length;i++) {System.out.print (Array[i]+" "); } }}
2. Realize the binary search method.
Public classZuoye15_erfenchazhao { Public Static voidMain (string[] args) {int[] Array =New int[]{12,45,21,35,65,24,43}; for(inti=0;i<array.length;i++){ for(intj=0;j<array.length-1;j++){ if(array[i]<Array[j]) { inttemp=Array[i]; Array[i]=Array[j]; ARRAY[J]=temp; } } } intLeft,mid,right; Left=0; Right=array.length-1; Mid= (left+right)/2; while(array[mid]!=45){ if(45>Array[mid]) { Left=mid+1; } Else if(45<Array[mid]) { Right=mid-1; } Mid= (left+right)/2; } System.out.println (mid); }}
3. Using a variety of algorithms, simulation of the draw: from 1-36 randomly extracted 8 non-repeating numbers
Importjava.util.Arrays;ImportJava.util.Random; Public classZuoye13_yaojiangsuijishu { Public Static voidMain (string[] args) {int[] Array =New int[8];//defines an array to hold the number that is shaken toRandom A =NewRandom (); ints = a.nextint (36);//generate a random number if(s!=0) Array[0]=s;//give the first random number to the number No. 0 bit for(inti=1;i<8;i++) {//using loops to generate other random numbersS=a.nextint (36); for(intj=0;j<i;j++) {//using loops to compare if(Array[j]!=s && s!=0) {//compare to the value on each ordinal bit beforearray[i]=s; } Else{i--; Break; }}} arrays.sort (array); for(inti=0;i<array.length;i++) {System.out.print (Array[i]+" "); } }}
4, the direct selection of the sorting, that is, the specified sort position and other array elements are compared, if the condition is met to exchange element values, the difference bubble sort, not the exchange of adjacent elements, but rather the conditions of the elements to the specified position and the order of the exchange.
Public classZuoye18_zhijiepaixu { Public Static voidMain (string[] args) {int[] Array =New int[]{2,4,5,123,55,667,32,356}; intindex; for(inti=1;i<array.length;i++) {Index=0; for(intj=1;j<=array.length-i;j++){ if(array[j]>Array[index]) {Index=J; } } inttemp=array[array.length-i]; Array[array.length-i]=Array[index]; Array[index]=temp; } for(intK:array) {System.out.println (k);} }}
5. Invert the array, invert the array, replace the last element of the array with the first element, replace the second to the second, and so on, until all the array elements are reversed.
Public classZuoye19_fanzhuanshuzu { Public Static voidMain (string[] args) {int[] Array =New int[]{123,45,66,33,68,24,89}; inttemp; intLen =Array.Length; for(inti=0;i<len/2;i++) {Temp=Array[i]; Array[i]=array[len-1-i]; Array[len-1-i]=temp; } for(intJ:array) {System.out.print (j+ "");} }}
A few small topics about arrays-bubble sort, binary lookup, direct select sort, inverse array