美文网首页
快速排序

快速排序

作者: jsjack_wang | 来源:发表于2018-04-26 16:29 被阅读0次
public class QuickSortDemo {
    public static void main(String[] args) {
        int[] array = {9, 6, 7, 6, 5, 4, 3, 2, 6, 0};
        printArray(array);
        quickSort(array);
        printArray(array);
    }

    private static void quickSort(int[] array) {
        if (Objects.isNull(array) || array.length == 1) {
            return;
        }
        arrayPartQuickSort(array, 0, array.length - 1, 0);
    }

    private static void arrayPartQuickSort(int[] array, int startIndex, int endIndex, int baseIndex) {
        if (endIndex - startIndex < 1) {
            return;
        }
        int tempStartIndex = startIndex;
        int tempEndIndex = endIndex;
        int tempBaseValue = array[baseIndex];
        int tempStartValue;
        int tempEndValue;
        while (true) {
            while(true) {
                tempEndValue = array[tempEndIndex];
                if (tempEndValue < tempBaseValue) {
                    break;
                }
                if (tempEndIndex > tempStartIndex) {
                    tempEndIndex --;
                } else {
                    break;
                }
            }
            while(true) {
                tempStartValue = array[tempStartIndex];
                if (tempStartValue > tempBaseValue) {
                    break;
                }
                if (tempStartIndex < tempEndIndex) {
                    tempStartIndex ++;
                } else {
                    break;
                }
            }
            if (tempStartIndex == tempEndIndex) {
                break;
            }
            int swapValueTemp = array[tempEndIndex];
            array[tempEndIndex] = array[tempStartIndex];
            array[tempStartIndex] = swapValueTemp;
        }
        int swapValueTemp = array[baseIndex];
        array[baseIndex] = array[tempEndIndex];
        array[tempEndIndex] = swapValueTemp;

        arrayPartQuickSort(array, startIndex, tempEndIndex - 1, startIndex);
        arrayPartQuickSort(array, tempEndIndex + 1, endIndex, tempEndIndex + 1);
    }

    private static void printArray(int[] array) {
        if (Objects.isNull(array)) {
            System.out.println(array);
        }
        for (int item : array) {
            System.out.print(item + " ");
        }
        System.out.println();
    }
}

相关文章

  • 七大排序算法之快速排序

    七大排序算法之快速排序 @(算法笔记)[排序算法, 快速排序, C++实现] [TOC] 快速排序的介绍: 快速排...

  • 面试准备--排序

    堆排序 快速排序(simple) 快速排序(regular) 归并排序 Shell排序 插入排序 选择排序 冒泡排序

  • 排序

    插入排序 选择排序 冒泡排序 归并排序 快速排序* 三路快速排序

  • 算法笔记01-排序#2

    快速排序敢叫快速排序,那它一定得快。 快速排序 概述 快速排序也是分治排序的典型,它快,而且是原地排序。不过,要防...

  • PHP 实现快速排序

    导语 这篇了解下快速排序。 快速排序 快速排序(英语:Quicksort),又称划分交换排序(partition-...

  • 快速排序的Python实现

    目录 快速排序的介绍 快速排序的Python实现 快速排序的介绍 快速排序(quick sort)的采用了分治的策...

  • 数据结构与算法 快速排序

    起因:快速排序,又称分区交换排序,简称快排,之前没有了解过,抽空学习一下。 快速排序 1 快速排序 快速排序的定义...

  • 数组-快速排序

    采用快速方式对数组进行排序 快速排序百科:快速排序(Quicksort)是对冒泡排序算法的一种改进.快速排序是通过...

  • OC数据结构&算法

    更多整理资料尽在?一平米小站 目录 选择排序 冒泡排序 插入排序 快速排序 双路快速排序 三路快速排序 堆排序 选...

  • 要成功就做一百题-94

    题目名称 今天来几个排序,都是经典题目,包括带拆分的快速排序,堆排序,归并排序。 描述 快速排序快速排序核心就是分...

网友评论

      本文标题:快速排序

      本文链接:https://www.haomeiwen.com/subject/lqlmlftx.html