为了账号安全,请及时绑定邮箱和手机立即绑定

我的快速排序算法所需的透视切换次数与我的分配中的透视切换次数不同的原因是什么?

我的快速排序算法所需的透视切换次数与我的分配中的透视切换次数不同的原因是什么?

鸿蒙传说 2022-08-17 10:39:34
因此,显然在某些情况下,我的算法需要较少的透视更改来完成列表的排序。实际上,我的算法确实对列表进行了正确的排序,但数据透视表的数量小于或等于我所给出的示例。我的赋值中给出的一个例子是这个数组:3 45 12 -3 4 -3 21 0 6 20输出应该是这样的:枢轴数: 7第一个元素: -3最后一个元素: 45这是我得到的:枢轴数: 5第一个元素: -3最后一个元素: 45在另一个例子中,它适用于正确数量的枢轴:9 2 4 7 3 7 10 11 12 13 13 10 13 13我应该得到什么,也得到什么:枢轴数量: 10第一个元素: 2最后一个元素: 13我特别困惑的是,它在某些情况下有效,而在其他情况下则不起作用。代码如下:public static void quickSort(int[] arr, int start, int end, CountObject count){    int partition = partition(arr, start, end, count);    //partition will return the position the pivot. The pivot will be at the right place, hence if either side    //of the pivot consists of only one element, it should not be sorted    //check whether the part left from the pivot should still be sorted   if(partition-1>start) {        quickSort(arr, start, partition - 1, count);    }    //check whether the part right from the pivot should still be sorted    if(partition+1<end) {        quickSort(arr, partition + 1, end, count);    }}public static int partition(int[] arr, int start, int end, CountObject count){    int pivot = arr[start];    count.increaseCount();    //checks if left pointer < pivot    for(int i=end; i>start; i--){        if(arr[i]>pivot){            int temp= arr[end];            arr[end]=arr[i];            arr[i]=temp;            end--;        }    }    int temp = arr[start];//=pivot    arr[start] = arr[end];    arr[end] = temp;    return end;}}我正在使用 CountObject 类进行计数。它包含一个方法增加计数和一个实例变量计数。
查看完整描述

1 回答

?
白衣非少年

TA贡献1155条经验 获得超0个赞

所以我终于想通了。我不得不使用另一种技术来遍历列表。在我的OP中,我使用第一个元素作为枢轴,并将其与列表末尾开始的所有元素进行比较。现在我从列表的第二个元素/当前子列表开始。


这是解决我问题的代码,我希望这将使某人节省2天的工作,尽管我自己做这件事很有教育意义。


import java.util.Scanner;


public class Quickie {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int temp;


        int size = sc.nextInt();


        int[] list = new int[size];

        for (int i = 0; i < size; i++) {

            temp = sc.nextInt();

            list[i] = temp;

        }

        int end = size - 1;

        CounterClass count = new CounterClass(0);


        quickSort(list, 0, end, count);


        int firstElement = list[0];

        int lastElement = list[size - 1];

        System.out.println("Number of pivots: " + count.getCount());

        System.out.println("First Element: " + firstElement);

        System.out.println("Last Element: " + lastElement);

    }

    private static void quickSort (int []arr, int start, int end, CounterClass count){

        int partition = partition(arr, start, end, count);


        if (partition-1>start){

            quickSort(arr, start, partition-1,count);

        }

        if (partition+1<end){

            quickSort(arr, partition+1,end,count);

        }

    }

    private static int partition (int[]arr, int start, int end, CounterClass count){

        int pivot = arr[start];


        count.count++;

        int pointer = start+1;

        int i =pointer;

        for (int j=pointer; j<=end;j++){

            if (arr[j]<pivot){

                int temp = arr[j];

                arr[j]=arr[i];

                arr[i]=temp;

                i++;

            }

        }

        i-=1;


        int temp=arr[start];

        arr[start]=arr[i];

        arr[i]=temp;

        return (i);

    }




}



class CounterClass{

    int count;

    public CounterClass(int count){

        this.count = count;

    }


    public int getCount() {

        return count;

    }

}


查看完整回答
反对 回复 2022-08-17
  • 1 回答
  • 0 关注
  • 113 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号