Bubble sort of sorting algorithm
First, the initial bubble sort
Import Java.util.Arrays;
/**
*
* @title bubblesort
* @describe bubble sort
* @author Zhang Fuchang
* @date October 1, 2016 PM 3:56:30
*/
Pub Lic class Bubblesortlow {
//bubble sorting is the basis for fast sorting, but the sorting speed is slow.
//Basic idea: Compare the 1th element in the sequence with the 2nd element, if the former is greater than the latter, then two elements exchange position, otherwise do not exchange;
//The 2nd element is compared with the 3rd element, if the former is greater than the latter, then two elements are exchanged, otherwise they are not exchanged;
And so on, until the n-1 element is compared to the nth element, if the former is greater than the latter, then two elements are exchanged, otherwise they are not exchanged;
//After such a trip, the maximum value of n elements is placed in the nth position;
//Thereafter, the first n-1 The same process, so that the maximum value of the n-1 element is placed in the n-1 position;
//Then repeat the above procedure for the first n-2 elements ... The sort ends until the action of the element exchange position does not appear during a trip sort.
public static void Main (string[] args) {
//declaration int Array
int[] array = new INT[10];
Initialize an array with loops and random numbers
for (int i = 0; i < Array.Length; i++) {
Array[i] = (int) math.round (Math.random () * 1 00);
}
System.out.println ("Original array is:");
System.out.println ("--------------------------------------");
System.out.println (arrays.tostring (array));
Array = bubblesort (array);
System.out.println ("--------------------------------------");
System.out.println ("Sorted array is:");
System.out.println (arrays.tostring (array));
}
/**
*
* Function: The basic idea of bubble sorting is to keep comparing the adjacent two numbers, allowing the larger elements to move backwards. The largest number is chosen after a round of comparisons, and after the 2nd round, the number of the second largest is chosen,
And so on
*
*
* Parameter: int[] Array
*
* return type: int[]
*/
private static int[] Bubblesort (int[] array) {
Use a temporary array instead of the original array
Int[] arr = array;
for (int i = 0; i < arr.length; i++) {
System.out.println ("No." + (i + 1) + "trip");
for (int j = 0; J < arr.length-1-I; j + +) {
if (Arr[j] > arr[j + 1]) {
int temp = Arr[j];
ARR[J] = arr[j + 1];
Arr[j + 1] = temp;
}
}
Print an array of each trip
System.out.println (arrays.tostring (arr));
}
return arr;
}
}
Second, the improved version of the bubble sort
Import Java.util.Arrays;
/**
*
* @title Bubblesort
* @describe Bubble sort
* @author Zhang Fuchang
* @date October 1, 2016 3:56:30
*/
public class Bubblesorthigh {
Bubble sorting is the basis for fast sorting, but the sorting speed is slow.
Basic idea: The 1th element in the sequence is compared with the 2nd element, if the former is greater than the latter, then two elements are exchanged, otherwise they are not exchanged;
Then the 2nd element is compared with the 3rd element, if the former is greater than the latter, then the two elements exchange position, otherwise it is not exchanged;
And so on, until the n-1 element is compared with the nth element, if the former is greater than the latter, then two elements are exchanged for positions, otherwise they are not exchanged;
After such a trip, the maximum value of n elements is placed in the nth position;
Thereafter, the same process is performed on the first N-1 element, so that the maximum value of the n-1 element is placed in the n-1 position;
Then repeat the above process for the first n-2 elements ... The sort ends until the action of the element exchange position does not appear during a trip sort.
public static void Main (string[] args) {
Declaring an integral type array
int[] array = new INT[10];
Initializing an array with loops and random numbers
for (int i = 0; i < Array.Length; i++) {
Array[i] = (int) math.round (math.random () * 100);
}
System.out.println ("Original array is:");
System.out.println (arrays.tostring (array));
System.out.println ("--------------------------------------");
Array = bubblesort (array);
System.out.println ("--------------------------------------");
System.out.println ("Sorted array is:");
System.out.println (arrays.tostring (array));
}
/**
*
* Function: The basic idea of bubble sorting is to keep comparing the adjacent two numbers, allowing the larger elements to move backwards. The largest number is chosen after a round of comparisons, and after the 2nd round, the number of the second largest is chosen,
And so on
*
*
* Parameter: int[] Array
*
* return type: int[]
*/
private static int[] Bubblesort (int[] array) {
Use a temporary array instead of the original array
Int[] arr = array;
for (int i = 0; i < arr.length; i++) {
System.out.println ("No." + (i + 1) + "trip");
Swapped flag with an initial value of false
Boolean swap = false;
for (int j = 0; J < arr.length-1-I; j + +) {
if (Arr[j] > arr[j + 1]) {
If swapped, the swap flag is set to True
Swap = true;
int temp = Arr[j];
ARR[J] = arr[j + 1];
Arr[j + 1] = temp;
}
}
System.out.println (arrays.tostring (arr));
If the interchange flag is false, that is, there is no interchange, then the nearest loop is popped
if (!swap) {
Break
}
}
return arr;
}
}
The bubble sort Java implementation of sorting algorithm