Java实现的多种排序

import org.omg.CORBA.NO_IMPLEMENT;

import javax.validation.constraints.Max;
import java.util.*;

public class Text {

    public  void BubbleSort(List<Integer> arrayList){
        int size = arrayList.size();
        for(int i=0;i<size-1;i++){
            for(int j=0;j<size-i-1;j++){
                if(arrayList.get(j) < arrayList.get(j+1)){
                    int tmp = arrayList.get(j);
                    arrayList.set(j,arrayList.get(j+1));
                    arrayList.set(j+1,tmp);
                }
            }
        }
    }
    public  static  void InsertSort(int[] num, int n){
        for(int i=1;i<n;i++){
            if(num[i]>=num[i-1]){
                continue;
            }
            int j = i;
            while((j>0)&&(num[j]<num[j-1])){
                int tmp = num[j];
                num[j]=num[j-1];
                num[j-1]=tmp;
                j--;
            }
        }
    }
    public static  void  HashSort(int[] num){
        int max_num = num[0];
        int min_num = num[0];
        for(int i=1;i<num.length;i++){
            if(num[i] > max_num){
                max_num = num[i];
            }
            if(num[i] < min_num){
                min_num = num[i];
            }
        }
        int[] hashnum = new int[max_num-min_num+1];
        for(int i=0;i<num.length;i++){
            hashnum[num[i]-min_num]++;
        }
        int cnt = 0;
        for(int i=0;i<=max_num-min_num;i++){
            while(hashnum[i]!=0){
                num[cnt++] = i+min_num;
                hashnum[i]--;
            }
        }
    }
    public  static  void ShellSort(int[] num ,int n,int d){
        if(d<=0){
            d = n/2;
        }
        for(int i=d;i>0;i=i/2){
            for(int j=i;j<n;j++){
                int z = j;
                if(num[z]>num[z-i]){
                    continue;
                }
                while((z-i>=0)&&num[z]<num[z-i]) {
                    int tmp = num[z];
                    num[z] = num[z - i];
                    num[z - i] = tmp;
                    z -= i;
                }
            }
        }
    }

    public  static void SelectSort(int[] num,int n){
        int max_num;
        int index;
        for(int i=1;i<n;i++){
            max_num = num[0];
            index =0;
            for(int j=1;j<n-i+1;j++){
                if(num[j]>max_num){
                    max_num = num[j];
                    index = j;
                }
            }
            num[index]=num[n-i];
            num[n-i]=max_num;
        }
    }

    public  static  int LeftSon(int n){
        return n*2+1;
    }
    public  static  int RigthSon(int n){
        return n*2+2;
    }
    public  static  int Parent(int n){
        return  (n-1)/2;
    }
    public  static  void Debug(int[] num){
        for(int i=0;i< num.length;i++){
            System.out.print(num[i]+" ");
        }
        System.out.println();
    }
    public  static  void HeapSort(int[] num){
        int _size = num.length;
        for(int i=1;i<_size;i++){
            int index = i;
            while (Parent(index)>=0&&num[index]>num[Parent(index)]){
                int tmp = num[Parent(index)];
                num[Parent(index)] = num[index];
                num[index] = tmp;
                index = Parent(index);
            }
        }
        for(int i=1;i<_size;i++){
            int tmp = num[0];
            num[0] = num[_size-i];
            num[_size-i] = tmp;
            int index = 0;
            while(LeftSon(index)<(_size-i)){
                if(num[LeftSon(index)]>num[index]){
                    if(num[RigthSon(index)]>=num[LeftSon(index)]&&RigthSon(index)<(_size-i)) {
                        int tmp_num = num[RigthSon(index)];
                        num[RigthSon(index)] = num[index];
                        num[index] = tmp_num;
                        index = RigthSon(index);
                    } else {
                        int tmp_num = num[LeftSon(index)];
                        num[LeftSon(index)] = num[index];
                        num[index] = tmp_num;
                        index = LeftSon(index);
                    }
                } else if(num[RigthSon(index)]>num[index]&&RigthSon(index)<(_size-i)){
                    int tmp_num = num[RigthSon(index)];
                    num[RigthSon(index)] = num[index];
                    num[index] = tmp_num;
                    index = RigthSon(index);
                } else {
                    break;
                }
            }
        }
    }
    public  static  void MegreSort(int[] arr,int left, int right , int[] temp){
        if (left<right){
            int mid = (left+right)/2;
            MegreSort(arr,left,mid,temp);
            MegreSort(arr,mid+1,right,temp);
            Megre(arr,left,right,mid,temp);
        }
    }
    public  static  void Megre(int[] num,int left,int right,int mid ,int [] temp){
        int _size = right-left+1;
        int left_i  = left;
        int left_j = mid+1;
        int cnt=left_i;
        for(int i=0;i<_size;i++){
            if(num[left_j]<num[left_i]){
                temp[cnt]=num[left_j];
                left_j++;
                cnt++;
            }
            else{
                temp[cnt]=num[left_i];
                left_i++;
                cnt++;
            }
            if(left_i>mid){
                for(int j=left_j;j<=right;j++){
                    temp[cnt++]=num[j];
                }
                break;
            }
            if(left_j>right){
                for(int j=left_i;j<=mid;j++){
                    temp[cnt++]=num[j];
                }
                break;
            }
        }
        for(int i=left;i<=right;i++){
            num[i]=temp[i];
        }
    }
    public  static  void MegreSort(int[] num){
        int[] temp = new  int[num.length];
        MegreSort(num,0,num.length-1,temp);
    }

