排序算法(Java实现)

一、冒泡排序

package com.study.数据结构.排序算法;

/**
 * @author sjn
 * @date 2022-1-15
 */

import java.util.Arrays;

public class BubbleSort {
    public static void main(String[] args) {
        int[] sort = {1, 5, 6, 3, 9, 4, 5};
        System.out.println(Arrays.toString(sort));

        BubbleSort(sort);
        System.out.println(Arrays.toString(sort));
    }

    //冒泡排序
    public static void BubbleSort(int[] sort) {
        //控制共比较多少轮
        for (int i = 0; i < sort.length - 1; i++) {
            //控制比较的次数
            for (int j = 0; j < sort.length - 1 - i; j++) {
                if (sort[j] > sort[j + 1]) {
                    //实现交换
                    int temp = sort[j];
                    sort[j] = sort[j + 1];
                    sort[j + 1] = temp;
                }
            }
        }
    }

}


二、快速排序

package com.study.数据结构.排序算法;

/**
 * @author sjn
 * @date 2022-1-15
 */

import java.util.Arrays;

public class QuickSort {
    public static void main(String[] args) {
        int[] sort = {1, 5, 6, 3, 9, 4, 5};
        System.out.println(Arrays.toString(sort));

        quickSort(sort, 0, sort.length - 1);
        System.out.println(Arrays.toString(sort));
    }

    //快速排序
    public static void quickSort(int[] sort, int start, int end) {
        if (start < end) {
            //把数组中的第0个数字作为标准数
            int stard = sort[start];
            //记录下需要排序的下标
            int low = start;
            int high = end;
            while (low < high) {
                //右边的数字比标准数大
                while (low < high && sort[high] >= stard) {
                    high--;
                }
                //使用右边的数字替换左边的数
                sort[low] = sort[high];
                //如果左边的数字比标准数小
                while (low < high && sort[low] <= stard) {
                    low++;
                }
                //使用左边的数字替换右边的数
                sort[high] = sort[low];
            }
            //把标准数赋给低所在的位置的元素
            sort[low] = stard;
            //处理所有的比标准数小的数字
            quickSort(sort, start, low);
            //处理所有的比标准数大的数字
            quickSort(sort, low + 1, end);
        }
    }
}

三、插入排序

package com.study.数据结构.排序算法;

/**
 * @author sjn
 * @date 2022-1-15
 */

import java.util.Arrays;

public class InsertSort {
    public static void main(String[] args) {
        int[] sort = {1, 5, 6, 3, 9, 4, 5};
        System.out.println(Arrays.toString(sort));

        insertSort(sort);
        System.out.println(Arrays.toString(sort));
    }

    //插入排序
    public static void insertSort(int[] sort) {
        //遍历所有的数字
        for (int i=1; i<sort.length; i++) {
            //如果当前数字比前一个数字小
            if (sort[i] < sort[i-1]) {
                //把当前遍历数字存起来
                int temp = sort[i];
                //遍历当前数字前面所有的数字
                int j;
                for (j=i-1; j>=0&&temp<sort[j];j--) {
                    //把前一个数字赋给后一个数字
                    sort[j+1] = sort[j];
                }
                //把临时变量(外层for循环的当前元素)赋给不满足条件的后一个元素
                sort[j+1] = temp;
            }
        }
    }
}

四、希尔排序

package com.study.数据结构.排序算法;

/**
 * @author sjn
 * @date 2022-1-15
 */

import java.util.Arrays;

public class ShellSort {
    public static void main(String[] args) {
        int[] sort = {1, 5, 6, 3, 9, 4, 5};
        System.out.println(Arrays.toString(sort));

        shellSort(sort);
        System.out.println(Arrays.toString(sort));

    }

    //希尔排序
    public static void shellSort(int[] sort) {
        int gap = sort.length;
        while (gap > 1) {
            gap = gap / 2;
            for (int i = 0; i < sort.length - gap; i++) {
                int end = i;
                int temp = sort[i + gap];
                while (end >= 0) {
                    if (temp > sort[i]) {
                        sort[end + gap] = sort[end];
                        end = end - temp;
                        sort[end + gap] = temp;
                    } else {
                        break;
                    }
                }
            }
        }
    }
}

五、选择排序

package com.study.数据结构.排序算法;

/**
 * @author sjn
 * @date 2022-1-16
 */

import java.util.Arrays;

public class SelectSort {
    public static void main(String[] args) {
        int[] sort = {1, 5, 6, 3, 9, 4, 5};
        System.out.println(Arrays.toString(sort));

        SelectSort(sort);
        System.out.println(Arrays.toString(sort));
    }

    //选择排序
    public static void SelectSort(int[] sort) {
        //遍历所有的数
        for (int i=0; i<sort.length; i++) {
            int minIndex = i;
            //把当前遍历的数和后面所有的数依次进行比较,并记录下最小的数的下标
            for (int j=i+1; j<sort.length; j++) {
                //如果后面比较的数比记录的最小的数小
                if (sort[minIndex] > sort[j]) {
                    //记录下最小的那个数的下标
                    minIndex = j;
                }
            }
            //如果最小的数和当前遍历数的下标不一致,说明下标为minIndex的数比当前遍历的数更小。
            if (i != minIndex) {
                int temp = sort[i];
                sort[i] = sort[minIndex];
                sort[minIndex] = temp;
            }
        }
    }
}

