当前位置:网站首页>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));
}边栏推荐
- 2022/4/8考试总结
- Chrome realizes automated testing: recording and playback web page actions
- 【云原生】k8s 部署redis集群
- 只会Excel想做图表可视化,让数据动起来?可以,快来围观啦(附大量模板下载)
- Buuctf brushes eleven questions (05)
- 网络开发套接字以及UDP、TCP协议
- 摩托罗拉诉海能达案一审结果出炉:海能达被判赔53亿元
- android 11 安全策略及权限管理
- 格力口罩来了!KN95口罩只要5.5元一个!
- Analysis of cloud native application security organization structure
猜你喜欢

Cy3荧光标记抗体/蛋白试剂盒 (10~100mg标记量)

Build your own website (22)

带你掌握 Makefile 分析

PyQt5快速开发与实战 4.10 窗口绘图类控件

图论的小技巧以及扩展

An article to solve the bigkey problem in redis

Cy3 fluorescent labeling antibody / protein Kit (10~100mg labeling amount)

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

SparkSQL的UDF及分析案例,220726,,

Object creation process and object layout
随机推荐
Shandong football match
2022/5/17考试总结
联发科携手三星推出全球首款支持Wi-Fi 6的8K电视
Six employees have been confirmed! Samsung closed the turtle tail mobile phone factory for the third time!
Three consecutive high-frequency interview questions of redis online celebrity: cache penetration? Cache breakdown? Cache avalanche?
Markdown extended syntax
Complete Guide to IOT architecture
数据仓库项目从来不是技术项目
三星存储工厂又发生火灾!
Another fire broke out in Samsung storage factory!
干货|语义网、Web3.0、Web3、元宇宙这些概念还傻傻分不清楚?(中)
2022/3/10 考试总结
组件的传参
UDF and analysis cases of sparksql, 220726,,
摩托罗拉诉海能达案一审结果出炉:海能达被判赔53亿元
Bluetooth framework summary
Take byte offer in four rounds and answer the interview questions
Leetcode-461. Hamming distance
紫光FPGA解决口罩难题!助力口罩生产全面提速
多肽KC2S修饰白蛋白纳米粒/靶向肽GX1修饰人血清白蛋白纳米粒探针的研究制备