排序: 快速排序 + 冒泡排序 + 插入排序 + 选择排序

排序大纲分类

在这里插入图片描述

In-place sort algorithms 就地排序
就是 不开辟额外的内存空间 进行排序。

stability difficulty 稳定性
相同元素的排序不稳定。

快速排序

找到 中间点
把数组 分为 两个部分

小于 P 的部分
大于 等于 p 的部分

在这里插入图片描述

快速排序的理想状态

选出一个数, 左边 和 右边 的 数的 数量 差不多。
尽可能 保证两边的 均分。


import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


public class Solution {

    public void sortIntegers(int[] A) {
        if (A == null || A.length == 0) {
            return;
        }

        quickSort(A, 0, A.length - 1);
    }

    private void quickSort(int[] A, int start, int end) {
        if (start >= end) {
            return;
        }

        int left = start, right = end;

        // pivot 不能选 首, 尾
        // get value not index
        int pivot = A[left + (right - left) / 2];

        // left <= right not left < right
        while (left <= right) {

            // A[left] < pivot not <=
            while (left <= right && A[left] < pivot) {
                left++;
            }

            while (left <= right && A[right] > pivot) {
                right--;
            }

            if (left <= right) {
                int temp = A[left];
                A[left] = A[right];
                A[right] = temp;
                left++;
                right--;
            }
        }

        quickSort(A,start,right);
        quickSort(A,left,end);
    }


    public static void main(String[] args) {
        int[] t1 = {5,5,3,2,4,8,63,1,0,4,8};
        Solution s1 = new Solution();
        s1.sortIntegers(t1);
        for (int i : t1) {
            System.out.print(i + " ");
        }
    }
}

归并排序 mergeSort

public class Solution {

    public void sortIntegers(int[] A) {
        if (A == null || A.length == 0) {
            return;
        }

        int[] temp = new int[A.length];

        mergeSort(A, 0, A.length - 1, temp);
    }

    private void mergeSort(int[] A, int start, int end, int[] temp) {
        if (start >= end) return;

        int pivot = start + (end - start) / 2;

        mergeSort(A, start, pivot, temp);
        mergeSort(A, pivot + 1, end, temp);
        merge(A, start, end, temp);
    }

    private void merge(int[] A, int start, int end, int[] temp) {
        int leftStart = start;
        int middle = start + (end - start) / 2;
        int rightStart = middle + 1;

        int index = start;

        while (leftStart <= middle && rightStart <= end) {
            if (A[leftStart] <= A[rightStart]) {
                temp[index++] = A[leftStart++];
            } else {
                temp[index++] = A[rightStart++];
            }
        }

        while (leftStart <= middle) {
            temp[index++] = A[leftStart++];
        }


        while (rightStart <= end) {
            temp[index++] = A[rightStart++];
        }


        for (int i = start; i <= end; i++) {
            A[i] = temp[i];
        }

    }


    public static void main(String[] args) {
//        int[] t1 = {5,5,3,2,4,8,63,1,0,4,8};
        int[] t1 = {5,56,45,5,5,5,5,8,8,8,6456,5,66,5,4,123,12,312,3,12};
        Solution s1 = new Solution();
        s1.sortIntegers(t1);
        for (int i : t1) {
            System.out.print(i + " ");
        }
    }
}

代码

选择排序

public class Solution {
    /**
     * @param a: an integer array
     * @return: nothing
     */
    public void sortIntegers(int[] a) {
        // write your code here

        int n = a.length;

        for (int i = 0; i < n - 1; i++){
            int min = i;
            for (int j = i + 1; j < n; j++){
                if (a[j] < a[min]){
                    min = j;
                }
            }
            swap(a, i, min);
        }
    }
     public void swap(int[] a, int index1, int index2){
         int temp = a[index1];
         a[index1] = a[index2];
         a[index2] = temp;
     }
}

冒泡排序

// bubble sort
 public class Solution{
     public void sortIntegers(int[] a){
         int n = a.length;

        for (int i = 0; i < n- 1;i++){
             for (int j = 0; j < n - 1 - i; j++){
              if (a[j] > a[j+1]) swap(a, j, j+1);
             }
         }
     }

     public void swap(int[] a, int index1, int index2){
         int temp = a[index1];
         a[index1] = a[index2];
         a[index2] = temp;
     }
 }

快速排序

public class Solution{
    public void sortIntegers(int[] a) {
        int low = 0;
        int hight = a.length - 1;
        quickSort(a, low, hight);
    }

    public void quickSort(int[] a, int low, int hight){
        
        if (low > hight) return;

        int left, right, pivot;
        
        left = low;
        right = hight;
        pivot = a[left];

        while(left < right){
            while(left < right && a[right] >= pivot) right--;

            if (left < right) a[left] = a[right];

            while(left < right && a[left] <= pivot) left++;

            if (left < right) a[right] = a[left];

            if (left == right) a[left] = pivot;
        }

        quickSort(a, low, right - 1);
        quickSort(a, right + 1, hight);
    }
}

快速排序 的视频 一定看视频,不要看代码,直接看代码 不容易理解
马士兵什么的,没有这个讲的简单易懂,看这个

插入排序

public class Solution{
    public void sortIntegers(int[] a){
        int n = a.length;
        for (int i = 1; i < n; i++){
            int temp = a[i];
            int j = i;

            while (j > 0 && a[j - 1] > temp){
                a[j] = a[j - 1]; 
                j--;
            }

            a[j] = temp;
        }
    }
}

拓扑排序 Topological sort

偏序排序

初始化 每个点 的 indegree

https://www.bilibili.com/video/BV1at411T75o?spm_id_from=333.337.search-card.all.click&vd_source=8d8fef6cad2875d6b6b4c08c3a9ac66d

二分搜索 Binary Search

按值进行二分搜索

模版1

        // left = left bound with f(left) = false
        // right = right bound with f(right) = true
        while (left < right - 1) {
            mid = (left + right) / 2;
            if (f[mid] == false) left = mid;
            else if (f[mid] == true) right = mid;
        }
        
        return left;

这个模版适用于 division problems
在这里插入图片描述

模版2

先 假设 一个结果
小于等于 这个结果 的 有多少个
如果 比 k 多 就 减少结果
如果 比 k 少 就 增加

在这里插入图片描述

搞清楚每种数据结构都是为什么服务的

一个问题可以使用 不同的data structure 进行解决。

data structure 本身就是一个工具。
拧螺丝 可以 用 手钳子 可以 用手,可以用扳手。

了解不同的数据结构,他们的复杂度 和 应用。

以便于在遇到问题的时候,可以灵活的使用。

不同数据结构的结合

嵌套结构

映射结构

线性结构 linear data structure

在这里插入图片描述
array
queue
stack
linked list 插入
double linked list

heap 和 二分搜索 —— binary heap

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值