    public static  void CountSort(int[] num){
        int max_num = num[0];
        int min_num = num[0];
        for(int i=1;i<num.length;i++) {
            if (num[i] > max_num) {
                max_num = num[i];
            }
            if (num[i] < min_num) {
                min_num = num[i];
            }
        }
        int tmp1=min_num;
        int tmp2=max_num;
        int[] arr_Count = new int[tmp2-tmp1+1];
        for(int i=0;i<num.length;i++){
            arr_Count[num[i]-tmp1]++;
        }
        for(int i=min_num+1;i<=max_num;i++){
            arr_Count[i-tmp1] += arr_Count[i-tmp1-1];
        }
        int[] ans = new int[num.length];
        for(int i=0;i<num.length;i++){
            ans[arr_Count[num[i]-tmp1]-1]=num[i];
            arr_Count[num[i]-tmp1]--;
        }
        for(int i=0;i<num.length;i++){
            num[i] = ans[i];
        }
    }
    static class  Node{
        int num;
        Node Next;
        void  push_back(Node n){
            Node node = this;
            while(node.Next!=null){
                node = node.Next;
            }
            node.Next = n;
            n.Next = null;
        }
        void  SetNum(int num){
            this.num = num;
        }
        void  Clear(){
            this.Next=null;
        }
    }
    public static  void RadixSort(int[] num){
        int max_num = num[0];
        for (int i = 1; i < num.length; i++) {
            if(num[i]>max_num){
                max_num = num[i];
            }
        }
        int BitCnt = 0;
        while(max_num!=0){
            max_num/=10;
            BitCnt++;
        }
        Node[] temp = new Node[10];
        for(int i=0;i<10;i++){
            temp[i] = new Node();
        }
        int[] tmp = new int[num.length];
        int snum = 1;
        while(BitCnt!=0) {
            int cnt = 0;
            for (int i = 0; i < num.length; i++) {
                Node node = new Node();
                node.SetNum(num[i]);
                temp[(num[i]/snum) % 10].push_back(node);
            }
            for (int i = 0; i <= 9; i++) {
                Node node = temp[i];
                while (node.Next != null) {
                    tmp[cnt++] = node.Next.num;
                    node = node.Next;
                }
                temp[i].Clear();
            }
            for(int i=0;i<num.length;i++){
                num[i] = tmp[i];
            }
            snum*=10;
            BitCnt--;
        }
    }

    public  static  void  QuickSort(int[] num,int left ,int right){
        if(left>=right) {
            return;
        }
        int Snum = num[left];
        int i = left;
        int j = right;
        while(i < j){
            while (i < j && num[j] >= Snum){
                j--;
            }
            if(i<j) num[i++]=num[j];
            while(i < j && num[i] <Snum){
                i++;
            }
            if(i<j) num[j--]=num[i];
        }
        num[i] = Snum;
        QuickSort(num,left,i);
        QuickSort(num,i+1,right);
    }
    public static  void BucketSort(int[] num){
        int max_num = num[0];
        int min_num = num[0];
        for(int i=1;i<num.length;i++){
            if(num[i]>max_num){
                max_num = num[i];
            }
            if(num[i]<min_num){
                min_num = num[i];
            }
        }
        int[][]nums = new int[max_num/10-min_num/10+1][10000];
        int[] cnt = new int[max_num/10-min_num/10+1];
        for(int i=0;i<num.length;i++){
            nums[num[i]/10-min_num/10][cnt[num[i]/10-min_num/10]] = num[i];
            cnt[num[i]/10-min_num/10]++;
        }
        for(int i=0;i<max_num/10-min_num/10+1;i++){
            QuickSort(nums[i],0,cnt[i]-1);
        }
        int z = 0;
        for(int i=0;i<max_num/10-min_num/10+1;i++){
            for(int j=0;j<cnt[i];j++){
                num[z++]=nums[i][j];
            }
        }
    }
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        Collections.addAll(list,123,1,345,12,111,111111,1234);
        Text text = new Text();
        for(int i=0;i<list.size();i++){
            System.out.print(list.get(i)+" ");
        }
        System.out.println("--------------------------");
        // int[] num = new int[]{123,1,345,12,111,111111,1234};
        int[] num = new int[]{1,21,31,11};
        Text.BucketSort(num);
        System.out.println();
        for(int i=0;i<num.length;i++){
            System.out.print(num[i]+" ");
        }

    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值