使用枢轴的中值快速排序

发布于 2025-01-10 00:42:49 字数 2567 浏览 4 评论 0原文

我有一个关于我的快速排序算法的问题,它在小数组上运行,但是当数组变大时程序停止运行,基本上我只是想找到中值,所以我不断分割数组以丢弃我正在处理的值不担心,所以我将使用三个值来找到最佳枢轴并获取该索引,然后运行快速排序,以便两侧的值小于或大于枢轴,然后检查枢轴索引以及中值应该在哪里例如,如果我有 20索引数组中值应为 10, n 如果我的主元位于索引 7 我从索引 7 到 20 开始一个新的快速排序,n 继续递归运行它直到主元索引位于中值

    // median of three
    private static int getPivot(int[] nums, int left, int right) {
        int[] triplet = new int[] {nums[left], nums[(left + right) / 2], nums[right]};
        Arrays.sort(triplet);
//             pivotIndex is either left, right, or middle
            if(triplet[1] == nums[left]){
                pivotIndex = left;
            }
            else if(triplet[1] == nums[(left + right) / 2]){
                pivotIndex = (left + right) / 2;
            }
            else if(triplet[1] == nums[right]){
                pivotIndex = right;
            }

        return triplet[1];
    }

    // swap two values
    private static void swap(int[] nums, int left, int right) {
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }

/// 这是快速排序

块引用

     static int median;
   static int pivotIndex;

    public static double findMedian(int[] nums) {
        median = nums.length / 2;
        quicksort(nums, 0, nums.length - 1);
        return (nums[median]);
    }

    private static void quicksort(int[] nums, int left, int right) {

        if (left >= right)
        return;
        int fromLeft = left;
        int fromRight = right;
        int pivot = getPivot(nums, left, right);
        swap(nums, pivotIndex, right);
        pivotIndex = right;
        fromRight--;
        while(fromLeft < fromRight){
            if ( (nums[fromLeft] > pivot) && (nums[fromRight] < pivot)) {
                swap(nums, fromLeft, fromRight);
                fromLeft++;
                fromRight--;
            }
            while (nums[fromLeft] < pivot){
                fromLeft++;
            }
            while (nums[fromRight] > pivot){
                fromRight--;
            }
            if( fromLeft > fromRight){
                pivotIndex = fromLeft;
                swap(nums, fromLeft, right);
            }
        }
        if(pivotIndex == median){
            return;
        }
        if(pivotIndex > median){
            quicksort(nums, left, pivotIndex);
        }
        if(pivotIndex < median){
            quicksort(nums, pivotIndex, right);
        }

da 问题是当数字变得足够大时只是说应用程序正在运行并且不执行任何操作,我不明白为什么,即使它花了 n^2 它最终应该显示一个答案

i have a question regarding my quicksort algorithm, it runs on small arrays but when the arrays get large the program quits running, basically I'm just trying to find the median value, so i keep splitting the array to discard values that I'm not worried about, so I'll use three values to find the best pivot and get that index, then i run quicksort so the values on either side are less or greater than pivot, and then check the pivot index with where the median value should be, for example if i have a 20 index array the median value should be 10, n if my pivot is at index 7 i start a new quicksort from index 7 to 20, n keep recursively running it until the pivotindex is at the median

    // median of three
    private static int getPivot(int[] nums, int left, int right) {
        int[] triplet = new int[] {nums[left], nums[(left + right) / 2], nums[right]};
        Arrays.sort(triplet);
//             pivotIndex is either left, right, or middle
            if(triplet[1] == nums[left]){
                pivotIndex = left;
            }
            else if(triplet[1] == nums[(left + right) / 2]){
                pivotIndex = (left + right) / 2;
            }
            else if(triplet[1] == nums[right]){
                pivotIndex = right;
            }

        return triplet[1];
    }

    // swap two values
    private static void swap(int[] nums, int left, int right) {
        int temp = nums[left];
        nums[left] = nums[right];
        nums[right] = temp;
    }

/// this is the quick sort

Blockquote

     static int median;
   static int pivotIndex;

    public static double findMedian(int[] nums) {
        median = nums.length / 2;
        quicksort(nums, 0, nums.length - 1);
        return (nums[median]);
    }

    private static void quicksort(int[] nums, int left, int right) {

        if (left >= right)
        return;
        int fromLeft = left;
        int fromRight = right;
        int pivot = getPivot(nums, left, right);
        swap(nums, pivotIndex, right);
        pivotIndex = right;
        fromRight--;
        while(fromLeft < fromRight){
            if ( (nums[fromLeft] > pivot) && (nums[fromRight] < pivot)) {
                swap(nums, fromLeft, fromRight);
                fromLeft++;
                fromRight--;
            }
            while (nums[fromLeft] < pivot){
                fromLeft++;
            }
            while (nums[fromRight] > pivot){
                fromRight--;
            }
            if( fromLeft > fromRight){
                pivotIndex = fromLeft;
                swap(nums, fromLeft, right);
            }
        }
        if(pivotIndex == median){
            return;
        }
        if(pivotIndex > median){
            quicksort(nums, left, pivotIndex);
        }
        if(pivotIndex < median){
            quicksort(nums, pivotIndex, right);
        }

da problem i am having is when the number gets big enough is just says the application is running and doesn't do anything and i can't figure out why, even if it took n^2 it should eventually display an anser

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文