冒泡、直接插入、归并排序、快速排序

参考:https://www.cnblogs.com/chengxiao/p/6103002.html

          http://www.cnblogs.com/chengxiao/p/6104371.html

          https://www.cnblogs.com/chengxiao/p/6129630.html

          https://www.cnblogs.com/chengxiao/p/6194356.html

 

冒泡排序:

package com.sxj.Paixu;
/**
 * <pre>
 *     author: sxj
 *     blog  : https://www.cnblogs.com/sxj1/ 
 *     time  : 2018/11/28
 *     desc  :
 * </pre>
 */
public class Maopao {
    
    public static void bubblesort(int[] arr) {
        //设定一个标志
        boolean flag=true;
        for(int i=0;i<arr.length-1&&flag;i++) {
            //无交换
            flag=false;
            for(int j=arr.length-2;j>=i;j--) {
                
                if(arr[j]>arr[j+1]) {
                    Kuaishu.swap(arr, j, j+1);
                    //有交换
                    flag=true;
                }
            }
        }
    }


}

直接插入:

package com.sxj.Paixu;

/**
 * @author sxj
 * @param arr
 *
 */
public class Charu {
    public static void insertionSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int j = i;
            while (j > 0 && arr[j] < arr[j - 1]) {
                Kuaishu.swap(arr,j,j-1);
                j--;
            }
        }
    }

}

 

快速排序:(关键字的比较和交换是跳跃进行的,因此不稳定)

package com.sxj.Paixu;
/**
 * <pre>
 *     author: sxj
 *     blog  : https://www.cnblogs.com/sxj1/ 
 *     time  : 2018/11/28
 *     desc  :
 * </pre>
 */
public class Kuaishu {
    public static void sort(int[] array,int low ,int hig) {
        int pivot;
        if(low<hig) {
            //算出枢轴值
            pivot=parttion(array,low,hig);
            //分别对低、高子表递归排序
            sort(array,low,pivot-1);
            sort(array,pivot+1,hig);
            
        }
    }
    
     public static void sort(int []arr){
            sort(arr,0,arr.length-1);
        }
    
    public static int parttion(int[] array,int low,int hig) {
//        int pivot;
        //三数取中法
        int mid=low+(hig-low)/2;
        //现将hig置最高点,那么中间值只有low或者mid,接下来就是两个数的比较问题
        if(array[low]>array[hig]) {
            swap(array,low,hig);
        }
        if(array[mid]>array[hig]) {
            swap(array,mid,hig);
        }
        if(array[low]<array[mid]) {
            swap(array,low,mid);    
        }
        int key=array[low];
        
        //排序过程,key值左边的数都小于key,右边的数大于key,最后排于low=hig
        while(low<hig) {
            while(low<hig&&array[hig]>=key) {
                hig--;
            }
            array[low]=array[hig];
            
            while(low<hig&&array[low]<=key) {
                low++;
            }
            array[hig]=array[low];
        }
        //low为
        array[low]=key;
        return low;
        
        
    }

    public static void swap(int[] arr,int a,int b) {
        int temp=arr[a];
        arr[a]=arr[b];
        arr[b]=temp;
    }
    
    
}

归并排序:

package com.sxj.Paixu;

//import java.util.Arrays;

/**
 * <pre>
 *     author: sxj
 *     blog  : https://www.cnblogs.com/sxj1/ 
 *     time  : 2018/11/28
 *     desc  :
 * </pre>
 */
//import java.util.Arrays;

public class Guibing {
    /*
     * 
    public static void main(String []args){
        int []arr = {9,8,7,6,5,4,3,2,1};
        sort(arr);
        System.out.println(Arrays.toString(arr));
    }
    */
    public static void sort(int []arr){
        int []temp = new int[arr.length];//在排序前,先建好一个长度等于原数组长度的临时数组,避免递归中频繁开辟空间
        sort(arr,0,arr.length-1,temp);
    }
    private static void sort(int[] arr,int left,int right,int []temp){
        if(left<right){
            int mid = (left+right)/2;
            sort(arr,left,mid,temp);//左边归并排序,使得左子序列有序
            sort(arr,mid+1,right,temp);//右边归并排序,使得右子序列有序
            merge(arr,left,mid,right,temp);//将两个有序子数组合并操作
        }
    }
    private static void merge(int[] arr,int left,int mid,int right,int[] temp){
        int i = left;//左序列指针
        int j = mid+1;//右序列指针
        int t = 0;//临时数组指针
        while (i<=mid && j<=right){
            if(arr[i]<=arr[j]){
                temp[t++] = arr[i++];
            }else {
                temp[t++] = arr[j++];
            }
        }
        while(i<=mid){//将左边剩余元素填充进temp中
            temp[t++] = arr[i++];
        }
        while(j<=right){//将右序列剩余元素填充进temp中
            temp[t++] = arr[j++];
        }
        t = 0;
        //将temp中的元素全部拷贝到原数组中
        while(left <= right){
            arr[left++] = temp[t++];
        }
    }
}

 

测试代码:

package com.sxj.Paixu;

import java.util.Arrays;
/**
 * <pre>
 *     author: sxj
 *     blog  : https://www.cnblogs.com/sxj1/ 
 *     time  : 2018/11/28
 *     desc  :
 * </pre>
 */
public class Test {

    public static void main(String[] args) {
        // TODO 自动生成的方法存根
        int[] arr1 = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
        int[] arr2= {1,3,4,9,4,2,5,1,0,3,7};
//        Guibing s=new Guibing();
        Guibing.sort(arr1);
        Kuaishu.sort(arr2);
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));

    }

} 

 

猜你喜欢

转载自www.cnblogs.com/sxj1/p/10057359.html