排序 java(插入、冒泡、归并、快速、堆、桶排序(基数排序))

插入排序法


import java.util.Arrays;
import java.util.Random;

public class Book23_1 {
    public static void main(String[] args) {
        Random input = new Random();
        int[] list = new int[10];
        for (int i = 0; i< list.length;i++){
            list[i] = input.nextInt(100);
        }
        InsertionSort(list);
    }

    public static void InsertionSort(int[] list){
        for (int i = 1; i< list.length; i++){
            int ChaRu = list[i];
            int j;
            System.out.println("原来"+Arrays.toString(list));
            for (j = i-1; j >=0 && list[j] > ChaRu; j--){
                list[j+1] = list[j];
            }
            list[j+1] = ChaRu;
        }
    }
}
// 这个插入排序就是将 后面比较的数据保存下来,
// 然后对前面的数据与后面的数据进行比较,如果前大于后就直接将前赋值给后
// 最后将后面比较保存下来的数据赋值给 最前面的那个就行了

冒泡(暴力筛选)


import java.util.Arrays;
import java.util.Random;

public class Bmaopao23_3 {
    public static void main(String[] args) {
        Random input = new Random();
        int[] list = new int[20];
        for(int i = 0; i < list.length ; i++){
            list[i] = input.nextInt(100);
        }
        MaoPao(list);
        System.out.println(Arrays.toString(list));
    }

    public static void MaoPao(int[] list) {
        for (int i = 1; i < list.length; i++) {
            for (int j = 0; j < list.length - i; j++) {
                if (list[j] > list[j + 1]) {
//                    swap list[j] with list[j+1];
                    int zhong = list[j];
                    list[j] = list[j + 1];
                    list[j + 1] = zhong;
                }
            }
        }
    }

// 将没有交换的位置跳过
    public static void MaoPaoG(int[] list) {
        boolean needNextPass = true;
        for (int i = 1; i < list.length && needNextPass; i++) {
            needNextPass = false;
            for (int j = 0; j < list.length - i; j++) {
                if (list[j] > list[j + 1]) {
                    int temp = list[j + 1];
                    list[j + 1] = list[j];
                    list[j] = temp;
                    needNextPass = true;
                }
            }
        }
    }
}

归并


import java.util.Arrays;
import java.util.Random;

public class BGuiBing23_5 {
    public static void main(String[] args) {
        Random input = new Random();
        int[] list = new int[10];
        int i = 0;
        while (i < list.length) {
            list[i] = input.nextInt(100);
            i++;
        }
        System.out.println(Arrays.toString(list));
        mergeSort(list);
        System.out.println(Arrays.toString(list));
    }

    // 利用到了递归 
    // 归并 先分  分两段
    public static void mergeSort(int[] list) {
        if (list.length > 1) {
            int len = list.length;
            int[] d1 = new int[len / 2];
            int[] d2 = new int[len - d1.length];
            // 前一半
            System.arraycopy(list, 0, d1, 0, d1.length);
            mergeSort(d1);
//            System.out.println(Arrays.toString(d1) + " " + Arrays.toString(d2) + " " + Arrays.toString(list));
//            System.out.println("a");

            // 后一半
            System.arraycopy(list, d1.length, d2, 0, d2.length);
            mergeSort(d2);
//            System.out.println(Arrays.toString(d1) + " " + Arrays.toString(d2) + " " + Arrays.toString(list));
//            System.out.println("b");
            // 分完过后在进行合并
            merge(d1, d2, list);
//            System.out.println(Arrays.toString(d1) + " " + Arrays.toString(d2) + " " + Arrays.toString(list));
        }
    }

    // 归并 后合
    public static void merge(int[] d1, int[] d2, int[] heB) {
        int index1 = 0;
        int index2 = 0;
        int indexH = 0;
        while (index1 < d1.length && index2 < d2.length) {
            if (d1[index1] < d2[index2]) {
//                heB[indexH] = d1[index1];
//                index1++;
//                indexH++;
                heB[indexH++] = d1[index1++];
            } else {
//                heB[indexH] = d2[index2];
//                index2++;
//                indexH++;
                heB[indexH++] = d2[index2++];
            }
        }
        while (index1 < d1.length) {
            heB[indexH++] = d1[index1++];
        }
        while (index2 < d2.length) {
            heB[indexH++] = d2[index2++];
        }
    }
}

