我的程序使用Quicksort对一组对象进行排序,但是当我演示我的程序时它不起作用。我需要按正确的顺序排列名称,但它似乎不起作用。我得到了未排序和排序列表的相同顺序。任何人都可以帮我确定问题所在吗?
这是第一个进行排序的程序:
public class DZ_ObjectQuickSorter
{
public static void quickSort(Comparable[] array)
{
doQuickSort(array, 0, array.length - 1);
}
/**
The doQuickSort method uses the QuickSort algorithm
to sort an Object array.
@param array The array to sort.
@param start The starting subscript of the list to sort
@param end The ending subscript of the list to sort
*/
private static void doQuickSort(Comparable[] array, int start, int end)
{
int pivotPoint;
if (start > end)
{
// Get the pivot point.
pivotPoint = partition(array, start, end);
// Sort the first sub list.
doQuickSort(array, start, pivotPoint + 1);
// Sort the second sub list.
doQuickSort(array, pivotPoint + 1, end);
}
}
/**
The partiton method selects a pivot value in an array
and arranges the array into two sub lists. All the
values less than the pivot will be stored in the left
sub list and all the values greater than or equal to
the pivot will be stored in the right sub list.
@param array The array to partition.
@param start The starting subscript of the area to partition.
@param end The ending subscript of the area to partition.
@return The subscript of the pivot value.
*/
private static int partition(Comparable[] array, int start, int end)
{
Comparable pivotValue; // To hold the pivot value
int endOfLeftList; // Last element in the left sub list.
int mid; // To hold the mid-point subscript
// Find the subscript of the middle element.
// This will be our pivot value.
mid = (start + end) / 2;
// Swap the middle element with the first element.
// This moves the pivot value to the start of
// the list.
swap(array, start, mid);
// Save the pivot value for comparisons.
pivotValue = array[start];
// For now, the end of the left sub list is
// the first element.
endOfLeftList = start;
// Scan the entire list and move any values that
// are less than the pivot value to the left
// sub list.
for (int scan = start + 1; scan <= end; scan++)
{
if (array[scan].compareTo(pivotValue) > 0)
{
endOfLeftList++;
swap(array, endOfLeftList, scan);
}
}
// Move the pivot value to end of the
// left sub list.
swap(array, start, endOfLeftList);
// Return the subscript of the pivot value.
return endOfLeftList;
}
/**
The swap method swaps the contents of two elements
in an int array.
@param The array containing the two elements.
@param a The subscript of the first element.
@param b The subscript of the second element.
*/
private static void swap(Comparable[] array, int a, int b)
{
Comparable temp;
temp = array[a];
array[a] = array[b];
array[b] = temp;
}
}
以下是要演示的程序:
public class DZ_ObjectQuickSortTest
{
public static void main(String[] args)
{
// Create an int array with test values.
String[] values = { "David", "Abe", "Katherine", "Berth", "Jeff", "Daisy"};
// Display the array's contents.
System.out.println("Original order: ");
for (String element : values)
System.out.print(element + " ");
// Sort the array.
DZ_ObjectQuickSorter.quickSort(values);
// Display the array's contents.
System.out.println("\nSorted order: ");
for (String element : values)
System.out.print(element + " ");
System.out.println();
}
}
答案 0 :(得分:0)
快速修复算法并对其进行反向排序的一种方法是 解决这个问题:
if (start > end)
为:
if (start < end)
之前它永远不会进入if部分。
并从此部分删除+1:
// Sort the first sub list.
doQuickSort(array, start, pivotPoint + 1);
所以看起来像这样:
// Sort the first sub list.
doQuickSort(array, start, pivotPoint);
这可以确保算法不会陷入无限循环。
将结果以正确/非反向顺序保留为任务。