算法系列二:排序

一.快排:

1.代码:

package com.inspire.jdk.caculate;

/**
 * Created by
 */
public class QuickSort {

    public static void main(String[] args) {

        int[] a = new int[]{2,7,4,5,10,1,9,3,8,6};
        sort(a,0,a.length-1);

        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]);
        }

    }

    /**
     * 将数组的某一段元素进行划分,小的在左边,大的在右边,
     * @param data
     * @param start
     * @param end
     * @return
     */
    public static int divide(int[] data,int start,int end){
        //每次都以最右边的元素为基准值
        int base = data[end];
        //start一旦等于end,说明左右两个指针合并到了同一个位置,结束此轮循环
        while (start<end){

            while (start< end && data[start] <= base)
                //从左边开始遍历,如果比基准值小,继续向右走
                start++;
            //当while循环结束时,说明当前的data[start]比基准值大,应该进行交换
            if(start < end){
                //交换
                int temp = data[start];
                data[start] = data[end];
                data[end] = temp;
                //交换后,被调换的值调到了基准值的右边,因此右边同时向前移动一位
                end--;
            }
            while (start < end && data[end] >= base)
                end--;
            if(start < end){
                int temp = data[start];
                data[start] = data[end];
                data[end] = temp;
                start++;
            }
        }
        //返回start,end都可以.此时start和end都为基准值所在的位置
        return end;

    }

    /**
     * 排序
     * @param data
     * @param start
     * @param end
     */
    public static void sort(int[] data,int start,int end){
        if (start > end){
            //如果只有一个元素就不用排序了
            return;
        }else {
            int position = divide(data,start,end);
            sort(data,start,position-1);
            sort(data,position+1,end);
        }
    }
}

二.归并排序

1.代码:

package com.inspire.jdk.caculate;

import java.util.Arrays;

/**
 * Created by
 * 归并算法原理:
 * 1)申请空间,使其大小为两个已经排序序列之和,该空间用来存放合并后的序列
 (2)设定两个指针,最初位置分别为两个已经排序序列的起始位置
 (3)比较两个指针所指向的元素,选择相对小的元素放入到合并空间,并移动指针到下一位置
 (4)重复步骤3直到某一指针达到序列尾
 (5)将另一序列剩下的所有元素直接复制到合并序列尾
 参考:https://blog.csdn.net/jianyuerensheng/article/details/51262984
 *
 */
public class MergeSort {
    public static void main(String[] args) {
        int[] data = { 51, 46, 20, 18};
        mergeSort(data,0,data.length-1);
        System.out.println(Arrays.toString(data));

    }

    public static void merge(int[] data,int low,int mid,int high){

        //新建一个数组
        int[] temp = new int[high - low + 1];
        //左指针
        int i = low;
        //右指针
        int j = mid+1;
        int k = 0;
        //把较小的元素先移到数组中
        while (i <= mid && j <= high){
            if (data[i] < data[j]){
                //先把左边的移动到新数组中
                //temp[k++] = data[i++];
                temp[k] = data[i];
                k++;
                i++;
            }else {
                //先把右边的移动到新数组中
                //temp[k++] = data[j++];
                temp[k] = data[j];
                k++;
                j++;
            }
        }
        //把左边剩余的元素移动到数组中
        while (i <= mid){
            temp[k++] = data[i++];
        }
        //把右边剩余的元素移动到数组中
        while (j <= high){
            temp[k++] = data[j++];
        }

        //将临时数组元素拷贝到原数组
        for (int l = 0; l < temp.length; l++) {
            data[l+low] = temp[l];
        }

    }

    public static void mergeSort(int[] data,int low,int high){

        int mid = (low+high)/2;
        if (low < high){
            //二路归并排序里面有两个Sort,多路归并排序里面写多个Sort就可以了
            //左边
            mergeSort(data,low,mid);
            //右边
            mergeSort(data,mid+1,high);

            //左右归并
            merge(data,low,mid,high);

            System.out.println(Arrays.toString(data));

        }
    }
}

猜你喜欢

转载自www.cnblogs.com/inspred/p/9240755.html