First, the basic idea:
Bubble sort is a simple sort of exchange class. The basic idea is to scan the elements to be sorted from the beginning, compare the adjacent elements in sequence during the scanning process, and move the elements with large key values back. After each pass, the element with the highest key value is moved to the end, where the element's position is noted, and the next order only needs to be compared to this location until all the elements are sorted in an orderly manner.
Generally, the n elements are bubbled up, and a total of n-1 is required. The 1th trip needs to compare n-1 times, 2nd trip needs to compare n-2 times, ... The first trip needs to be compared n-i times.
Second, the algorithm
2.1 Raw Bubble Sorting algorithm
public static int[] BubbleSort1 (int [] arr) {int temp;//temp variable int count=arr.length;//array length//control loop count (actually count-1 times, Except for the last time the inner loop has ended, it does not affect) for (int i=0;i<count;i++) {//I-count-i times to compare for (int j=0;j<count-i-1;j++) {if (arr[j ]>arr[j+1]) {//Exchange data temp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;}}} return arr;}
2.2 First improvement (based on the original algorithm)
Use the Flag field to control the number of loops, when a trip is compared, no data exchange occurs when the data is ordered, there is no need for the next trip to compare
public static int[] BubbleSort2 (int [] arr) {int temp;//temp variable int count=arr.length;//array length Boolean flag;// Control the number of cycles (actually count-1 times, except where the last internal loop has ended and does not affect) for (int i=0;i<count;i++) {flag=false;//I-trip ordering requires count-i comparison for (int j= 0;j<count-i-1;j++) {if (arr[j]>arr[j+1]) {//Exchange data temp=arr[j];arr[j]=arr[j+1];arr[j+1]=temp;flag=true;}} If the last scan does not have an interchange, the array is already in order, exiting the Loop if (!flag) {break;}} return arr;}
2.3 Second improvement (on the basis of the original algorithm)
The first is to use flag to control the number of cycles to less cycle comparison, this method is to use a variable Lastpos save each trip to the last data exchange location, the next cycle only need to compare Arr[0...lastpos], and ARR[LASTPOS...N] Already in order.
public static int[] BUBBLESORT3 (int [] arr) {int temp;//temp variable int lastpos=arr.length-1;//Saves the last interchange of each pass, controlling the number of times per trip of the comparison int flagpos =0;//temporary variable//control loop number (actually arr.length-1 times) for (int i=0;i<arr.length;i++) {//I-trip sort requires arr.length-i-times comparison, Lastpos the location for the last interchange for (int j=0;j<lastpos;j++) {if (arr[j]>arr[j+1]) {//Exchange data temp=arr[j];arr[j]=arr[j+1];arr[j+1]= Temp;flagpos=j;}} lastpos=flagpos;//as the last subscript for the next comparison}return arr;
2.4 Third Improvement (merge of First and second, performance is better than one or two, take one and two advantages combined)
Flagcontrol the number of cycles,Lastpos control the last subscript of each cycle
public static int[] BUBBLESORT4 (int [] arr) {int temp;//temp variable boolean flag;//control loop number int lastpos=arr.length-1;//save subscript for last interchange per trip , control the number of times per trip comparison int flagpos=0;//temp variable//control loop number (actually arr.length-1 times) for (int i=0;i<arr.length;i++) {flag=false;// I need to make a arr.length-i comparison when ordering, Lastpos for the last interchange location for (int j=0;j<lastpos;j++) {if (arr[j]>arr[j+1]) {//Exchange data Temp=arr[j] ; arr[j]=arr[j+1];arr[j+1]=temp;flagpos=j;flag=true;}} lastpos=flagpos;//If the last scan does not have an interchange, the array is already in order, exiting the Loop if (!flag) {break;}} return arr;}
An improved bubble sorting algorithm--java