1, the evaluation standard of ranking algorithm
Stability: If A is originally in front of B, while A=b, A is still in front of B;
Instability: If A is originally in front of B, and A=b, then a may appear behind B;
Time complexity: The time it takes for an algorithm to execute.
Spatial complexity: The amount of memory required to run a program.
2, algorithm description
Algorithm Description: It repeatedly visited the elements to be sorted, comparing the adjacent two elements in turn, if they were wrong in order to replace them, until no elements need to be exchanged, sorting is done.
3, implementation steps
- Compare adjacent elements, if the previous one is larger than the last one, put them in two swap positions.
- Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. When this is done, the final element will be the maximum number.
- Repeat the above steps for all elements, except for the last one.
- Repeat the above steps each time for fewer elements, until there are no pairs of numbers to compare.
1) General implementation
1 Private Static voidBubbleSort1 (int[] a) {2 LongStart = System.nanotime ();//The function is timed in nanoseconds, 1 milliseconds =1000 microseconds =1000000 nanoseconds3 intLen =a.length;4 for(inti = 0; i < Len; i++) {//requires n-1 cycles, each time the largest element is "floating" like a bubble to the end of the array5 for(intj = 0; J < len-1-I.; J + +) {//Compare the two adjacent elements in turn to make the larger one move backwards6 if(A[j] > A[j+1]) {//if the condition changes to A[j] >= a[j+1], the sort is unstable7Swap (a,j,j+1); 8 } 9 }Ten } One LongEnd =System.nanotime (); ASystem.out.println ((End-start)/1000.0 + "US"); - } - the Private Static voidSwapint[] A,intJinti) { - inttemp =A[j]; -A[J] =A[i]; -A[i] =temp; +}
2) Improved implementation
Analysis of the general implementation of the problem, it does not matter whether the incoming array is (partially) ordered, all need to carry out the n-1 cycle, each loop to compare adjacent elements in turn.
Improved: Set up a flag bit POS, which records the last position of the interchange in the order of each trip. Since the POS location after the record has been exchanged in place, so in the next sequencing as long as the scan to POS location. For example, in extreme cases, an ordered array is ordered, and only 1 scans are required.
Description: Improvements are only effective for ordered or partially ordered sequences, and if a completely unordered sequence is ordered, there is no effect.
1 Private Static voidBubbleSort2 (int[] a) {2 LongStart =System.nanotime (); 3 intLen =a.length;4 inti = len-1;//used to record the last interchange in each order, that is, the next scan is OK, and the back is in order.5 while(i > 0) {//I=0 says the elements that are back from 0 are ordered and then stopped scanning.6 intpos = 0; 7 for(intj = 0; J < I; J + +) { 8 if(A[j] > a[j + 1]) { 9pos =J; TenSwap (a,j,j+1); One } A } -i =POS; - } the LongEnd =System.nanotime (); -System.out.println ((End-start)/1000.0 + "US"); -}4, algorithm analysis
Complexity of Time:
Best case: T (n) = O (n) when the input data is already in the positive order
Worst case: T (n) = O (n^2) when the input data is reversed
Average condition: T (n) = O (n^2)
Complexity of space:
In each loop, the extra space required is an extra space for the value exchange, so the spatial complexity is a constant O (1)
Stability: Stable
Sort algorithm (a) bubbling method