快速排序


import java.util.Arrays;
import java.util.Random;

public class BQuickSort23_8 {
    public static void main(String[] args) {
        Random input = new Random();
        int[] list = new int[10];
        int i = 0;
        while (i < list.length) {
            list[i] = input.nextInt(100);
            i++;
        }
        System.out.println(Arrays.toString(list));
        quickSort(list);
        System.out.println(Arrays.toString(list));
    }
    // 这个是用于用户使用的方法
    public static void quickSort(int[] list){
        quickSort(list, 0, list.length-1);
    }
    public static void quickSort(int[] list, int first, int last){
        // 这里就是我们 要进行排序的判断
        if(last > first){
            int pointIndex = partition(list,first,last);
            // 快排也要 先分  分为两段 后再去前面进行 基准点的判断 并将序排好

            // 前一段
            quickSort(list, first, pointIndex-1);

            // 后一段
            quickSort(list, pointIndex+1,last);
        }
    }

    // 这个方法是用于 将元素交换用的  将大于基准元素的 first  与 小于基准元素的 last 交换
    // 要有返回值   因为我们要返回 基准点的下标 这样 以基准点的下标为准 它的两边都是排好了序的了
    public static int partition(int[] list, int first, int last){
        int point = list[first];
        int low = first+1;
        int high = last;
        // 交换就出去
        while(high > low){
            // low 没有基准点大 并且 low <= high low++
//            while(list[low]<point && low<=high)
            while(list[low]<point)
                low++;
//            while(list[high]<point && low<=high)
            while(list[high]>point)
                high--;
            if(high > low){
                int t = list[low];
                list[low] = list[high];
                list[high] = t;
            }
        }
        // high < low 的时候  要进行基准点的确定了
        // 如果出现了 high点 大于基准点的  不行
        while (high > first&& list[high] > point){
            high--;
        }

        //  将 基准点与 比基准点小的(high)点进行 交换  这样 大 小 就分家了
        if(high > first && list[high] <= point){
            int t = list[first];
            list[first] = list[high];
            list[high] = t;
            return high;
        }else{
            return first;
        }
    }
}

修改的快速排序

将基准点设为 first last mid 的中位数

但是注意分割点没有变化 还是在中间的point

就直接将中位数 交换到first的位置就行了


import java.util.Arrays;
import java.util.Random;

public class GQuickSort23_4 {
    public static void main(String[] args) {
        Random input = new Random();
        int[] list = new int[10];
        int i = 0;
        while (i < list.length) {
            list[i] = input.nextInt(100);
            i++;
        }
        System.out.println(Arrays.toString(list));
        quickSort(list);
        System.out.println(Arrays.toString(list));

    }
    // 利用 第一个 中间元素  最后一个 的 中位数 作为基准元素

    public static void quickSort(int[] list) {
        quickSort(list, 0, list.length - 1);
    }


    public static void quickSort(int[] list, int first, int last) {
        if (last > first) {
            int pointIndex = partition(list, first, last);
            quickSort(list, first, pointIndex - 1);
            quickSort(list, pointIndex + 1, last);
        }
    }

    // 只有在这里返回 中间元素 然后在 这里定义 基准元素
    // 在这里直接就将 中间的 交换到 first就行了
    public static int partition(int[] list, int first, int last) {
        int mid = (first+last)/2;


        if((list[first] > list[last] && list[first] < list[mid]) || (list[first] > list[mid] && list[first] < list[last])){
        }else if((list[mid] > list[last] && list[mid] < list[first]) || (list[mid] > list[first] && list[mid] < list[last])){
            int temp = list[mid];
            list[mid] = list[first];
            list[first] = temp;
        }else{
            int temp = list[last];
            list[last] = list[first];
            list[first] = temp;
        }


        int point = list[first];
        int low = first + 1;
        int high = last;
        while (low < high) {

            while (list[low] < point)
                low++;
            while (list[high] > point)
                high--;

            if (high > low) {
                int temp = list[low];
                list[low] = list[high];
                list[high] = temp;
            }
        }

        while (high > first && list[high] > point)
            high--;

        if (high > first && list[high] <= point) {
            int t = list[first];
            list[first] = list[high];
            list[high] = t;
            return high;
        } else {
            return first;
        }
    }
}

