Java quicksort
November 5, 2009 3:50 PM
Subscribe
hey java programmers, I have a beginners quicksort problem for you.
I am trying to write some code in Java that will read in the numbers from a file (one # on each line of .txt file name inputted by user) put them into an array, and then run quick sort on the array, AND THEN print out the sorted array. This last part I'm having torouble with right now. I tried to format it below, but maybe copy and paste to Eclipse would be easier to read,
Errors I marked with comments, and what the error is, if anyone can help me get this to run, thanks everyone!
import java.io.*;
import java.io.File;
import java.util.Scanner;
public class sdfs {
public static void main(String[] args) throws IOException {
System.out.print("Name of file with array: ");
Scanner readIn = new Scanner(System.in);
String input = readIn.nextLine();
}
public static void testScan1(String filename) {
File file = new File(filename);
Scanner scan;
try {
int[] array = new int[5];
scan = new Scanner(file);
} catch (java.io.FileNotFoundException e) {
System.out.println("couldn't open. file not found ");
return;
}
while (scan.hasNext()) {
for (int i = 0; i <> pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
return i;
}
void quickSort(int[] arr, int left, int right) {
int index = partition(arr, left, right);
if (left < (index - 1)) {
;
}
quickSort(arr, left, index - 1);
if (index < right) {
quickSort(arr, index, right);
}
}
}>
posted by Benzle to technology (9 comments total)
But seriously, why not use the existing sort methods? See e.g. this random Java sorting link with examples. (trust me, there's no way you (*) can write something that's even close to the algorithms already in there :-).
*) nor your teacher, if this is homework. (insert rant about mostly pointless programming exercises here.)
posted by effbot at 4:03 PM on November 5