# Merge sort

## Merge sort principle

What is merge sort? To merge and sort, we must first understand what is merging: merging two ordered sequences into one ordered sequence, which we call "merging".

Idea: merge sort is to sort the sequence of numbers by using the idea of merge. According to the specific implementation, merge sorting includes "top-down" and "bottom-up". It is also a very typical application of divide and conquer.

## Algorithm implementation

1. Algorithm description

Step 1: divide n elements into two subsequences containing n/2 elements (there may be 1 more on the left than on the right)

Step 2: use MS to recursively sort the two subsequences until the number of elements in all parts is 1. (finally, the whole original sequence can be decomposed into n subsequences)

Step 3: gradually merge two ordered series from the bottom.

2. Illustration

3. Algorithm space complexity and time complexity

Time complexity:

- Worst: o( n log 2 n n\log _{2}n nlog2n)
- Best: o( n log 2 n n\log _{2}n nlog2n)
- Average: o( n log 2 n n\log _{2}n nlog2n)

Space complexity (secondary storage): o (n)

Stability: stable

## Examples

Use merge sort to output the following sequence from small to large: 123,45,6,22,99,1,38,41, - 6,0

java code:

import java.util.Arrays; public class Test { public static void mergeSort(int[] arr, int first, int last){ if(null == arr || arr.length < 0 || first == last){ return; } mergeSort(arr, first, (last + first)/2); mergeSort(arr, (last + first)/2 + 1, last); sort(arr, first, (last + first)/2, last); } //This method is to merge two ordered arrays, just like inserting the sorting algorithm directly public static void sort(int[] arr, int first, int mid, int last ){ int[] temp = new int[last - first + 1]; int i = 0; int j = 0; int index = 0; while(i < mid - first + 1 && j < last - mid){ if(arr[first + i] >= arr[mid + 1 + j]){ temp[index] = arr[mid + 1 + j]; j++; index++; } else { temp[index] = arr[first + i]; i++; index++; } } while(i < mid - first + 1){ temp[index] = arr[first + i]; index++; i++; } while(j < last - mid){ temp[index] = arr[mid + 1 + j]; index++; j++; } for(int k = first, n = 0; k <= last; k++, n++){ arr[k] = temp[n]; } } public static void main(String[] args) { int[] arr=new int[]{123,45,6,22,99,1,38,41,-6,0}; //Merge sort mergeSort(arr,0,9); System.out.println("The result of merging and sorting is:"); System.out.println(Arrays.toString(arr)); } }

# Bucket sorting

## Bucket sorting principle

The so-called box sorting is a sorting algorithm. Its working principle is to divide the array into a limited number of buckets. It is a fast and simple method. Bucket sorting can be regarded as an upgraded version of counting sorting.

It divides the data to be sorted into multiple ordered buckets, sorts the data in each bucket separately, and then takes out the data in each bucket in turn to complete the sorting. If a stable inner sorting is used and the relative order between elements is not changed when inserting elements into the bucket, bucket sorting is a stable sorting algorithm.

In order to make bucket sorting more efficient, we need to do these two things:

- Increase the number of barrels as much as possible with sufficient additional space
- The mapping function used can evenly distribute the input N data into K buckets

Fastest: when the input data can be evenly distributed to each bucket.

Slowest: when the input data is allocated to the same bucket.

## Algorithm implementation

1. Algorithm description

- Set a quantitative array as an empty bucket;
- Traverse the sequence and put the elements into the corresponding bucket one by one;
- Sort each bucket that is not empty;
- Put the elements back into the original sequence from a bucket that is not empty.

2. Illustration

3. Algorithm space complexity and time complexity

Time complexity:

- Worst: o( n 2 n^{2} n2)
- Best: o( n n n) Or o( n + k n+k n+k)
- Average: o( n n n) Or o( n + k n+k n+k)

Space complexity (secondary storage): o( n n n) Or o( n + k n+k n+k)

The average time complexity of bucket sorting is o( n + n 2 / k + k n+n^2/k+k n+n2/k+k) (divide the value range into n blocks + sort + re merge elements). When k is about equal to N, it is o( n n n)

Stability: stable

## Examples

Use bucket sorting to output the following sequence from small to large:

66,13,51,76,81,26,57,69,23

java code:

import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class Test { public static void bucketSort(int[] arr){ int max = Integer.MIN_VALUE; int min = Integer.MAX_VALUE; for(int i = 0; i < arr.length; i++){ max = Math.max(max, arr[i]); min = Math.min(min, arr[i]); } //Barrels int bucketNum = (max - min) / arr.length + 1; List<List<Integer>> bucketArr = new ArrayList<>(bucketNum); for(int i = 0; i < bucketNum; i++){ bucketArr.add(new ArrayList<Integer>()); } //Put each element in the bucket for(int i = 0; i < arr.length; i++){ int num = (arr[i] - min) / (arr.length); bucketArr.get(num).add(arr[i]); } //Sort each bucket for(int i = 0; i < bucketArr.size(); i++){ Collections.sort(bucketArr.get(i)); } //Combine the elements in each non empty bucket int k=0; for(int i=0;i<bucketNum;i++){ if(bucketArr.get(i).size()!=0){ for(int n:bucketArr.get(i)){ arr[k]=n; k++; } } } } public static void main(String[] args) { int[] arr=new int[]{123,45,6,22,99,1,38,41,-6,0}; //Bucket sorting bucketSort(arr); System.out.println("The result of bucket sorting is:"); System.out.println(Arrays.toString(arr)); } }