当前位置:网站首页>八大排序之冒泡、快排、堆排、基数排序
八大排序之冒泡、快排、堆排、基数排序
2022-07-27 20:11:00 【徒醉了清风l】
一、冒泡排序
/**
* 冒泡排序
* @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));
}二、快速排序
public static void quickSort(int[] arr,int left,int right){
if(left >= right){
return;
}
//程序
//定义数组当中的第一个数为基准数
int baes = arr[left];
int i = left;
int j = right;
while (i !=j){
//j去找比当前基准数小的
while (arr[j] >= baes && i<j){
j--;
}
while (arr[i] <= baes && i<j){
i++;
}
//数值交换
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
// 基准数和相遇位置的数交换
arr[left] = arr[i];
arr[i] = baes;
quickSort(arr,left,i-1);
quickSort(arr,i+1,right);
}三、堆排序
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);
}
//堆顶元素和堆底元素进行互换
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));
}
/**
* 堆得维护
* @param arr
* @param parent
*/
public static void adjust(int[] arr,int parent,int length) {
//定义出左子树
int Child = 2 * parent + 1;
while (Child<length){//有左子节点
//对比左右子树
int rChild = Child + 1;
if(rChild<length && arr[rChild]>arr[Child]){
Child++;
}
if(arr[parent] < arr[Child]){ //父子节点需要进行交换 ,,因为构建大顶堆,父节点一定要大于子节点
int temp = arr[parent];
arr[parent] = arr[Child];
arr[Child] = temp;
parent = Child;
Child = 2 * Child + 1;
}else {
break;
}
}
}
}四、基数排序
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];
//找到最大值
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++) {
//按照个位的值进行排序
for (int i = 0; i < arr.length; i++) {
int element = arr[i] / n % 10; //计算出个位数的值
int count = bucketCounts[element];
bucket[element][count] = arr[i];
bucketCounts[element] = bucketCounts[element] + 1;
}
//数据的取出
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++;
}
}
//清空桶记录
bucketCounts[k] = 0;
}
n = n * 10;
}
}
}
五、测试主方法
public static void main(String[] args) {
int arr[] = {9, 5, 2, 3, 1, 8, 4, 20, 11};
//冒泡排序
// bubbleSort(arr);
//快速排序
// quickSort(arr,0,arr.length-1);
// System.out.println(Arrays.toString(arr));
//堆排序
/*
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));
*/
//基数排序
jishu(arr);
System.out.println(Arrays.toString(arr));
}边栏推荐
- jvm组成及内存模型
- Purple light FPGA solves the mask problem! Boost the overall speed-up of mask production
- 技术生涯10年,那些让我心动的技术书
- SSM integration process
- 2022/4/8 exam summary
- SSM整合流程
- Bluetooth framework summary
- The wave of smart home is coming, how to make machines understand the world [there is information at the end]
- Chapter 8 using web sessions through rest
- PyQt5快速开发与实战 4.9 对话框类控件
猜你喜欢

Jeninkins offline deployment

leetcode-461.汉明距离
In depth understanding of redis master-slave principle

Redis learning

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

物联网架构完全指南

Direct insertion sort of seven sorts

Leetcode-461. Hamming distance

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

干货|语义网、Web3.0、Web3、元宇宙这些概念还傻傻分不清楚?(中)
随机推荐
[cloud native] deploy redis cluster in k8s
Maximum sum of jz42 continuous subarray (force buckle) (GIF diagram)
BUUCTF刷题十一道(05)
Video human behavior detection
Leetcode-470. implement rand10() with rand7()
Chrome realizes automated testing: recording and playback web page actions
Purple light FPGA solves the mask problem! Boost the overall speed-up of mask production
2022 review plan of joint provincial election
Six employees have been confirmed! Samsung closed the turtle tail mobile phone factory for the third time!
[NOI2018] 冒泡排序(组合+卡特兰数+dp+树状数组)
QT common operation collection
SSM integration process
一篇搞定Redis中的BigKey问题
US officials suggested trump prevent Infineon from acquiring cypress
Cache learning
Uniswap集成sudoswap,能否拉开NFT流动性新序幕?
带你掌握 Makefile 分析
Fluorescence imaging of cle19 polypeptide in cells preparation of fluorescence quenching quantum dots of bovine serum albumin
Shandong football match
我与消息队列的八年情缘