〇, Introduction to sorting algorithm
sort : The process of arranging a set of data in a specified order.
sorted classification :
- Internal Sort : Load all data that needs to be processed into memory for sorting.
- External sorting : Due to the large amount of data, it cannot be loaded into memory, so external memory is needed for sorting.
The sorting algorithm we study is mainly the internal sorting algorithm . Among them, the internal sorting can be divided into : bubble sort, simple selection sort (referred to as selection sort), direct insertion sort (referred to as insertion sort), Hill sort, quick sort, merge sort, sort, and heap sort. algorithm:
This article will introduce bubble sort, selection sort, insertion sort, and quick sort. The next article will introduce Hill sort, merge sort, radix sort, heap sort.
1. Bubble sort
The basic idea of : To treat the sorted sequence from front to back, compare the values of adjacent elements in turn, and if the reverse order is found, exchange, so that the element with a larger value gradually moves from the front to the back, just like the bubbles under the water gradually rise upward. .
For the array [3, 9, -1, 10, 20]
to be sorted, the diagrammatic process of the bubble sort algorithm is as follows:
From this, the code of the bubble sort algorithm can be written:
public static void bubbleSort(int[] arr) {
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]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
This code can be optimized :
public static void bubbleSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int temp;
boolean flag = false;
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
flag = true;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
if (!flag) { //优化
break;
}
}
}
Second, the selection sort
The basic idea of is : From the data to be sorted, select an element according to the specified rules, and then exchange positions according to the rules to achieve sorting.
For the to-be-sorted array [3, 9, -1, 10, 20]
, the graphical process of the selection sort algorithm is as follows:
From this, the code of the selection sort algorithm can be written:
public static void selectSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[minIndex] > arr[j]) {
minIndex = j;
}
}
if (minIndex != i) {
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
}
3. Insertion sort
Basic idea : For the element to be sorted, find the appropriate position of the element by inserting to achieve sorting.
For the array [3, 9, -1, 10, 20]
to be sorted, the diagrammatic process of the insertion sort algorithm is as follows:
From this, the code of the insertion sort algorithm can be written:
public static void insertSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int insert = arr[i];
int j;
for (j = i - 1; j >= 0; j--) {
if (insert < arr[j]) {
arr[j + 1] = arr[j];
} else {
break;
}
}
arr[j + 1] = insert;
}
}
This code can be optimized :
public static void insertSort(int[] arr) {
for (int i = 1; i < arr.length; i++) {
int insertVal = arr[i];
int insertIndex = i - 1;
while (insertIndex >= 0 && insertVal < arr[insertIndex]) {
arr[insertIndex + 1] = arr[insertIndex];
insertIndex--;
}
if (insertIndex + 1 == i) { //优化
arr[insertIndex + 1] = insertVal;
}
}
}
4. Quick Sort
The basic idea of is : First, set a demarcation value ( element ), and then sort through one pass ( division function ), divide the array to be sorted into two parts, and all elements of one part are divided into two parts. To be small, and then use this method to quickly sort the two parts of the data respectively. The entire sorting process can be performed recursively, so that the entire data becomes an ordered sequence.
selection : The pivot can select the first element, the tail element, the middle element, random selection, three-point median method, absolute median method, etc. The choice of the first element as the pivot is often not fully considered. But for the sake of convenience, this article selects the first element of , , as the pivot.
of division function : The division function can choose one-way scan partition method, two-way scan partition method, etc. This article selects bidirectional scanning partition method : After selecting the pivot, determine the left and right "pointers" to scan the array to be sorted; ensure that the left pointer is on the left of the right pointer, and when the left pointer element is not greater than the pivot, it continues to advance to the right. When the element of the right pointer is not smaller than the pivot element, it continues to advance to the left; until the left and right pointers intersect, that is, the position of the pivot element.
From this, the code of the quicksort algorithm can be written:
public static void quickSort(int[] arr, int left, int right) {
int l = left;
int r = right;
int pivot = arr[left];
while (l <= r) {
while (l <= r && arr[l] <= pivot) l++; //左指针元素不大于主元时不断向右推进
while (l <= r && arr[r] >= pivot) r--; //右指针元素不小于主元时不断向左推进
if (l <= r) {
int temp = arr[l];
arr[l] = arr[r];
arr[r] = temp;
}
}
arr[left] = arr[r]; //arr[r]即为主元的位置
arr[r] = pivot;
if (left < r) quickSort(arr, left, r);
if (right > l) quickSort(arr, l, right);
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。