# Summary of Common Algorithms - Sort Paper

This paper summarizes the common high frequency algorithms for sorting.

#### 1. Bubble sort

The idea of bubble sorting is to compare elements and move larger or smaller elements to one end

``` public static void bubble(int[] array) {

for (int i = 0; i < array.length - 1; i++) {

for (int j = 0; j + 1 < array.length - i; j++) {

if (array[j] > array[j + 1]) {

int tmp = array[j];
array[j] = array[j + 1];
array[j + 1] = tmp;

}

}

}

}

```

#### 2. Quick Sort

The idea of fast sorting is to randomly select a number and use it as a critical point, divide the array into subarrays smaller than the critical point and subarrays larger than the critical point, and recursively process to achieve the final sorting.

```public static void quick(int[] array, int begin, int end) {

if (begin >= end) {
return;
}

int beginRange = begin;
int endRange = end;

int compareInt = array[begin];

begin++;

while (begin < end) {

if (array[end] > compareInt) {
end--;
continue;
}

if (array[begin] < compareInt) {
begin++;
continue;
}

int tmp = array[begin];
array[begin] = array[end];
array[end] = tmp;

}

if (array[beginRange] > array[begin]) {

int tmp = array[begin];
array[begin] = array[beginRange];
array[beginRange] = tmp;

}

quick(array, beginRange, begin - 1);
quick(array, end + 1, endRange);

return;

}
```

#### 3. Select Sort

The idea of choosing sort is to select a maximum value for each round and place it at the end of the array.Sorting is ultimately possible.

``` public static void selectSort(int[] array) {

if (array.length == 0) {
return;
}

for (int i = 0; i < array.length; i++) {

int min = array[i];
int minIndex = i;

for (int j = i; j < array.length; j++) {
if (array[j] < min) {
min = array[j];
minIndex = j;
}
}

int tmp = array[i];
array[i] = min;
array[minIndex] = tmp;
}
}
```

#### 4. Merge Sort

The idea of merge sort is to sort small arrays first, then combine small arrays into large arrays, until the size of the last merged array is the size of the original array, the merge sort is completed.

```public static void sort(int[] array, int left, int right) {

//1. Set recursive base case
if (left == right) {
return;
}
//2. Row on both sides
int mid = left + (right - left) / 2;
sort(array, left, mid);
sort(array, mid + 1, right);
//3. Consolidation
merge(array, left, mid + 1, right);

}

public static void merge(int[] array, int leftPtr, int rightPtr, int rightBound) {

int mid = rightPtr - 1;
int[] temp = new int[rightBound - leftPtr + 1];

int i = leftPtr, j = rightPtr, k = 0;
while (i <= mid && j <= rightBound) {
temp[k++] = array[i] <= array[j] ? array[i++] : array[j++];
}

while (i <= mid) {
temp[k++] = array[i++];
}
while (j <= rightBound) {
temp[k++] = array[j++];
}

//Don't forget to copy the temp array into arr
for (int m = 0; m < temp.length; m++) {
array[leftPtr + m] = temp[m];
}

}
```

#### 5. Bucket sorting

The idea of bucket sorting is to divide the elements into N buckets based on the difference between the maximum and minimum values in the array, drop them into them, sort them, and merge them.

``` public static void bucketSort(int[] array){

int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;

for(int i = 0; i < array.length; i++){
max = Math.max(max, array[i]);
min = Math.min(min, array[i]);
}

//Number of barrels
int bucketNum = (max - min) / array.length + 1;
ArrayList<ArrayList<Integer>> bucketArr = new ArrayList<>(bucketNum);
for(int i = 0; i < bucketNum; i++){
}

//Put each element in the bucket
for(int i = 0; i < array.length; i++){
int num = (array[i] - min) / (array.length);
}

//Sort each bucket
for(int i = 0; i < bucketArr.size(); i++){
Collections.sort(bucketArr.get(i));
}

int k = 0;

for (int i = 0; i < bucketArr.size(); i++) {

for (Integer integer : bucketArr.get(i)) {
array[k++] = integer;
}

}

}
```

#### 6. Count sorting

The counts are sorted by a special bucket with a value interval of 1.

```public static int[] countSort(int[] array) {
if (array == null || array.length == 0) {
return null;
}

int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;

//Find the maximum and minimum values in the array
for (int i = 0; i < array.length; i++) {
max = Math.max(max, array[i]);
min = Math.min(min, array[i]);
}

int help[] = new int[max];

//Find out how many times each number appears
for (int i = 0; i < array.length; i++) {
int mapPos = array[i] - min;
help[mapPos]++;
}

int index = 0;
for (int i = 0; i < help.length; i++) {
while (help[i]-- > 0) {
array[index++] = i + min;
}
}

return array;
}

```

#### 7. Hill Sorting

The idea of Hill sorting is in steps.Let the elements take half of the array as a step, compare and replace, and then reduce the step in turn. When the step is 0, the sorting is completed.Its main advantage is to avoid the inefficiency of one swap step per bubble sort.

``` public static void shellSort(int[] arr) {
//step:Step
for (int step = arr.length / 2; step > 0; step /= 2) {
//Compare a step interval [step, arr.length]
for (int i = step; i < arr.length; i++) {
int value = arr[i];
int j;

//Compare specific elements in step intervals
for (j = i - step; j >= 0 && arr[j] > value; j -= step) {
//J is the value of the left interval and j+step is the corresponding value of the right and left intervals.
arr[j + step] = arr[j];
}
//Here step is a negative number, [j + step] is the initial exchange value on the left interval
arr[j + step] = value;
}
}
}

```
##### The author personally summarizes, and expects to point out any errors. 