当前位置:网站首页>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));
}边栏推荐
- Jumpserver learning
- 51单片机内部外设:实时时钟(SPI)
- leetcode-470.用 Rand7() 实现 Rand10()
- Feed stream application reconfiguration - Architecture
- Parameter transmission of components
- US officials suggested trump prevent Infineon from acquiring cypress
- 浅析云原生应用安全组织架构
- [NOI2018] 冒泡排序(组合+卡特兰数+dp+树状数组)
- 带你掌握 Makefile 分析
- The follow-up is coming. Whether it's OK without reference, let's make it clear to everyone at once!
猜你喜欢

BUUCTF刷题十一道(05)

视频人体行为检测

cron 表达式

Analysis of cloud native application security organization structure
![The wave of smart home is coming, how to make machines understand the world [there is information at the end]](/img/8a/533e7f1fc96c03e6f8140efdd17983.png)
The wave of smart home is coming, how to make machines understand the world [there is information at the end]

云计算服务主要安全风险及应对措施

51单片机内部外设:实时时钟(SPI)

Jumpserver learning

On data management of data warehouse
DP traceability problem
随机推荐
MeterSphere金融公司落地经验分享
智能家居浪潮来袭,如何让机器看懂世界 【结尾有资料】
UDF and analysis cases of sparksql, 220726,,
Hc32f4a0 clock control
2022/5/31考试总结
一篇搞定Redis中的BigKey问题
Two dimensional code generation based on MCU and two dimensional code display on ink screen
The epidemic has spread to 28 states in the United States: more than 100000 employees such as apple and Microsoft work from home, and iphone11 is almost out of stock!
Unity 的基础光照
八大排序之冒泡、快排、堆排、基数排序
20 character short domain name bypass replication
Main security risks and Countermeasures of cloud computing services
雅思听力——剑雅5——Text1
2022/5/13 考试总结
Possible causes of index failure
Cache learning
可能导致索引失效的原因
8000字讲透OBSA原理与应用实践
数据仓库项目从来不是技术项目
2022 / 4 / 11 exam summary