Java 快速排序算法所需的枢轴开关数量与作业中的枢轴开关数量不同的原因是什么?
因此,显然,在某些情况下,我的算法需要较少的枢轴更改来完成列表的排序。实际上,我的算法对列表进行了正确排序,但支点的数量少于或等于我给出的示例。 我的作业中给出的一个示例是以下数组: 34512-34-3210620 输出应该是这样的: 枢轴数:7个Java 快速排序算法所需的枢轴开关数量与作业中的枢轴开关数量不同的原因是什么?,java,algorithm,sorting,quicksort,Java,Algorithm,Sorting,Quicksort,因此,显然,在某些情况下,我的算法需要较少的枢轴更改来完成列表的排序。实际上,我的算法对列表进行了正确排序,但支点的数量少于或等于我给出的示例。 我的作业中给出的一个示例是以下数组: 34512-34-3210620 输出应该是这样的: 枢轴数:7个 第一个元素:-3 最后要素:45 这就是我得到的: 枢轴数:5个 第一个元素:-3 最后要素:45 在另一个示例中,它使用正确数量的枢轴: 9247371011213131013 我应该得到的以及我得到的: 枢轴数:10 第一个元素:2 最后一部分
第一个元素:-3
最后要素:45 这就是我得到的: 枢轴数:5个
第一个元素:-3
最后要素:45 在另一个示例中,它使用正确数量的枢轴: 9247371011213131013 我应该得到的以及我得到的: 枢轴数: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;
}
公共静态无效快速排序(int[]arr,int start,int end,CountObject count){
int partition=分区(arr、start、end、count);
//分区将返回枢轴的位置。枢轴将位于正确的位置,因此如果任一侧
//透视图中的元素仅包含一个元素,不应对其进行排序
//检查从枢轴上留下的零件是否仍应分类
如果(分区1>启动){
快速排序(arr、开始、分区-1、计数);
}
//检查枢轴右侧的零件是否仍应排序
if(分区+1开始;i--){
if(arr[i]>枢轴){
int temp=arr[end];
arr[end]=arr[i];
arr[i]=温度;
结束--;
}
}
int temp=arr[start];//=pivot
arr[start]=arr[end];
arr[end]=温度;
返回端;
}
}
我正在使用CountObject类进行计数。它包含一个方法increaseCount和一个实例变量count 所以我终于明白了。我不得不使用另一种技术遍历列表。在我的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;
}
}
import java.util.Scanner;
公务舱快艇{
公共静态void main(字符串[]args){
扫描仪sc=新的扫描仪(System.in);
内部温度;
int size=sc.nextInt();
int[]列表=新的int[大小];
对于(int i=0;i启动){
快速排序(arr、开始、分区1、计数);
}
if(分区+1)