In the process of brushing questions, I forgot the commonly used sorting algorithms, so I wrote a note.
1, Bubble sorting
A simple sorting algorithm that repeatedly walks through the sequence to be sorted and compares the sizes of two adjacent elements. Put the larger elements at the end of the array in turn, or put the smaller elements at the head of the array in turn until the visit is completed.
1. Algorithm process
Compare adjacent elements and exchange them if the order is wrong
Do the same work for each pair of adjacent elements, from the first pair to the last pair
The element that has been placed in the last round does not need to be compared in the next round, because it is already the largest element in the current round
2. Code
public int[] bubbleSort(int[] nums){ for(int i = 0; i < nums.length - 1; i++){ for(int j = 0; j < nums.length - 1 - i; j++){ if(nums[j] > nums[j+1]){ int temp = nums[j]; nums[j] = nums[j+1]; nums[j+1] = temp; } } } return nums; }
2, Select sort
Simple and intuitive sorting algorithm. The algorithm process is to find the largest / smallest element in the unordered sequence and put it at the end / head of the team. Sequential cycle
1. Algorithm description
After n-1 times of sorting, the final result is obtained. Take finding the largest element as an example
Initial state: all out of order
Sorting: the elements behind the array are ordered and an increasing sequence. The first half of the elements are still in an unordered state
2. Code
public class SelectionSort { public int[] selectionSort(int[] nums){ for(int i = 0; i < nums.length; i++){ int minIndex = i; for(int j = i+1; j < nums.length; j++){ if(nums[j] < nums[minIndex]){ minIndex = j; } } int temp = nums[i]; nums[i] = nums[minIndex]; nums[minIndex] = temp; } return nums; } }
3, Insert sort
Build an ordered sequence step by step. For unordered data, scan the sorted sequence to find the corresponding insertion position
1. Algorithm description
Starting from the first element, it is sorted by default
Take out the next element and scan the sorted elements from back to front
If the element is larger than the new element, move the element to the next position
Find the position corresponding to the new element and set the position as the new element
2. Code
public static int[] insertSort(int[] nums){ for(int i = 1; i < nums.length; i++){ //The element at the current i location is a new element //Judge in order in the sorted sequence. If it is larger than the new element, move it back, otherwise insert it int pre = i - 1; int cur = nums[i]; //Here, you must save the value of num [i] with cur, because the value of num [pre + 1] may be modified while(pre >= 0 && nums[pre] > cur){ nums[pre + 1] = nums[pre]; pre--; } nums[pre+1] = cur; } return nums; }
4, Hill sort
Hill sort ratio general O(n ²) The sorting speed is much faster, which is suitable for small and medium-sized sorting.
1. Algorithm description
Group the sequences to be sorted according to a certain interval, and sort within the group first.
After each round, reduce the interval and continue sorting within the group
The initial interval size gap = num.length/2, which is recommended by hill
2. Code
The code part is worth thinking about.
public static void shellSort(int[] nums){ for(int gap = nums.length / 2; gap > 0; gap = gap / 2) { //A large cycle is a sequence within each group for (int i = gap; i < nums.length; i++) { int j = i; int temp = nums[j]; //Start sorting within groups while (j - gap >= 0 && temp < nums[j - gap]) { nums[j] = nums[j - gap]; j -= gap; } nums[j] = temp; } } }
5, Merge sort
The core idea of merging is divide and conquer. The array to be sorted is continuously divided into several small groups. When the size of the small groups is 1, it can be merged directly.
1. Algorithm description
The sequence with length n is divided into two subsequences with length n/2
Merge in subsequence
The two subsequences are merged into a complete ordered sequence
2. Code
public class MergeSort { public static void main(String[] args) { } public static void sort(int[] nums){ int[] temp = new int[nums.length]; sort(nums,0,nums.length - 1,temp); } public static void sort(int[] nums,int left,int right,int[] temp){ if(left < right){ int mid = (left + right) / 2; sort(nums,left,mid,temp); sort(nums,mid+1,right,temp); merge(nums,left,mid,right,temp); } } private static void merge(int[] nums, int left, int mid, int right, int[] temp) { int i = left; int j = mid + 1; int t = 0; if(i <= mid && j <= right){ if(nums[i] <= nums[j]){ temp[t++] = nums[i++]; }else{ temp[t++] = nums[j++]; } } while(i <= mid){ temp[t++] = nums[i++]; } while(j <= right){ temp[t++] = nums[j++]; } t = 0; //Copy the elements in temp to the original array while (left <= right){ nums[left++] = temp[t++]; } } }
()