# Day_08 array sorting algorithm

Keywords: Algorithm data structure

## Array sorting algorithm

Algorithm = data structure + program code
1. Sorting:
Suppose that the sequence containing n records is {R1, R2,... Rn}, and the corresponding keyword sequence is {K1, K2,..., Kn}. Reorder these records to {Ri1,Ri2,... Rin}, so that the corresponding keyword values meet the requirements of Ki1 < = ki2 < =... < = kin. Such an operation is called sorting.
Generally speaking, the purpose of sorting is to find quickly, such as binary search.
(1) Time complexity: analyze the comparison times of keywords and the movement times of records
(2) Spatial complexity: analyze how much auxiliary memory is needed in the sorting algorithm
(3) Stability: if the keyword values of two records A and B are the same, but the order of A and B remains unchanged after sorting, the sorting algorithm is said to be stable.
3. Sorting algorithm classification:
Internal sorting: the whole sorting process does not need the help of external memory (such as disk), and all sorting operations are completed in memory
External sorting: there are a lot of data involved in sorting, and the amount of data is very large. The computer cannot complete the whole sorting process in memory, so it must use external memory (such as disk). The most common external sort is multiway merge sort. It can be considered that external sorting is composed of multiple internal sorting.
4. Five characteristics of the algorithm
Input: there are 0 or more input data. These inputs must be clearly described and defined
Output: there must be at least one or more output results. No output result is allowed
Finiteness: the algorithm will automatically end after a limited number of steps without dead loop, and each step can be completed in an acceptable time
Certainty (clarity): every step in the algorithm has a clear meaning, and there will be no ambiguity
Feasibility (effectiveness): each step of the algorithm is clear and feasible, allowing users to calculate the answer with paper and pen.
5. Top ten internal sorting algorithms
Select sort: directly select sort and heap sort
Swap sort: bubble sort and quick sort (these two will write code)
Insert sort: direct insert sort, half insert sort, Shell sort
Merge sort:
Bucket sorting:
Cardinality sorting:
5.1 quick sort algorithm
Quick scheduling idea:
1) First, take a number from the sequence as the reference number and record it as key.
2) Move the subscript and put all the numbers not less than x to its right and all the numbers not greater than x to its left (in this way, there is no more than key on the left and no less than key on the right of the key position, just sort the left and right intervals)
3) Repeat the second step for the left and right intervals until there is only one number in each interval

```public class QuickSortTest {
public static void main(String[] args) {
int[] arr=new int[] {12,5,8,9,10};
System.out.println("The result before sorting is:");
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]+"\t");
}
System.out.println();
quickSort(arr,0,arr.length-1);
System.out.println("The sorted result is:");
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]+"\t");
}
}
public static void quickSort(int[] arr,int left,int right) {		//Quick sort
if(arr==null || left>right) {
return ;
}
if(left > right) {
return ;
}
int l=left;
int r=right;
int key=arr[left];
while(l != r) {
while(arr[r]>=key && l<r) {		//Shift left
r--;
}
while(arr[l]<=key && l<r) {		//Shift right
l++;
}
if(l<=r) {			//Swap the positions of l and r
int temp=arr[l];
arr[l]=arr[r];
arr[r]=temp;
}
}
arr[left]=arr[l];
arr[l]=key;
quickSort(arr,l+1,right);	//Recursively sort the subsequence on the right
quickSort(arr,left,l-1);	//Recursively sort the subsequence on the left
}

}

```

5.2 Select Sorting Algorithm
Select Sorting idea:
1) Find the minimum value in n unordered numbers (a[0]~a[n-1]) and exchange it with a[0];
2) Find the minimum value in the remaining unsorted n-1 numbers (a[1]~a[n-1]) and exchange it with a[1];
...
In the following order, in step n-1, find the minimum value in the two unordered numbers (a[n-2]~a[n-1]) and exchange it with a[n-2].

```public class SortTest02 {
public static void main(String[] args) {

int[] arr = new int[] {12,5,6,87,8,26};
System.out.println("The array before sorting is:");
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]+"\t");
}
for(int i=0;i<arr.length-1;i++) {
int index=i;
for(int j=i+1;j<arr.length;j++)
if(arr[j] < arr[index]) index=j;
int temp=arr[index];
arr[index]=arr[i];
arr[i]=temp;
}
System.out.println();
System.out.println("The sorted array is:");
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]+"\t");
}
}
}

```

5.3 bubble sorting
Bubble sorting idea:
Start from the first element and compare two adjacent elements. If the first element is larger or smaller than the first element, exchange their positions. In this way, the comparison is completed once, and then discard the largest or smallest element and continue the comparison until the sorting is completed.

```package com.jd.wds;

public class SorTest01 {
public static void main(String[] args) {
int[] arr = new int[] {15,6,8,16,28,9,13};
System.out.println("The result before sorting is:");
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]+"\t");
}
for(int i=0;i<arr.length-1;i++) {		//
for(int j=0;j<arr.length-i-1;j++) {
if(arr[j]>arr[j+1])
{
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
System.out.println();
System.out.println("The result of bubble sorting is:");
for(int i=0;i<arr.length;i++) {
System.out.print(arr[i]+"\t");
}
}
}

```

Sorting algorithm summary:

Posted by SilentQ-noob- on Tue, 02 Nov 2021 06:46:36 -0700