1. Bubble sort
Bubble sorting is a basic sorting algorithm. The main idea is: if you want to sort from small to large, compare the adjacent elements, and when the left element is larger than the right, exchange positions.
For example, an array: 9-18-6-5-12. Sort these 5 elements from small to large, the idea is as follows
(1) Compare No. 1 and No. 2, no exchange; compare No. 2 and No. 3, exchange 9-6-18-5-12; compare No. 3 and No. 4, exchange 9-6-5 -18-12; compare 4th and 5th place, exchange 9-6-5-12-18. At this time, the largest 18 is already in the last place
(2) Compare the first and second places, exchange 6-9-5-12-18; compare the second and third places, exchange 6-5-9-12-18; compare the third and fourth places Bits, without swapping. At this time, the second largest 12 is in the second to last
(3) Compare the 1st and 2nd places, exchange 5-6-9-12-18; compare the 2nd and 3rd places, do not exchange; the third largest 9 is now back
(4) Compare the 1st and 2nd place without exchange. Sorted
Summary: If you sort n numbers, you need n-1 trips to sort the end, because each trip can put a number back, each trip is the first bit to start backward comparison, but does not need to be compared to the end One.
package com.note;
import java.util.Scanner;
public class 冒泡排序 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();//表示有n个数
int[] arr = new int[n + 1];
//将n个数存储到arr数组中
for (int i=1;i<=n;i++){
arr[i] = sc.nextInt();
}
//冒泡排序
for (int i=1;i<=n-1;i++){//需要进行n-1趟排序
for (int j=1;j<=n-i;j++){//比较到n-i即可,因为每次都会排好一位
if (arr[j] > arr[j+1]){
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
//输出排序后的数组
for (int i=1; i<=n; i++)
System.out.print(arr[i]+" ");
}
}
The code is as above, and the notes are very clear, then enter
5
9 18 6 5 12
After the result is as follows
5 6 9 12 18
Second, quick sort
The quick sort algorithm is a frequently used algorithm, but its understanding is much more difficult than the bubble sort, because it involves recursive and divide-and-conquer ideas. These two ideas are very difficult to understand, even if I am a student from the class I think It is not easy, so I will not explain these two ideas here, but directly explain how quick sorting works.
For example, an array: 9-18-6-5-12-8-3-7. Sort these 8 elements from small to large, the idea is as follows
(1) First set a reference number, whichever is the first number, for example 9. So 9 is a reference number
(2) Set left = 1 to point to the first number 9, and right = 8 to point to the last number 7.
(3) Let right first move to the left until it finds a number less than the reference number. In fact, when right = 8, the element is 7 is less than 9, and left moves to the right until it finds a number greater than 9, when left = 2, Elements with 18 are greater than 9. Exchange arr [left] and arr [right]. So 9-7-6-5-12-8-3-18.
(4) Right continues to the left, the element is 3 less than 9 when right = 7; left continues to the right, the element is 12 greater than 9 when left = 5; exchanged for 9-7-6-5-3-8-12-18 .
(5) Right continues to the left, the element is 8 less than 9 when right = 6; left continues to the right, when left = 6, at this time when left and right meet, it is necessary to return the standard number, that is, exchange 9 and 8 Get 8-7-6-5-3-9-12-18. At this time, you will find that the left side of 9 is less than 9, and the right side of 9 is greater than 9.
(6) Quickly sort the numbers on the left of 9 in the same way, and quickly sort on the right.
package com.note;
import java.util.Scanner;
public class 快速排序 {
private static int[] arr;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();//表示有n个数
arr = new int[n + 1];
//将n个数存储到arr数组中
for (int i=1;i<=n;i++){
arr[i] = sc.nextInt();
}
//调用快速排序函数
f(1,n);
//输出排序后的数组
for (int i=1; i<=n; i++)
System.out.print(arr[i]+" ");
}
private static void f(int left, int right) {
if (left > right)
return;
//设置一个基准数
int temp = arr[left];
//设置左右2大使者
int i = left;
int j = right;
while (i != j){//当i和j没有相遇
//j向左寻找一个小于temp的数
while (arr[j]>=temp && i<j)
j--;
//i向右寻找一个大于temp的数
while (arr[i]<=temp && i<j)
i++;
//找到之后交换
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
//i和j相遇,就将基准数归位
arr[left] = arr[i];
arr[i] = temp;
f(left, i-1);//继续递归处理左边的数组
f(i+1, right);//继续递归处理右边的数组
}
}
Input
8
9 18 6 5 12 8 3 7
After output
3 5 6 7 8 9 12 18