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 answers total)
 
Not seeing any comments in there.

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, 2009


Is there some code missing here? Might want to post the code somewhere else; I think it's been mutilated by MeFi's HTML filters. (Unless the for loop really looks like that, in which case: <> is not an operator in Java; use !=.)
posted by equalpants at 4:14 PM on November 5, 2009


This code has a number of problems. For example:

1) "Not equals" in java is !=, not <>.

2) Also, a for() loop requires three arguments, separated by semicolons. You can leave them blank. For example, for(;i<1>
3) The function partition() is not defined.

It almost looks like this code was copied from another language and an attempt was made to convert into java's syntax.
posted by null terminated at 4:15 PM on November 5, 2009


Also, like equalpants suggests above, try pasting it into pastebin.com and linking to it from here.
posted by null terminated at 4:16 PM on November 5, 2009


If you're a programmer, just use the system qsort or the collection's sort() method.

If you're a student, then do your own homework.
posted by cmiller at 6:07 PM on November 5, 2009 [4 favorites]


Try posting on Stack Overflow. It's a Q&A site for programmers: very easy to post code there and have it formatted correctly, and you'll get multiple good answers within 10 minutes.
posted by Khalad at 7:20 PM on November 5, 2009


You'll get multiple answers, but you aren't guaranteed that they'll be good.
posted by secret about box at 12:47 AM on November 6, 2009


You need {
    to wrap (your code)
    with <pre> tags && escape(your text) {
        especially the less-than sign (<) == &lt;
        && the greater-than sign (>) == &gt;
    }
} else {
    you get crappy && unreadable code;
}

posted by Civil_Disobedient at 2:20 AM on November 6, 2009 [1 favorite]


I saw the exact same thing posted on StackOverflow several times yesterday. More appropriate there but you are still trying to get other people to do your homework.
posted by zemblamatic at 3:55 AM on November 6, 2009


« Older Bourbon, Afghanistan, Death?   |   Help me re-find this irreverent filmmaking... Newer »
This thread is closed to new comments.