First, bubble sort:
1, Algorithmic thinking:
For the data to be sorted, compare the two adjacent numbers from top to bottom and adjust them, moving the largest number downward and the smaller number upward. That is: Each trip compares adjacent two data elements in turn, placing the smaller numbers on the left, looping the same operation until all the data elements to be sorted are finished.
2. Example Analysis:
For example: We have to stand in a row of 10 people of varying height, asking them to line up in height from low to high, set 10 person numbered 0---9, the adjacent two persons in turn compare, if the left side is higher than the right person, then exchanges two person's position, otherwise does not exchange, until finally two person, namely completes a trip sort at this time. After a trip, the tallest person is already on the far right.
The result of a sort of order: (move to the last position after ranking the highest person)
So many times the sort, and finally finished the whole sort.
3, Algorithm analysis: (have small to large sort)
1>, each trip in the process, is to compare two consecutive numbers, if a[i]>a[i+1], then exchange two number, otherwise do not change;
2>, every trip is a heavy cycle, and because to go through the process, that is, there is a cycle outside, so there is a double cycle.
4, Algorithmic code: (Java version) [Java] View plain copy print?/** * bubble sort algorithm * @author xcbeyond * */ public class bubblesort { public static void main (String [] args) { // TODO auto-generated method stub int a[] ={13,15,37,89,60,39,12,109,56,72} ;