六、归并排序

package com.study.数据结构.排序算法;

import java.util.Arrays;

/**
 * @author sjn
 * @date 2022-1-16
 */

public class MergeSort {
    public static void main(String[] args) {
        int[] sort = {1, 3, 5, 2, 4, 6, 8, 10};
        System.out.println(Arrays.toString(sort));

        mergeSort(sort, 0, sort.length - 1);
        System.out.println(Arrays.toString(sort));
    }

    //归并排序
    public static void mergeSort(int[] sort, int low, int high) {
        int middle = (high + low) / 2;
        if (low < high) {
            //处理左边
            mergeSort(sort, low, middle);
            //处理右边
            mergeSort(sort, middle + 1, high);
            //归并
            merge(sort, low, middle, high);
        }
    }

    //归并
    public static void merge(int[] sort, int low, int middle, int high) {
        //用于存储归并后的临时数组
        int[] temp = new int[high - low + 1];
        //记录第一个数组中需要遍历的下标
        int i = low;
        //记录第二个数组中需要遍历的下标
        int j = middle + 1;
        //用于记录在临时数组中存放的下标
        int index = 0;
        //遍历两个数组取出小的数字,放入临时数组中
        while (i <= middle && j <= high) {
            //第一个数组的数据更小
            if (sort[i] <= sort[j]) {
                //把小的数据放入临时数组中
                temp[index] = sort[i];
                //让下标往后移一位
                i++;
            } else {
                temp[index] = sort[j];
                j++;
            }
            index++;
        }

        //处理多余的数据
        while (j <= high) {
            temp[index] = sort[j];
            j++;
            index++;
        }

        while (i <= middle) {
            temp[index] = sort[i];
            i++;
            index++;
        }

        //把临时数组中的数据重新存入原数组
        for (int k = 0; k < temp.length; k++) {
            sort[k + low] = temp[k];
        }
    }
}

七、基数排序

package com.study.数据结构.排序算法;

/**
 * @author sjn
 * @date 2022-1-16
 */

import java.util.Arrays;

public class RadixSort {
    public static void main(String[] args) {
        int[] sort = {23,6,189,45,9,287,56,1,798,34,65,652,5};
        System.out.println(Arrays.toString(sort));

        radixSort(sort);
        System.out.println(Arrays.toString(sort));
    }


    public static void radixSort(int[] sort) {
        //寸数组中最大的数字
        int max = Integer.MIN_VALUE;
        for (int i = 0; i < sort.length; i++) {
            if (sort[i] > max) {
                max = sort[i];
            }
        }

        //计算最大数字是几位数
        int maxLength = (max + "").length();
        //用于临时存储数据的数组
        int[][] temp = new int[10][sort.length];
        //用于记录在temp中相应的数组中存放的数字的数量
        int[] counts = new int[10];
        //根据最大长度的数决定比较的次数
        for (int i = 0, n = 1; i < maxLength; i++, n *= 10) {
            //把每一个数字分别计算余数
            for (int j = 0; j < sort.length; j++) {
                //计算余数
                int ys = sort[j] / n % 10;
                //把当前遍历的数据放入指定的数组中
                temp[ys][counts[ys]] = sort[j];
                //记录数量
                counts[ys]++;
            }
            //记录取的元素需要放的位置
            int index = 0;
            //把数字取出来
            for (int k = 0; k < counts.length; k++) {
                //记录数量的数组中当前余数记录的数量不为0
                if (counts[k] != 0) {
                    //循环取出元素
                    for (int l = 0; l < counts[k]; l++) {
                        //取出元素
                        sort[index] = temp[k][l];
                        //记录下一个位置
                        index++;
                    }
                    //把数量置为0
                    counts[k] = 0;
                }
            }
        }
    }

}

八、堆排序

package com.study.数据结构.排序算法;

import java.util.Arrays;

/**
 * @author sjn
 * @date 2022-1-17
 */

public class HeapSort {
    public static void main(String[] args) {
        int[] arr = {9, 6, 8, 7, 0, 1, 10, 4, 2};

        heapSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    //堆排序
    public static void heapSort(int[] arr) {
        //开始位置是最后一个非叶子节点,即最后一个节点的父节点
        int start = (arr.length - 1) / 2;
        //调整为一大顶堆
        for (int i = start; i >= 0; i--) {
            maxHeap(arr, arr.length, i);
        }
        //先把数组中的第0个和堆中最后一个数交换位置,再把前面的处理为大顶堆
        for (int i=arr.length - 1; i > 0; i--) {
            int temp = arr[0];
            arr[0] = arr[i];
            arr[i] = temp;
            maxHeap(arr,i,0);
        }
    }

    public static void maxHeap(int[] arr, int size, int index) {
        //左子节点
        int leftNode = 2 * index + 1;
        //右子节点
        int rightNode = 2 * index + 2;
        int max = index;
        //和两个子节点分别对比,找出最大的节点
        if (leftNode < size && arr[leftNode] > arr[max]) {
            max = leftNode;
        }
        if (rightNode < size && arr[rightNode] > arr[max]) {
            max = rightNode;
        }
        //交换位置
        if (max != index) {
            int temp = arr[index];
            arr[index] = arr[max];
            arr[max] = temp;
            //交换位置以后,可能会破坏之前排好的堆,所以,之前排好的堆需要重新调整
            maxHeap(arr, size, max);
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值