# Eight Sorting Algorithms

Keywords: Algorithm

## 1. Bubble sorting: (Average time complexity: O(n^2))

### 4. The code is as follows:

```​
public static void bubbleSort(int[] arr){
int temp = 0;
boolean flag = false;//Optimize
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]){
flag = true;
temp = arr[j + 1];
arr[j + 1] = arr[j];
arr[j] = temp;
}
}
if(!flag){
break;
}else{
flag = false;
}
}
}

​```

## 2. Quick sorting: (an improvement on bubble sorting) (Average time complexity: O(nlogn))

### 3. The code is as follows:

```public static void quickSort(int[] arr,int start,int end){
if(start >= end){//Recursive End Flag
return;
}
//Use the number 0 in the array as the standard number
int stard = arr[start];
int low = start;
int high = end;
while (low < high) {
while (low < high && stard <= arr[high]){
high --;
}//Find a number smaller than stard on the right
arr[low] = arr[high];//Number marked low for override after finding

while (low < high && stard >= arr[low]){
low ++;
}//Find a number larger than stard on the left
arr[high] = arr[low];//Number marked high for overwriting after finding
}
//Assign a standard number to a number marked low or high (low=high in this case)
arr[low] = stard;
//Recursively process numbers on both sides
quickSort(arr,start,low);
quickSort(arr,low+1,end);
}```

## 3. Merge Sort: (Average Time Complexity: O(nlogn))

### 3. The code is as follows:

```//Division+Combination Method
public static void mergeSort(int[] arr,int left,int right,int[] temp){
if(left < right){
int mid = (left + right) / 2;
//Decomposition recursively to the left
mergeSort(arr,left,mid,temp);
//Decomposition Recursively Right
mergeSort(arr,mid + 1,right,temp);
//To merge
merge(arr,left,mid,right,temp);
}
}
//Method of merging
/**
*
* @param arr  Sorted original array
* @param left  Initial index of left ordered sequence
* @param mid  Intermediate Index
* @param right Right Index
* @param temp Array to be transferred
*/
public static void merge(int[] arr,int left,int mid,int right,int[] temp) {
int i = left;//Initial index of left ordered sequence
int j =mid + 1;//Initial index of right ordered sequence
int t = 0;//Current index to temp array

//1. Fill the temp array with two ordered sequences according to the rules until one party has finished processing
while (i <= mid && j <= right) {
if(arr[i] <= arr[j]){
temp[t] = arr[i];
t++;
i++;
}else{
temp[t] = arr[j];
t++;
j++;
}
}
//2. Fill the temp array directly with an ordered sequence of remaining data
while (i <= mid){
temp[t] = arr[i];
t++;
i++;
}
while (j <= right){
temp[t] = arr[j];
t++;
j++;
}
//3. Copy elements of temp array to arr (not all)
t = 0;
int tempLeft = left;
while (tempLeft <= right) {
arr[tempLeft] = temp[t];
t++;
tempLeft++;
}

}```

## 4. Sorting cardinality: (Average time complexity: O(n^k))

### 4. The code is as follows:

```public static void radixSort(int[] arr){

//Preparing to start the cycle
//1. Get the maximum number of digits in the array
int max = arr[0];
for(int i = 1; i < arr.length; i++){
if(arr[i] > max){
max = arr[i];
}
}
//2. How many digits is the maximum number obtained
int maxLength = (max + "").length();

//Defines a two-dimensional array of 10 buckets, each of which is a one-dimensional array
//1. A two-dimensional array contains 10 one-dimensional arrays
//2. To prevent data overflow when placing data, the length of one-dimensional array is arr.length
//3. It is obvious that cardinality ordering is a classical algorithm for space-time conversion.
int[][] bucket = new int[10][arr.length];

//To record how much data is actually stored in each bucket, we define a one-dimensional array to record the number of data each bucket puts in.
int[] bucketElementCounts = new int[10];
//bucketElementCounts[0], which records the number of buckets [0] buckets put in

for(int i = 0, n = 1; i < maxLength; i++,n *= 10){
//Sort the number of digits corresponding to each element for each round (first one, second ten.)
for(int j = 0; j < arr.length; j++){
//Remove the value of each element's corresponding position and place it in the corresponding bucket
int digtiofElement = arr[j] / n % 10;
bucket[digtiofElement][bucketElementCounts[digtiofElement]] = arr[j];
bucketElementCounts[digtiofElement]++;
//For example, 53 means that when placed in bucket[3][bucketElementCounts[3], bucketElementCounts[3] starts at 0 and goes on for every number +
}
//In the order of this bucket, (the subscript of a one-dimensional array takes out the data and puts it in the original array)
int index = 0;
//Traverse through each bucket, and if there is data in the bucket, place it in the original array
for(int k = 0; k < bucketElementCounts.length; k++){
if(bucketElementCounts[k] != 0){
//Loop the kth bucket (that is, the kth one-dimensional array) into the
for(int l = 0; l<bucketElementCounts[k] ; l++){
arr[index] = bucket[k][l];
index++;
}
}
//For each round of processing, you need to set the array for each bucket count to 0
bucketElementCounts[k] = 0;
}
}
}```

### (2) Cardinal sorting is a classic way of changing time and takes up a lot of memory. It is easy to cause OutOfMemoryError when sorting data on sea.

Posted by ukspudnie on Sat, 16 Oct 2021 09:49:41 -0700