数据结构-算法(排序)(java实现)

1、交换排序

import java.util.Arrays;

//交换排序
public class ChangeSort {
    public static void swap(int[] array, int i, int j){
        int temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    //冒泡排序
    public static void bubbleSort(int[] array){
        if (array == null) return;
        int len = array.length;;
        for(int i = 0; i < len-1; i++){
            for (int j = len-1; j > i; j--){
                if (array[j] < array[j-1] ){
                    swap(array,j,j-1);
                }
            }
        }
    }

    //快速排序:
    /*1.从数列中选出一个元素,称为“基准”(pivot)
      2.重新排序数列,所有元素比基准小的摆放在基准前面,大的摆放在基准后面。在这个分区退出后,该基准就处于数列的中间位置。该步骤为分区(partition)操作
      3.递归地把小于基准值元素的子数列和大于基准值元素的子数列排序*/
    public static void quickSort(int[] array, int low, int high){
        if (array == null || low < 0 || high < 0 || low >= array.length) return;
        int pivotloc = partition(array, low, high);
        if(low < high){
            quickSort(array, low, pivotloc-1);
            quickSort(array,pivotloc+1,high);
        }
    }    
    public static int partition(int[] array, int low, int high){
        int pivokey = array[low];
        while(low < high){
            while(low < high && array[high] >= pivokey)
            {
                high--;
            }
            array[low] = array[high];
            while(low < high && array[low] <= pivokey)
            {
                low++;
            }
            array[high] = array[low];
        }
        array[low] = pivokey;
        return low;
    }
    public static void Test(int a[],int b[]) {
        System.out.println("The Source Secquence:");
        if (a == null) return;
        System.out.println(Arrays.toString(a));

        bubbleSort(a);
        System.out.println("BubbleSort Result: ");
        System.out.println(Arrays.toString(a));

        quickSort(b, 0, b.length-1);
        System.out.println("QuickSort Result:");
        System.out.println(Arrays.toString(b));
        System.out.println();
    }
    public static void main(String[] args){
        int a1[] = null;
        int a2[] = {1};
        int a3[] = {3, 6, 1, 8, 2, 9, 4};
        int a4[] = {1, 3, 5, 7, 9};
        int a5[] = {6, 9, 4, 8, -1};
        int a6[] = {9, 5, 4, 2, 1};
        int b1[] = null;
        int b2[] = {1};
        int b3[] = {3, 6, 1, 8, 2, 9, 4};
        int b4[] = {1, 3, 5, 7, 9};
        int b5[] = {6, 9, 4, 8, -1};
        int b6[] = {9, 5, 4, 2, 1};
        Test(a1,b1);
        Test(a2,b2);
        Test(a3,b3);
        Test(a4,b4);
        Test(a5,b5);
        Test(a6,b6);
    }
}

2、插入排序

import java.util.Arrays;

public class InsertSort {

