The most basic common sorting algorithms are:
Bubble Sort
Select Sort
Insert Sort
The time complexity of these three sorting algorithms is O(n²), which is relatively slow; the space complexity is O(1).
-------------------------------------------------- Java code ------------------------------------------------ ------------
public class Code_01_Sort { public static void main(String[] args) { //initialize the array int len = (int)(Math.random()*20);//Randomly generate an array length within 20 int [] arr = new int [len]; for(int i = 0; i < len; i++)//Randomly generate values within 1000 in the array { arr[i] = (int)(Math.random()*1000) - (int)(Math.random()*1000); } //bubbleSort(arr);//Call bubble sort //selectSort(arr);//Call selection sort //insertSort(arr);//Call insertion sort mergeSort(arr);//Call merge sort for(int i = 0; i < len; i++)//format the output array System.out.printf("%02d:%-+4d\n",i,arr[i]); } public static void bubbleSort(int[] arr) {//Bubble sort: each traversal will put the maximum value of the undetermined sorting interval to the back //Specific method: Each time the two adjacent elements are compared, if the latter element is smaller than the former element, the position is exchanged. //For each traversal, determine the number of elements in the sorting interval (in the latter part of the array) + 1 System.out.println("Bubble Sort: "); for(int end = arr.length-1; end > 0; end--) {//Each traversal will put the maximum value at the position of arr[end] for(int i = 0; i < end; i++)//Start from arr[0] and traverse forward to arr[end], compare arr[i] with the next adjacent element { if(arr[i] > arr[i+1])//If it is larger than the next element, swap the position { int tmp = arr[i]; arr[i] = arr[i+1]; arr[i+1] = tmp; } } } } public static void selectSort(int[] arr) {//Selection sort: each traversal will put the minimum value of the undetermined sorting interval to the front //Specific method: each element is compared with the first element of the undetermined sorting interval, and if it is smaller than the first element, the position is exchanged. //Each time it is traversed, determine the number of elements in the sorting interval (in the front part of the array) + 1 System.out.println("Select Sort: "); for(int i = 0; i < arr.length-1; i++) {//Each traversal will place the minimum value at the position of arr[i] for(int j=i+1; j < arr.length; j++)//Start from arr[i+1] and traverse backwards, and compare with arr[i] in turn { if(arr[i] > arr[j])//If there is an element smaller than arr[i], swap positions with arr[i]. { int tmp = arr[i]; arr[i] = arr[j]; arr[j] = tmp; } } } } public static void insertSort(int[] arr) {//Insertion sort: similar to adjusting the order of poker when playing poker //Specific method: Each element is compared with all the elements in front of it in turn, and if it is smaller than an element in front, the position is exchanged. //Each time it is traversed, determine the number of elements in the sorting interval (in the front part of the array) + 1 System.out.println("Insertion Sort: "); for(int i = 1; i < arr.length; i++) {//Each traversal will find a position suitable for the arr[i] element in the sorted interval for(int j = i; j >0; j--)//Start from the arr[i] element and traverse forward to arr[i-1] { if(arr[j] < arr[j-1])//If the arr[i] element is found to be smaller than the adjacent arr[i-1] element in front of it, then swap the position with the previous element { int tmp = arr[j]; arr[j] = arr[j-1]; arr[j-1] = tmp; } } } } }