[英]Quicksort array is not printing out correctly? (java)
I'm currently working on a university assignment which involves implementing sorting algorithms. 我目前正在从事大学作业,其中涉及实施排序算法。 I believe I have correctly implemented the quicksort algorithm, however in the test class the method just prints out the array being read in without sorting it.
我相信我已经正确实现了quicksort算法,但是在测试类中,该方法只是打印出要读取的数组,而没有对其进行排序。 Below is the code from the test class, and the code for the actual quicksort(which is in a seperate class called 'sort').
下面是来自测试类的代码,以及实际quicksort的代码(位于单独的类“ sort”中)。 Has anyone got any idea what I'm doing wrong?
有人知道我在做什么错吗?
import java.io.*;
import java.text.*;
import java.util.*;
public class Sort {
/** Array of integers to sort **/
private int[] A;
/** Size of the array **/
private int size;
/** Number of elements actually used in array **/
private int usedSize;
/** Global variables for counting sort comparisons **/
public int compIS;
/** Global comparison count for Insertion Sort **/
public int compQS;
/** Global comparison count for Quicksort **/
public int compNewS;
/** Global comparison count for new sort **/
/*****************/
/** Constructor **/
/*****************/
Sort(int max) {
/** Initialiase global sort count variables **/
compIS = 0;
compQS = 0;
compNewS = 0;
/** Initialise size variables **/
usedSize = 0;
size = max;
/** Create Array of Integers **/
A = new int[size];
}
public int getRightElement() {
return usedSize - 1;
}
public int getLeftElement() {
return A[0];
}
/*********************************************/
/*** Read a file of integers into an array ***/
/*********************************************/
public void readIn(String file) {
try {
/** Initialise loop variable **/
usedSize = 0;
/** Set up file for reading **/
FileReader reader = new FileReader(file);
Scanner in = new Scanner(reader);
/** Loop round reading in data while array not full **/
while (in.hasNextInt() && (usedSize < size)) {
A[usedSize] = in.nextInt();
usedSize++;
}
} catch (IOException e) {
System.out.println("Error processing file " + file);
}
}
/**********************/
/*** Display array ***/
/**********************/
public void display(int line, String header) {
/*** Integer Formatter - three digits ***/
NumberFormat FI = NumberFormat.getInstance();
FI.setMinimumIntegerDigits(3);
/** Print header string **/
System.out.print("\n" + header);
/** Display array data **/
for (int i = 0; i < usedSize; i++) {
/** Check if new line is needed **/
if (i % line == 0) {
System.out.println();
}
/**
* Display an ar ray element
**/
System.out.print(FI.format(A[i]) + " ");
}
}
public void quick(int L, int R) {
/* ensure there is more than one element in array */
if (R > L) {
/* split array in two */
int pLoc = partition(L, R);
/* sort left half */
quick(L, pLoc - 1);
/* sort right half */
quick(pLoc + 1, R);
}
System.out.println("\n\nAfter QuickSort: ");
for (int i = 0; i < usedSize; i++) {
System.out.println(A[i] + " ");
}
}
/* partitions array for quicksort */
public int partition(int L, int R) {
/* Select pivot */
int pivot = A[R];
/* initialise scanning pointers */
int pR = R;
int pL = L;
/* repeat until pointers cross */
while (pL < pR) {
compQS++;
/* move left pointer */
while (A[pL] < pivot) {
pL++;
}
/* move right pointer */
while ((A[pR] >= pivot) && (pR > L)) {
pR--;
//compQS++;
}
/* swap elements */
//compQS++;
if (pL < pR) {
swap(pL, pR);
L++;
R--;
}
}
/* put pivot in correct position */
swap(pL, R);
/* return pivot position */
return pL;
}
/* swaps elements in quicksort */
public void swap(int i, int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
public class TestSort
{
public static void main(String[] args)
{
Sort sortTest = new Sort(100);
/** Read in test data into array **/
sortTest.readIn("test1.txt");
/** Display array **/
sortTest.display(10,"Input Array 1");
/*apply insertion sort to array*/
//sortTest.insertion();
//sortTest.readIn("test1.txt");
sortTest.quick(sortTest.getLeftElement(), sortTest.getRightElement());
sortTest.newSort();
/** Display comparison counters **/
System.out.println("Quicksort comparison counter: " + sortTest.compQS);
System.out.println("\n\nInsertion sort comparison counter: " + sortTest.compIS);
}
One of your problems is that getLeftElement()
is returning the value at position 0
within your array instead of just returning 0
. 您的问题之一是
getLeftElement()
返回数组中位置0
的值,而不仅仅是返回0
。 In situations in which the value of the first element in the array is greater than the size of the array, no sorting will be done. 在数组中第一个元素的值大于数组大小的情况下,将不进行排序。
Also, I believe that your implementation of the method quick
is incorrect. 另外,我认为您
quick
实现该方法是不正确的。 Within the method you recursively invoke quick(L, pLoc - 1)
and quick(pLoc + 1, R)
. 在该方法中,您递归调用
quick(L, pLoc - 1)
和quick(pLoc + 1, R)
。 By invoking in this manner, you do not traverse all indices of the array in your array. 通过以这种方式调用,您不会遍历数组中数组的所有索引。 (Ex if
L
is 0
, R
is 10
, and pLoc
is 5
, then you do not involve the index 5 in the sorting of your array.) (例如,如果
L
为0
, R
为10
且pLoc
为5
,则在数组排序中不涉及索引5。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.