    //直接插入排序
    public static void insertDirectlySort(int a[]) {

        if (a == null) return;
        int len = a.length;
        try {
            for (int i = 0; i < len; i++) {//第一个值开始遍历
                for (int j = i + 1; j < len && j > 0; j--) {//将j位置前排序,遍历j与前面所有值
                    if (a[j] < a[j - 1]) {//相邻值比较,小于则交换
                        int temp = a[j];
                        a[j] = a[j - 1];
                        a[j - 1] = temp;
                    }

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }



    }
    //shell排序
    public static void shellSort(int data[]) {
        if (data == null) return;
        int j = 0;
        int temp = 0;
        int len = data.length / 2;
        for (int increment  = len; increment > 0; increment /= 2) {//从该增量开始比较排序,以该处为距离倍数对比

            for (int i = increment; i < data.length; i++) {
                temp = data[i];
                for (j = i; j >= increment; j -= increment) {
                    if(temp < data[j - increment]){//j-increment的值与temp对比
                        data[j] = data[j - increment];
                        System.out.println(Arrays.toString(data));
                    }else{
                        break;
                    }
                }
                data[j] = temp;//最后将temp插入该块最小的位置处,或不变
                System.out.println(Arrays.toString(data));
            }
        }
    }

    public static void Test(int a[],int b[]) {
        System.out.println("The Source Secquence:");
        if (a == null) return;
        System.out.println(Arrays.toString(a));


        insertDirectlySort(a);

        System.out.println("InsertDirectlySort Result: ");
        System.out.println(Arrays.toString(a));



       shellSort(b);
        //输出结果
        System.out.println("ShellSort Result:");
        System.out.println(Arrays.toString(b));
        System.out.println();
    }
    public static void main(String[] args){
        //基本数据数值按址传参,设两对相同内容数值分别进行排序
        int a1[] = null;
        int a2[] = {1};
        int a3[] = {3, 6, 1, 8, 2, 9, 4};
        int a4[] = {1, 3, 5, 7, 9};
        int a5[] = {6, 9, 4, 8, -1};
        int a6[] = {9, 5, 4, 2, 1};
        int b1[] = null;
        int b2[] = {1};
        int b3[] = {9, 4, 1, 5, 2, 3};
        int b4[] = {1, 3, 5, 7, 9};
        int b5[] = {6, 9, 4, 8, -1};
        int b6[] = {9, 5, 4, 2, 1};
        System.out.println("Test-1\n");
        Test(a1,b1);
        System.out.println("------------------------------\n");
        System.out.println("Test-2\n");
        Test(a2,b2);
        System.out.println("------------------------------\n");
        System.out.println("Test-3\n");
        Test(a3,b3);
        System.out.println("------------------------------\n");
        System.out.println("Test-4\n");
        Test(a4,b4);
        System.out.println("------------------------------\n");
        System.out.println("Test-5\n");
        Test(a5,b5);
        System.out.println("------------------------------\n");
        System.out.println("Test-6\n");
        Test(a6,b6);
        System.out.println("------------------------------\n");
        System.out.println("-----End-----");
    }
}


3、归并排序

import java.util.Arrays;

//归并排序
public class MergingSort {

    public static void Test(int a[]) {
        System.out.println("The Source Secquence:");
        if (a == null) return;
        System.out.println(Arrays.toString(a));

        mergeSort(a,0,a.length-1);
        System.out.println("MergeSort Result: ");
        System.out.println(Arrays.toString(a));
        System.out.println();
    }
    public static void main(String[] args){
        int a1[] = null;
        int a2[] = {1};
        int a3[] = {3, 6, 1, 8, 2, 9, 4};
        int a4[] = {1, 3, 5, 7, 9};
        int a5[] = {6, 9, 4, 8, -1};
        int a6[] = {9, 5, 4, 2, 1};
        Test(a1);
        Test(a2);
        Test(a3);
        Test(a4);
        Test(a5);
        Test(a6);
    }

    //归并排序:将原始序列数组对半分,将分后的两个序列排序分别进行对半分,直至low=mid,左右归并排序后递归返还排序结果,最后将原始的左右归并排序
    /*1、申请空间,使其大小为两个已经排序序列之和,该空间用来存放合并后的序列
      2、设定两个指针,最初位置分别给两个已经排序序列的起始位置
      3、比较两个指针所指空间的元素,选择相对小的元素放入合并空间,并移动指针到下一位置
      4、将另一序列剩下的所有元素之间复制到合并序列尾*/
    public static int[] mergeSort(int[] nums, int low, int high) {
        if (nums == null || low < 0 || low > nums.length-1 || high < 0) return nums;
        int mid = (low + high) / 2;//取中间值
        if (low < high) {
            // 左边,递归到low=mid
            mergeSort(nums, low, mid);
            // 右边,递归到mid+1=high
            mergeSort(nums, mid + 1, high);
            // 左右归并
            merge(nums, low, mid, high);
        }
        return nums;//返回归并后的nums排序
    }
    //归并
    public static void merge(int[] nums, 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 (nums[i] < nums[j]) {
                temp[k++] = nums[i++];//赋值后K++,i++
            } else {
                temp[k++] = nums[j++];
            }
        }
        // 把左边剩余的数移入数组
        while (i <= mid) {
            temp[k++] = nums[i++];
        }
        // 把右边边剩余的数移入数组
        while (j <= high) {
            temp[k++] = nums[j++];
        }
        // 把新数组中的数覆盖nums数组的相对位置
        for (int k2 = 0; k2 < temp.length; k2++) {
            nums[k2 + low] = temp[k2];
        }
    }
}

4、选择排序

import java.util.Arrays;

public class SelectSort {
    //直接选择排序
    public static void selectDirectlySort(int[] a) {
        if (a == null) return;
        int min = 0;
        int i = 0;
        int j = 0;
        int index = 0;
        int len = a.length;
        for (i = 0; i < len - 1; i++) {
            min = a[i];//给定假设的最小值
            index = i;
            for (j = i + 1; j < len; j++) {//从当前最小值往后遍历
                if (a[j] < min) {
                    min = a[j];//最小值从新赋值
                    index = j;
                }
            }
            a[index] = a[i];
            a[i] = min;//排序后最小值位置
        }
    }

