Thought (Ascending):
1. Compare the size of the two adjacent data, guarantee the large value in the back, and finally, after a sort of sorting will select the maximum value of this number of groups, placed in the last one;
2. In the exclusion of the last maximum array, select one of the maximum values to be placed in the final one;
3. The sorting is done until the array length-1 maximum is excluded (selected);
For example: The array is {10, 34, 300, 56, 6, 56, 7, 87, 5} with an array length of 9;
Public classFind_em { Public Static voidMain (string[] args) {intarr[]={10,34,300,56,6,56,7,87,5}; System.out.println ("Raw Data:"); for(inti=0;i<arr.length;i++) {System.out.print (Arr[i]+ "\ T"); } System.out.println ("\ n Sort (ascending):"); //Bubble Sort for(intj=1;j<arr.length;j++{ //control the number of cycles, that is, select a few maximum values and then stop for(inti=0;i<arr.length-j;i++) { //Exchange data, select Maximum Value if(Arr[i]>Arr[i+1]) { //If it is descending, just change it to < intT=arr[i+1]; Arr[i+1]=Arr[i]; Arr[i]=T; } } for(inti=0;i<arr.length;i++) {System.out.print (Arr[i]+ "\ T"); } System.out.println ("Select the maximum value:" +arr[arr.length-J]); } System.out.println ("Sorted Data:"); for(inti=0;i<arr.length;i++) {System.out.print (Arr[i]+ "\ T"); } }}
Results:
Raw DATA:10 34 300 56 6 56 7 87 5sort (Ascending):10 34 56 6 56 7 87 5 -Select the maximum value: 30010 34 6 56 7 56 5 AboutSelect the maximum value: 8710 6 34 7 56 5 AboutSelect the maximum value: 566 10 7 34 5 AboutSelect the maximum value: 566 7 10 5 theSelect the maximum value: 346 7 5TenSelect the maximum value: 106 57-TenSelect the maximum value: 756 7Select the maximum value: 6sorted Data:5 6 7 10 34 56 56 87 300
Comparison number: 36
Number of exchanges: 20
Algorithm for bubbling Sorting