当前位置:网站首页>Bubbling, fast sorting, heap sorting and cardinality sorting of the eight sorts
Bubbling, fast sorting, heap sorting and cardinality sorting of the eight sorts
2022-07-27 22:58:00 【Drunk and refreshing】
One 、 Bubble sort
/**
* Bubble sort
* @param arr
*/
public static void bubbleSort(int arr[]){
for (int j=0;j<arr.length;j++){
for (int i = 0;i<arr.length-1;i++){
if (arr[i]>arr[i+1]){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1]= temp;
}
}
}
System.out.println(Arrays.toString(arr));
}Two 、 Quick sort
public static void quickSort(int[] arr,int left,int right){
if(left >= right){
return;
}
// Program
// Define the first number in the array as the base number
int baes = arr[left];
int i = left;
int j = right;
while (i !=j){
//j Find one smaller than the current benchmark
while (arr[j] >= baes && i<j){
j--;
}
while (arr[i] <= baes && i<j){
i++;
}
// Value exchange
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// The number exchange between the reference number and the encounter position
arr[left] = arr[i];
arr[i] = baes;
quickSort(arr,left,i-1);
quickSort(arr,i+1,right);
}3、 ... and 、 Heap sort
public class HeapSort {
public static void main(String[] args) {
int[] arr = new int[]{5,7,4,2,0,3,1,6};
for (int p = arr.length-1;p>=0;p--){
adjust(arr,p,arr.length);
}
// Top and bottom elements are interchanged
for(int i = arr.length-1;i>=0;i--){
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
adjust(arr,0,i);
}
System.out.println(Arrays.toString(arr));
}
/**
* Stack maintenance
* @param arr
* @param parent
*/
public static void adjust(int[] arr,int parent,int length) {
// Define the left subtree
int Child = 2 * parent + 1;
while (Child<length){// There are left child nodes
// Compare the left and right subtrees
int rChild = Child + 1;
if(rChild<length && arr[rChild]>arr[Child]){
Child++;
}
if(arr[parent] < arr[Child]){ // Parent and child nodes need to be exchanged ,, Because build a big top pile , The parent node must be larger than the child node
int temp = arr[parent];
arr[parent] = arr[Child];
arr[Child] = temp;
parent = Child;
Child = 2 * Child + 1;
}else {
break;
}
}
}
}Four 、 Radix sorting
import java.util.Arrays;
public class JiShuSort {
public static void main(String[] args) {
int[] arr = new int[]{587,956,12,47,30,20,15,11,21,31,57,91,35,120};
sort(arr);
System.out.println(Arrays.toString(arr));
}
public static void sort(int[] arr){
int[][] bucket = new int[10][arr.length];
int[] bucketCounts = new int[10];
// Find the maximum
int max = arr[0];
for (int f = 0;f<arr.length;f++){
if(arr[f]>max){
max = arr[f];
}
}
int maxLength = (max+"").length();
int n = 1;
for (int h = 0;h<maxLength;h++) {
// Sort by the value of a bit
for (int i = 0; i < arr.length; i++) {
int element = arr[i] / n % 10; // Calculate the value of single digits
int count = bucketCounts[element];
bucket[element][count] = arr[i];
bucketCounts[element] = bucketCounts[element] + 1;
}
// Data extraction
int index = 0;
for (int k = 0; k < bucketCounts.length; k++) {
if (bucketCounts[k] != 0) {
for (int l = 0; l < bucketCounts[k]; l++) {
arr[index] = bucket[k][l];
index++;
}
}
// Empty the bucket record
bucketCounts[k] = 0;
}
n = n * 10;
}
}
}
5、 ... and 、 Test the main method
public static void main(String[] args) {
int arr[] = {9, 5, 2, 3, 1, 8, 4, 20, 11};
// Bubble sort
// bubbleSort(arr);
// Quick sort
// quickSort(arr,0,arr.length-1);
// System.out.println(Arrays.toString(arr));
// Heap sort
/*
for (int p=arr.length-1;p>=0;p--){
adjust(arr,p,arr.length);
}
for (int i =arr.length-1;i>=0;i--){
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
adjust(arr,0,i);
}
System.out.println(Arrays.toString(arr));
*/
// Radix sorting
jishu(arr);
System.out.println(Arrays.toString(arr));
}边栏推荐
- [NOI2018]归程(Kruskal重构树/可持久化并查集)
- 我与消息队列的八年情缘
- Three consecutive high-frequency interview questions of redis online celebrity: cache penetration? Cache breakdown? Cache avalanche?
- JVM composition and memory model
- Leetcode-470. implement rand10() with rand7()
- 2022/3/22考试总结
- leetcode-461.汉明距离
- 8000字讲透OBSA原理与应用实践
- Do you want to be dismissed? Let's take a look at the "exit tips" of programmers
- Parameter transmission of components
猜你喜欢

Take byte offer in four rounds and answer the interview questions

If there is no reference ground at all, guess if you can control the impedance?

视频人体行为检测
DP traceability problem

An article to solve the bigkey problem in redis

Leetcode-461. Hamming distance

Markdown extended syntax

Three consecutive high-frequency interview questions of redis online celebrity: cache penetration? Cache breakdown? Cache avalanche?

20 character short domain name bypass replication

Possible causes of index failure
随机推荐
TFRecord的Shuffle、划分和读取
Data warehouse project is never a technical project
Fluorescence imaging of cle19 polypeptide in cells preparation of fluorescence quenching quantum dots of bovine serum albumin
2022/3/22 examination summary
Five network management trends in 2022
Another fire broke out in Samsung storage factory!
It is said that Huawei will cut the order again! Supply chain manufacturers are more difficult
Take you to master makefile analysis
Possible causes of index failure
2022/6/9 考试总结
2022/6/9 exam summary
已有6名员工确诊!三星第三度关闭龟尾手机工厂!
C语言详解系列——函数的认识(5)函数递归与迭代
On data management of data warehouse
51单片机内部外设:实时时钟(SPI)
对象创建过程及对象布局
Convnext:a convnet for the 2020s - model Brief
Parameter transmission of components
Take byte offer in four rounds and answer the interview questions
Oppo find x2 series release: 3k+120hz curved screen, DxO score first, top version 6999 yuan!