    //堆排序
    /*1.构建一个基础堆
      2.堆首与堆尾置换(将最大值置换到末尾)
      3.把堆的尺寸缩小1,并调用heapify()将新的无序堆重新排序
      4.循环2,3步骤,直至堆的尺寸为1
      */

    public static void heapSort(int[] array) {
        if (array == null) return;
        buildHeap(array);//构建基础堆
        int n = array.length;
        int i = 0;
        for (i = n - 1; i >= 1; i--) {
            swap(array, 0, i);//交换堆的第一个值到最后,得到array[0]~array[i--]构成的新的无序堆
            heapify(array, 0, i);//将该无序数组重新构建成堆
        }
    }
    //构建基础堆
    public static void buildHeap(int[] array) {
        int n = array.length;//数组中元素的个数
        for (int i = n / 2 - 1; i >= 0; i--)
            heapify(array, i, n);//构建基础堆,使得每个父节点大于其子节点,子节点大小不做比较
    }
    public static void heapify(int[] A, int idx, int max) {
        int left = 2 * idx + 1;// 左节点的下标(如果存在的话)
        int right = 2 * idx + 2;// 左节点的下标(如果存在的话)
        int largest = 0;
        //寻找3个节点中最大值节点的下标
        if (left < max && A[left] > A[idx])
            largest = left;//左节点值大于父节点值,最大值节点重新赋值
        else
            largest = idx;//当前节点最大,跳出该构建堆方法
        if (right < max && A[right] > A[largest])
            largest = right;//右节点值大于最大值节点值,最大值节点重新赋值
        if (largest != idx) {
            swap(A, largest, idx);//将最大值位置与idx交换

            heapify(A, largest, max);//保证该节点的左右子节点小于该节点
        }
    }
    public static void swap(int[] array, int i, int j) {
        int temp = 0;
        temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }

    public static void Test(int a[], int b[]) {
        System.out.println("The Source Secquence:");
        if (a == null) return;
        System.out.println(Arrays.toString(a));

        selectDirectlySort(a);
        System.out.println("BubbleSort Result: ");
        System.out.println(Arrays.toString(a));

        heapSort(b);
        System.out.println("QuickSort Result:");
        System.out.println(Arrays.toString(b));
        System.out.println();
    }

    public static void main(String[] args) {
        int a1[] = null;
        int a2[] = {1};
        int a3[] = {3, 6, 1, 8, 2, 9, 4};
        int a4[] = {1, 3, 5, 7, 9};
        int a5[] = {6, 9, 4, 8, -1};
        int a6[] = {9, 5, 4, 2, 1};
        int b1[] = null;
        int b2[] = {1};
        int b3[] = {3, 6, 1, 8, 2, 9, 4};
        int b4[] = {1, 3, 5, 7, 9};
        int b5[] = {6, 9, 4, 8, -1};
        int b6[] = {9, 5, 4, 2, 1};
        Test(a1, b1);
        Test(a2, b2);
        Test(a3, b3);
        Test(a4, b4);
        Test(a5, b5);
        Test(a6, b6);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_34866380/article/details/80009708