ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

八大排序算法总汇

2021-05-20 11:34:42  阅读:131  来源: 互联网

标签:arr int high ++ 算法 总汇 low static 排序


//冒泡排序:

public static void maopao(){
int[]a={1,3,2,5,9,8,7};
for (int i=0;i<a.length-1;i++){
for (int j=0;j< a.length-i-1;j++){
if (a[j]>a[j+1]){
a[j]=a[j]^a[j+1];
a[j+1]=a[j+1]^a[j];
a[j]=a[j]^a[j+1];
}
}
}
}

//基数排序:

public static void jishu(int[]arr){
if(arr.length<=1)
return;
int max=0;
for (int i=0;i<arr.length;i++){
if(max<arr[i]){
max=arr[i];
}
}
int maxDight=1;
while (max/10>0){
maxDight++;
max=max/10;
}
int[][]tong=new int[10][arr.length];
int base=10;
for (int i=0;i<maxDight;i++){
int[]tonglength=new int[10];
for (int j=0;j<arr.length;j++){
int whichtong=(arr[j]%base)/(base/10);
tong[whichtong][tonglength[whichtong]]=arr[j];
tonglength[whichtong]++;
}
int k=0;
for (int b=0;b< tong.length;b++){
for (int p=0;p<tonglength[b];p++){
arr[k++]=tong[b][p];
}
}
}
}

//堆排序:

public static void max_heapify(int[]a,int n){
int child;
for (int i=(n-1)/2;i>=0;i--){
child=2*i+1;
if(child!=n&&a[child]<a[child+1]){
child++;
}
if(a[i]<a[child]){
int temp=a[i];
a[i]=a[child];
a[child]=temp;
}
}
}
public static void dui(int[]a){
for (int i=a.length-1;i>0;i--){
max_heapify(a,i);
int temp=a[0];
a[0]=a[i];
a[i]=temp;
}
}

//希尔排序:

public static void xier(int[]a){
int length=a.length;
int h=1;
while (h<a.length/3) h=3*h+1;
for (;h>=1;h/=3){
for (int i=0;i<a.length-h;i+=h){
for (int j=i+h;j>0;j-=h){
if (a[j] < a[j - h]) {
int temp = a[j];
a[j] = a[j - h];
a[j - h] = temp;
}
}
}
}
}

//归并排序:

private static int[] aux;
public static void sort(int[]a){
aux=new int[a.length];
sort(a,0,a.length-1);
System.out.println(Arrays.toString(a));
}
public static void sort(int[]a,int low,int high){
if(low>=high){
return;
}
int mid=(low+high)/2;
sort(a,low,mid);//
sort(a,mid+1,high);//
guibing(a,low,mid,high);
}

public static void guibing(int[]a,int low,int mid,int high){
int i=low,j=mid+1;
for (int k=low;k<=high;k++){
aux[k]=a[k];
}
for (int k=low;k<=high;k++){
if(i>mid){
a[k]=aux[j++];
}else if(j>high){
a[k]=aux[i++];
}else if(aux[j]<aux[i]){
a[k]=aux[j++];
}else {
a[k]=aux[i++];
}
}
}

//快速排序:

public static void main(String[] args) {
int[] arr = { 49, 38, 65, 97, 23, 22, 76, 1, 5, 8, 2, 0, -1, 22 };
quickSort(arr, 0, arr.length - 1);
System.out.println("排序后:");
for (int i : arr) {
System.out.print(i+" ");
}
}

private static void quickSort(int[] arr, int low, int high) {
if (low < high) {
int index = getIndex(arr, low, high);
quickSort(arr, low, index - 1);
quickSort(arr, index + 1, high);
}

}
private static int getIndex(int[] arr, int low, int high) {
int tmp = arr[low];
while (low < high) {
while (low < high && arr[high] >= tmp) {
high--;
}
arr[low] = arr[high];
while (low < high && arr[low] <= tmp) {
low++;
}
arr[high] = arr[low];

}
arr[low] = tmp;
return low;
}

//插入排序:

public static void charu(int[]nums){
for (int i=1;i<nums.length;i++){
int temp=nums[i];
int insertPost=-1;
for (int j=i-1;j>=0;j--){
if(temp<nums[j]){
nums[j+1]=nums[j];
insertPost=j;
}else {
break;
}
}
if(insertPost!=-1){
nums[insertPost]=temp;
}
}
}

//选择排序:

public static void xuanze(){
int[] arr = {5, 4, 7, 1, 8, 2, 3, 6, 9};
for (int i = 0; i < arr.length ; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] >arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}

标签:arr,int,high,++,算法,总汇,low,static,排序
来源: https://www.cnblogs.com/hhc16619804375/p/14789150.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有