快速排序-双边循环法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package sort;
 
/**
 * @author: tianhaichao
 * @date: 2022/9/15 17:51
 * @description: 快速排序,双边循环法
 * 算法思想--分治算法
 *  * 两个关键点,
 *  * 1.比较标准值  pivot 所有值与pivot比较,大的放一边,小的放一边
 *  * 2.临界点,从第一个数字开始,作为leftMark位置,同时从最后一个也开始作为rightMark位置
 *      如果leftMark的值小于pivot,leftMark++ 否则,停止等待
 *      同时进行的左边,如果rightMark的值大于pivot,rightMark -- 否则,rightMark与leftMark交换
 *      直到rightMark与leftMark重合,完成一次循环。
 *      交换rightMark=leftMark的位置与pivot的值,完成一波整理,此时pivot的值确定了
 *      同样的算法别执行【rightMark=leftMark的位置】左右部分数据的顺序
 *  * 递归执行 算法时间复杂度是O(logn)
 */
public class QuickSort2 {
    public static void quickSort(int[] array, int startIndex, int endIndex) {
        // 递归结束条件
        if (startIndex >= endIndex) {
            return;
        }
        int markIndex = partition(array,startIndex,endIndex);
        // 此时数据已经被分到两边了,然后再分别执行两边的排序
        quickSort(array,startIndex,markIndex-1);
        quickSort(array,markIndex + 1,endIndex);
    }
 
    /**
     * @author: tianhaichao
     * @date: 2022/9/15 16:12
     * @description: 将数据分成两边,mark左边的数据比pivot大,右边的数据比pivot小,返回mark下标
     */
    public static int partition(int[] array, int startIndex, int endIndex) {
        int leftIndex = startIndex;
        int rightIndex = endIndex;
        int pivot = array[startIndex];
        System.out.print("pivot:"+pivot+"   ");
        System.out.print("调整数组:");
        print(array,startIndex,endIndex);
        int temp;
        while (leftIndex < rightIndex){
            if (array[leftIndex] <= pivot && leftIndex <= endIndex){
                leftIndex ++;
            }else{
                if(array[rightIndex] >= pivot && rightIndex >= startIndex){
                    rightIndex --;
                }else{
                    // 交换左右位置
                    temp = array[leftIndex];
                    array[leftIndex] = array[rightIndex];
                    array[rightIndex] = temp;
                }
            }
        }
        // 分好边之后,交互pivot和mark的位置
        array[startIndex] = array[leftIndex];
        array[leftIndex] = pivot;
        System.out.println();
        System.out.print("结果:");
        print(array);
        System.out.println("数组被分为两部分:");
        System.out.print("数组A:");
        print(array,startIndex,leftIndex-1);
        System.out.print("  和数组B:");
        print(array,leftIndex + 1,endIndex);
        System.out.println();
        System.out.println("======================================");
        return leftIndex;
    }
 
    public static void main(String[] args) {
        int[] array = new int[]{5,2,8,6,10,9,1,4};
        QuickSort.quickSort(array,0,array.length - 1);
    }
    public static void print(int[] array){
        for (int i = 0;i<array.length;i++){
            System.out.print(" ");
            System.out.print(array[i]);
            System.out.print(" ");
        }
        System.out.println();
    }
 
    public static void print(int[] array,int start,int end){
        for (int i = start;i<=end;i++){
            System.out.print(" ");
            System.out.print(array[i]);
            System.out.print(" ");
        }
    }
}

  

posted @   田海超  阅读(54)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示