java实现排序算法,快排,堆排序,冒泡,归并排序

package practice;

import java.util.Arrays;

public class sortfamily {
    public static void main(String[]args){
        int []num = {1,4,3,2,5,6,7,8,9};
        sortfamily it = new sortfamily();
//        //it.quick(num,0,8);
//        it.heap(num);
        it.sort(num,0,8);
        System.out.println(Arrays.toString(num));
    }

    //冒泡法
    public void bub(int []num){
        int len = num.length;
        for(int i =0;i<len;i++){
            for(int j =0;j<len-i-1;j++){
                if(num[j]>num[j+1]){
                    int temp = num[j];
                    num[j] = num[j+1];
                    num[j+1] = temp;
                }    
            }
        }
    }

    //快排
    public void quick(int num[],int left,int right){
        if(left>right){
            return;
        }
        int low = left;
        int high = right;
        int len = num.length;
        int temp = num[low];
        while(low<high){
            while(low<high&&num[high]>temp){
                high--;
            }
            while(low<high&&num[low]<=temp){
                low++;
            }
            if(low<high){
                int t =num[low];
                num[low] = num[high];
                num[high] = t;
            }
        }
        num[left] = num[low];
        num[low]=temp;
        quick(num,left,low-1);
        quick(num,low+1,right);
    }

    //堆排序
    public void heap(int num[]){
        int len =num.length;
        for(int i =len;i>0;i--){
            heap_mk(num,i);
            swap(num,0,i-1);
        }
        
    }
    public void heap_mk(int num[],int limit){
        if(num.length<=0||num.length<limit){
            return ;
        }
        
        for(int parent = limit/2;parent>=0;parent--){
            if(parent*2>=limit){
                continue;
            }
            int left = parent*2;
            int right = left+1>=limit?left:left+1;
            int maxchild = num[left]>num[right]?left:right;
            if(num[parent]<num[maxchild]){
                swap(num,parent,maxchild);
            }
        }
        
    }
    public void swap(int []num,int left,int right){
            int t =num[left];
            num[left] = num[right];
            num[right] = t;
    }
    //二路归并排序
    public  int[] sort(int []num,int low,int high){
        int len = num.length;
        int mid = (high+low)/2;
        if(low<high){
            sort(num,low,mid);
            sort(num,mid+1,high);
            merge(num,low,mid,high);
        }
        return num;
    }
    public  void merge(int num[],int low,int mid,int high){
        int []temp = new int[high-low+1];
        int k =0;
        int i = low;
        int j = mid+1;
        while(i<=mid&&j<=high){
            if(num[i]<num[j]){
                temp[k++] = num[i++];
            }else{
                temp[k++] = num[j++];
            }
        }
        //处理剩余的数字
        while(i<=mid){
            temp[k++]=num[i++];
        }
        while(j<=high){
            temp[k++]=num[j++];
        }
        for(int x=0;x<temp.length;x++){
            num[x+low]=temp[x];
        }
    }
}
   

猜你喜欢

转载自blog.csdn.net/weixin_40106836/article/details/89335324