Points:Algorithmic thinking (that's how you want to sort) and its core code (JAVA)
algorithm idea : Bubble sort as the name implies, the basic object is a bubble (representing an element or number to be sorted), according to the order from large to small or from small to large, by comparing the size of the adjacent bubbles to the water or the bottom of the bubble.
algorithm Abstract explanation : There is a C language based on the usual knowledge of the array, the default ordering from small to large and a total of N to sort the number, then the output of the list[0]~list[n] should be from small to large arrangement. In a 22 comparison, such as list[i]>list[i+1], the position of two digits is exchanged, otherwise it will not change. And then go on and on to get the entire sorted array.
Core code:
1 Public voidBubblesort (int[] list) {2 3 //number of times to traverse4 for(inti = 0; i < list.length-1; i++) {5 6 //Compare the size of two adjacent numbers from the back to the next, and then, after traversing once, place the small number I in the array in the first position.7 8 for(intj = list.length-1; J > i; j--) {9 Ten //compares adjacent elements if the preceding number is greater than the subsequent number, then the interchange One A inttemp = 0;//the number of temporary numbers to exchange - - if(List[j-1] >List[j]) { the -temp = List[j-1]; - -LIST[J-1] =List[j]; + -LIST[J] =temp; + A } at - } - - - -System.out.format ("%d trip: \ t", i); in - printall (list); to + } - the}
Algorithm understanding
sort Time : Best case I is already in order, need O (n), the worst is reverse o (n^2); Simply put, the closer the data is to the positive order, the better the algorithm.
stability : The bubble sort is to move the small element forward or move the large element backwards. The comparison is an adjacent two element comparison, and the interchange also occurs between these two elements.
So the order of the same elements is not changed, so bubble sort is a stable sorting algorithm .
Optimization
A common improvement to bubble sorting is to add the flag variable nochange, which is used to flag whether there is a data exchange in a sequencing process.
If there is no data exchange at the time of a trip, all data is ordered and the sort is immediately ended, avoiding unnecessary comparisons.
1 //An optimization algorithm for Bubblesort2 3 Public voidBubblesort_2 (int[] list) {4 5 inttemp = 0;//the number of temporary numbers to exchange6 7 BooleanNoChange =false;//Swap Flags8 9 Ten One //number of times to traverse A - for(inti = 0; i < list.length-1; i++) { - theNoChange =false; - - //Compare the size of two adjacent numbers from the back to the next, and then, after traversing once, place the small number I in the array in the first position. - + for(intj = list.length-1; J > i&& (false! = NoChange ); j--) { - + //compares adjacent elements if the preceding number is greater than the subsequent number, then the interchange A at if(List[j-1] >List[j]) { - -temp = List[j-1]; - -LIST[J-1] =List[j]; - inLIST[J] =temp; - toNoChange =true; + - } the * } $ Panax Notoginseng - the //if the flag is false, it means that this round traversal is not commutative, it is an ordered sequence, and can end the sort + A//if(false==NoChange) Break (where the IF statement and break are actually unnecessary, just add this judging condition to the for-judging loop statement, so I commented out) the +
- $ $ -System.out.format ("%d trip: \ t", i); - the printall (list); - Wuyi } the -}
Bubble sort of sorting algorithm