The basic idea of Bubble Sort sorting is to think of the array to be sorted as a descending order, the record with smaller keywords as lighter, and the record with larger keywords as heavier, the value of a smaller keyword is regarded as a bubble in the water floating up, a larger keyword such as a rock sinking, when the bubbles are floating to the corresponding position, the sorting ends.
Algorithm performance analysis
(1) time complexity. The total number of times is 3/2 (n-1) * n.
(2) spatial complexity. The space complexity of only one auxiliary unit is O (1)
(3) algorithm stability. Bubble Sorting is a stable sorting algorithm.
/*
* Kiss_My_Love
* 2012/8/20
* Bubble Sorting
**/
Public static Object [] bubbleSort (Object [] sort ){
For (int I = 1; I <sort. length; I ++ ){
For (int j = 0; j <sort. length-I; j ++ ){
If (Integer) sort [j]> (Integer) sort [j + 1]) {
Object temp = sort [j];
Sort [j] = sort [j + 1];
Sort [j + 1] = temp;
}
}
}
Return sort;
}