堆(大)

小堆一样的就只需要改变几个符号就行


import java.util.ArrayList;
import java.util.List;

// 堆就是 链表  也是二叉树
public class Heap {
    // 堆用链表来存  可以随时添加
    private ArrayList<Integer> list = new ArrayList<>();
    // 注意: 父节点 (i-1)/2  左子节点 2i+1  右子节点 2i+2
    // 用的是完全二叉树

    // 构造函数
    public Heap(int[] list) {
        for (int e : list)
            add(e);
    }

    public Heap(List<Integer> list) {
        for (Integer e : list)
            add(e);
    }

    // 堆中的元素 添加
    // 首先 是将元素添加进 链表的最后
    // 然后 是对其进行位置的检索 排到正确的位置
    public void add(Integer e) {
        list.add(e);
        int currentIndex = list.size() - 1;
        while (currentIndex > 0) {
            int parentIndex = (currentIndex - 1) / 2;
            if (list.get(parentIndex) < list.get(currentIndex)) {
                int temp = list.get(currentIndex);
                list.set(currentIndex, list.get(parentIndex));
                list.set(parentIndex, temp);
                currentIndex = parentIndex;
            } else
                break;
        }
    }

    // 一直 remove 最上面的节点 后保存下来就是 排好了序的
    // 存的就 不在这里了 当然也可也写一个sort方法
    public Integer remove() {
//        System.out.println(list.toString());
        if (list.size() <= 0)
            return null;

        // 用最后一个节点替换根节点
        // 让根节点成为当前节点
        // while(当前节点具有子节点并且当前节点小于它的子节点){
        //  将当前节点和它的较大子节点交换
        //  现在当前节点往下面退了一个层次
        // }
        // 首先我们可以将 第一个直接赋值给 最后一个 然后 remove它
        // 再最后来进行排序
        Integer result = list.get(0);
        int currentIndex = 0;
        Integer temp = list.get(currentIndex);
        list.set(0, list.get(list.size() - 1));
        list.set(list.size() - 1, temp);
        list.remove(list.size() - 1);

        while (currentIndex < list.size()) {
            int leftChildIndex = 2 * currentIndex + 1;
            int rightChildIndex = 2 * currentIndex + 2;
            int childIndex;

            // Find the maximum between two children
            if (leftChildIndex >= list.size()) break;
            else if (rightChildIndex == list.size())
                childIndex = leftChildIndex;
            else
                childIndex = list.get(leftChildIndex) > list.get(rightChildIndex) ? leftChildIndex : rightChildIndex;

            if (list.get(childIndex) > list.get(currentIndex)) {
                Integer temp1 = list.get(currentIndex);
                list.set(currentIndex, list.get(childIndex));
                list.set(childIndex, temp1);
                currentIndex = childIndex;
            } else {
                break;
            }
        }
        return result;
    }

    public int size() {
        return list.size();
    }
}

桶排序 (基数排序)


import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;

public class tryBuckets {
    public static void main(String[] args) {
        Random input = new Random();
        int[] list = new int[10];
        for (int i = 0; i < 10; i++) {
            list[i] = input.nextInt(100);
        }
        ArrayList<Integer>[] buckets = new ArrayList[list.length];
        int geShu = 0;
        for (int i = 0; i < 10; i++) {
            buckets[i] = new ArrayList<>();
        }

        // 这里循环次数取决于 最大元素的位数 100 三次
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < list.length; j++) {
                // 根据key 来放入元素
                int key = list[j] / (int) Math.pow(10, i) % 10;
                buckets[key].add(list[j]);
            }
            // 将元素放回到原来list中 相当于 对list中的元素进行排序
            geShu = 0;  //
            for (int h = 0; h < buckets.length; h++) {
                int k = 0; // 就用这个来进行放置
                if (buckets[h] != null) {
                    while (buckets[h].size() > 0) {   // 利用h位置的元素个数只要大于0 就拿出来
                        list[geShu++] = buckets[h].get(k);
                        buckets[h].remove(k);
                    }
                }
            }
        }
        System.out.println(Arrays.toString(list));
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

你这个年纪你是怎么睡得着的

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值