Reputation: 15
So I'm making a simple sort program but the sort methods return 0s rather than the numbers of the array being sorted. Perhaps someone could shed a bit of light as to why this is happening. The numbers themselves are taken from a .txt file and then sorted in numerical order.
import java.io.File;
import java.util.Scanner;
public class quickSort{
public static void main(String[] args) throws Exception {
File infile = new File("input.txt");
Scanner input = new Scanner(infile);
int[] data = new int[100];
int x, count;
count = 0;
while (input.hasNext()){
x = input.nextInt();
data[count++] = x;
}
System.out.println("Array before Sort");
for (int i = 0; i < count; i++){
System.out.printf(" %d", data[i]);
if ((i+1)%7==0) System.out.println();
}
quicksort(data, count);
System.out.println("\n\nArray after quickSort");
for (int i = 0; i < count; i++){
System.out.printf(" %d", data[i]);
if ((i + 1)%7==0) System.out.println();
}
System.out.println();
}
public static void quicksort(int[] data, int count) {
quicksortHelper(data, 100, data.length - 1);
}
Changing the code between 0 and 100 above this line modifies the output a bit.
protected static void quicksortHelper(int[] data, int bottom, int top){
if (bottom < top) {
int midpoint = partition(data,bottom,top);
quicksortHelper(data, bottom, midpoint -1 );
quicksortHelper(data, midpoint + 1, top);
}
}
protected static int partition(int[] data, int bottom, int top){
int pivot = data[top];
int firstAfterSmall = bottom;
for (int i = bottom ; i < top; i++){
if (data[i] <= pivot) {
swap(data, firstAfterSmall, i);
firstAfterSmall++;
}
}
swap(data, firstAfterSmall, top);
return firstAfterSmall;
}
protected static void swap(int[] data, int i, int j){
int temp = data[i];
data[i] = data[j];
data[j] = temp;
}
}
Any help is appreciated, my brain is about to explode messing around with this.
Sample output: Array before sort 1 5 3 2 9 10 100 4 6 5
array after quicksort 1 6 3 2 9 10 100 4 6 5
If the numbers mentioned above are changed to 0 then the "array after quicksort" is printed in all 0s, like such
array after quicksort 0 0 0 0 0 0 0 0 0 0
Upvotes: 0
Views: 91
Reputation: 3694
You could use Arrays.sort(data)
to sort instead of having all that messy code :)
Upvotes: 0
Reputation: 7624
In your quicksort
method, you call quicksortHelper
with the arguments (100, data.length - 1). This will sort the last element of the array, which is not that helpfull. Change it to :
quicksortHelper(data, 0, count);
In terms of always printing 0, without futher insight, can't really help beyond checking the data in input.txt
and making sure it has exactly 100 ints in it etc, otherwise your array values are not being initialised.
Although I assume this is for an assignment, rather than reinvent the wheel, you can always call Arrays.sort to accomplish this task in the future.
Edit:
The second argument should be count
rather than data.length - 1
, else, you are sorting the array which will contain ~ 90 0's in in then only printing out the first 10, which after sorting the entire thing will all be 0's. Thus, you only want to sort the first count
items.
Upvotes: 1