first, the basic idea:
Bubble sort is a simple sort of commutative class. The basic idea is to scan the elements that are to be sorted from scratch, to compare the adjacent elements in sequence during the scan, and to move the elements of the key value back. After each trip, the element with the largest key value is moved to the end, and the position of the element is noted, and the next order is only required to compare to this location until all elements are ordered.
In general, the N-elements are bubble-sorted, requiring a total n-1 trip. The 1th trip needs to compare n-1 times, the 2nd trip needs to compare n-2 times, ... I need to compare n-i times for the first trip. two, 4 algorithm realization
Package Com.struct.array; /** * @ Description Array Sort * @ Project name Java_datastruct * @ Package Name Com.struct.array * @ class name ArraySort * @author Chenlin * @date June 22, 2010 Midday 8:28:46/public class ArraySort {/** * Adjacent two values * * @param arr/public static void B UbbleSort1 (long[] arr) {for (int i = 0; i < arr.length-1; i++) {//MAX do n-1 trip sort for (int j = 0; j < arr.length-1-I; J + +) {//To sort the current unordered interval arr[0......length-i-1] (the range of J is critical, this range is incrementally narrowed) if (Arr[j] > arr[j + 1]) {//Exchange small value to post
Face arr[j] = Arr[j] ^ arr[j + 1];
Arr[j + 1] = Arr[j] ^ arr[j + 1];
ARR[J] = Arr[j] ^ arr[j + 1];
}} list (arr); /** * Adjacent Two value comparison * * @param arr/public static void BubbleSort2 (long[) arr) {for (int i = 0; i < arr.length-1; i++)
{for (int j = 0; J < arr.length-1-I; j + +) { if (Arr[j] > arr[j + 1]) {arr[j] = Arr[j] + arr[j + 1];
Arr[j + 1] = Arr[j]-arr[j + 1];
ARR[J] = arr[j]-arr[j + 1];
}} list (arr); /** * Adjacent Two value comparison * * @param arr/public static void BubbleSort3 (long[) arr) {for (int i = 0; i < arr.length-1; i++)
{for (int j = 0; J < arr.length-1-I; j +) {if (Arr[j] > arr[j + 1]) {
Long temp = arr[j];
ARR[J] = arr[j + 1];
Arr[j + 1] = temp;
}} list (arr); /** * Adjacent Two value comparison * * @param arr/public static void BubbleSort4 (long[) arr) {for (int i = 0; i < arr.length-1; i++)
{for (int j = arr.length-1-i; j > 0; j--) {if (Arr[j] < arr[j-1]) { Long temp = arr[j];
ARR[J] = arr[j-1];
ARR[J-1] = temp;
}} list (arr); public static void list (long[] arr) {for (int i = 0; i < arr.length; i++) {System.out.prin
T (Arr[i] + "");
} System.out.println ("");
public static void Main (string[] args) {long arr[] = {22, 33, 44, 11, 23, 43, 54, 0, 98};
BUBBLESORT4 (arr);
}
}