Results 1 to 13 of 13

Thread: Quicksort

  1. #1
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default Quicksort

    Hello,
    it's me again... first thank u so much for ur help with my last java stuff... it went brilliant...
    now my IT exam is in about 3 weeks and at the moment i am just revising... but the main problem is, that we get 40% of our marks through java... we have to write programms in our exam on paper... horrible...:(
    but the good thing is, that there can be only a couple of programs be asked... only the ones which we did in my lectures :-)
    but as we never got the code.. i thought about just writing some of the sorting programms so that i have some idea about it and that i am getting into it better... and they r actually quite similar :-)
    so my first one which i did was quicksort... but it is not probable working... so i was wondering if anyone of u could have a short look on it and tell me what is wrong... that would be so lovely...

    thank u so much

    little_polarbear

    oh and there is one stupid thing... i wanted to give out the sorted numbers, but it is not working... i know it is stupid... i should know by this time how to do it... but it just doesn't work :-(


    import java.io.*;

    public class quicksort
    {


    private int[] reading(String fileName)
    throws FileNotFoundException

    {
    int[] array = null;

    try
    {

    BufferedReader in = new BufferedReader(new FileReader(fileName));
    String zeile = null;
    int line = 0;



    while(in.readLine() != null)
    line++;
    in.close();

    array = new int[line];

    in = new BufferedReader(new FileReader(fileName));

    for(int v = 0; v<line; v++)
    {
    zeile = in.readLine();
    array[v] = Integer.parseInt(zeile);
    System.out.println(array[v]);
    }

    in.close();
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }
    return array;

    }


    public int [] quickSort(int[] array, int beginning, int end)
    {
    if (end> beginning){
    int pivot = beginning;
    int middle = beginning;

    for (int i=beginning+1; i<= end; i++){
    if (array[i]< array[middle]){
    middle++;
    swap(array, i, middle);
    }
    }
    swap(array, pivot, middle);

    array= quickSort(array, middle+1, end);
    array =quickSort(array, beginning, middle-1);
    System.out.println(array);

    }
    return array;
    }

    private void swap(int[] result, int a, int b){
    int temp = result[a];
    result[a] = result[b];
    result[b] = temp;
    }






    public static void main(String[] args)
    throws FileNotFoundException
    {
    quicksort app = new quicksort();
    int[] array = app.reading("fielpath");


    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    " it just doesn't work"
    Could you explain the above? Is there a compile error or a run time error or is the output different than you want?

    If you're getting errors, copy the full text and paste here.
    if you don't like the output, copy that here and explain what you want to change about it.

  3. #3
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default

    there r no errors... i just cannot give out the sorted numbers on the console :-(

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    A quick way to do that is to use the System.out.println() method.
    Have you tried that?
    What happens when you execute your program?

    Looking at the code, I don't see where you call the sort method after you've read in the file. And after sorting you need to call another method to print out the results.

  5. #5
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default

    ok, i chankged my public static void main method into

    public static void main(String[] args)
    throws FileNotFoundException
    {
    quicksort app = new quicksort();
    int[] array = app.reading("fielpath");
    app.quickSort(array, 0, 0);

    so and i used system.out.println at the end of my quickSort method...
    but it is not giving out the sorted numbers on the console...

    if i am exectuing the programm, nothing is happening and therefore i actually don't know, if it is working... that's the reason why i wanted to give out the sorted numbers so that i can see if it is woking

    little_polarbear

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    How do you know "nothing is happening"? Does that mean that there is nothing printed on the console window?
    Add enough println() so you can see where the execution is going.
    Copy the console output including the java command and post it here.

  7. #7
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default

    ok, i changed a bit in my code, and now it is completely getting mad... i put some System.out.println() stuff in it, but it is not giving out the sorted numbers and my main problem is, that i don't know where to put it exactly... i have no idea if my quicksort is actually working because i cannot see it... i am using eclipse and thought i could maybe find the error if i am using breakpoints, but sth went wrong with my destop and so i cannot see is problable...

    little_polarbear




    import java.io.*;

    public class quicksort
    {
    private static int[] quicksortarray = null;

    public quicksort(String fileName)
    throws FileNotFoundException
    {
    reading(fileName);
    }

    public void reading(String fileName)
    throws FileNotFoundException

    {
    BufferedReader in = new BufferedReader(new FileReader(fileName));
    String line = null;
    int track = 0;

    try
    {
    while(in.readLine() != null)
    track++;
    in.close();
    quicksortarray = new int[track];

    in = new BufferedReader(new FileReader(fileName));

    for(int v = 0; v<track; v++)
    {
    line = in.readLine();
    quicksortarray[v] = Integer.parseInt(line);

    }
    }

    catch (Exception e)
    {
    e.printStackTrace();
    }
    }

    private int[] quicksort(int[] quicksortarray)
    {

    int end = 0;
    int beginning = 0;


    if (end> beginning){
    int pivot = beginning;
    int mitte = beginning;

    for (int i=beginning+1; i<= end; i++){
    if (quicksortarray[i]< quicksortarray[mitte]){
    mitte++;
    swap(quicksortarray, i, mitte);
    System.out.println(quicksortarray[i]);
    }
    }
    swap(quicksortarray, pivot, mitte);

    quicksortarray= quicksort(quicksortarray);
    quicksortarray =quicksort(quicksortarray);

    }
    return quicksortarray;

    }

    private void swap(int[] result, int a, int b){
    int temp = result[a];
    result[a] = result[b];
    result[b] = temp;
    }




    public static void main(String[] args)
    throws FileNotFoundException

    {
    quicksort app = new quicksort("filepath");
    app.quicksort(quicksortarray);

    }
    }

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    I see ONE println(). Add some more!
    Add one in main() after the call: app.quicksort(...
    Also add println that put out data in the middle of your methods and loop. println("i=" + i); or println("a= " + a + ", b=" + b) in swap()
    Sorry about eclipse. IDEs are nice when you know how to use them. I'd prefer a simpler editor while a student is learning programming so that he'd understand what is happening and not have to fight the IDE.

  9. #9
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default

    ok, it is now giving out the numbers, but not sorted... i wanted to put it into the main method as well, but i didn't knw exactly what to put into the field, because i didn't initialised i into my main method... it is really strange... i used a normal text edit at the beginning but we have to knwo ecplipse for later...

    import java.io.*;

    public class quicksort
    {
    private static int[] quicksortarray = null;

    public quicksort(String fileName)
    throws FileNotFoundException
    {
    reading(fileName);
    }

    public void reading(String fileName)
    throws FileNotFoundException

    {
    BufferedReader in = new BufferedReader(new FileReader(fileName));
    String line = null;
    int track = 0;

    try
    {
    while(in.readLine() != null)
    track++;
    in.close();
    quicksortarray = new int[track];

    in = new BufferedReader(new FileReader(fileName));

    for(int v = 0; v<track; v++)
    {
    line = in.readLine();
    quicksortarray[v] = Integer.parseInt(line);
    System.out.println(quicksortarray[v]);

    }
    }

    catch (Exception e)
    {
    e.printStackTrace();
    }
    }

    private int[] quicksort(int[] quicksortarray)
    {

    int end = 0;
    int beginning = 0;


    if (end> beginning){
    int pivot = beginning;
    int mitte = beginning;

    for (int i=beginning+1; i<= end; i++){
    if (quicksortarray[i]< quicksortarray[mitte]){
    mitte++;
    swap(quicksortarray, i, mitte);
    System.out.println(quicksortarray[i]);
    }
    }
    swap(quicksortarray, pivot, mitte);

    quicksortarray= quicksort(quicksortarray);
    quicksortarray =quicksort(quicksortarray);
    System.out.println(quicksortarray);

    }
    return quicksortarray;

    }

    private void swap(int[] result, int a, int b){
    int temp = result[a];
    result[a] = result[b];
    result[b] = temp;
    }




    public static void main(String[] args)
    throws FileNotFoundException

    {
    quicksort app = new quicksort("/Volumes/Anna/Datei.txt");
    app.quicksort(quicksortarray);


    }
    }

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    Can you copy and post console from when you execute your program? Also show the contents of the file being sorted.

    For testing I'd leave out the file and just load the array to be sorted in your program: quickSortArray = new int[] {1, 30, 3, 2};
    Later you can change you code to get the numbers from a file. To get your sort working you do NOT need a file.

  11. #11
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default

    3
    9
    7
    1
    6
    3
    5
    6
    8
    2

    that's the contents of my file. and that's given out on my console when i am using the programm.

    ok, now i am getting really strange errors

    Exception in thread "main" java.lang.NullPointerException
    at java.io.FileInputStream.<init>(FileInputStream.jav a:103)
    at java.io.FileInputStream.<init>(FileInputStream.jav a:66)
    at java.io.FileReader.<init>(FileReader.java:41)
    at quicksort.reading(quicksort.java:17)
    at quicksort.<init>(quicksort.java:10)
    at quicksort.main(quicksort.java:87)

    and this is my code


    import java.io.*;

    public class quicksort
    {
    private static int[] quicksortarray = new int[] {1, 30, 3, 2};

    public quicksort(String fileName)
    throws FileNotFoundException
    {
    reading(fileName);
    }

    public void reading(String fileName)
    throws FileNotFoundException

    {
    BufferedReader in = new BufferedReader(new FileReader(fileName));
    String line = null;
    int track = 0;

    try
    {
    while(in.readLine() != null)
    track++;
    in.close();
    quicksortarray = new int[track];

    in = new BufferedReader(new FileReader(fileName));

    for(int v = 0; v<track; v++)
    {
    line = in.readLine();
    quicksortarray[v] = Integer.parseInt(line);
    System.out.println(quicksortarray[v]);

    }
    }

    catch (Exception e)
    {
    e.printStackTrace();
    }
    }

    private int[] quicksort(int[] quicksortarray)
    {

    int end = 0;
    int beginning = 0;


    if (end> beginning){
    int pivot = beginning;
    int mitte = beginning;

    for (int i=beginning+1; i<= end; i++){
    if (quicksortarray[i]< quicksortarray[mitte]){
    mitte++;
    swap(quicksortarray, i, mitte);
    System.out.println(quicksortarray[i]);
    }
    }
    swap(quicksortarray, pivot, mitte);

    quicksortarray= quicksort(quicksortarray);
    quicksortarray =quicksort(quicksortarray);
    System.out.println(quicksortarray);

    }
    return quicksortarray;

    }

    private void swap(int[] result, int a, int b){
    int temp = result[a];
    result[a] = result[b];
    result[b] = temp;
    }




    public static void main(String[] args)
    throws FileNotFoundException

    {
    quicksort app = new quicksort(null/*"/Volumes/Anna/Datei.txt"*/);
    app.quicksort(quicksortarray);


    }
    }

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,412
    Rep Power
    25

    Default

    at quicksort.reading(quicksort.java:17)
    This line of the error message gives you the statement nbr where the error occured. Where is line 17?
    What variable on that line is null?
    Please add some comments to your code to explain why you are doing things? for example why are you doing this:[/CODE]
    track++;
    in.close();
    quicksortarray = new int[track];
    [/CODE]

  13. #13
    Join Date
    Jun 2008
    Posts
    54
    Rep Power
    0

    Default

    Hey,
    just wanted to say thank you for ur help and advise...
    i finally managed it :-)

    little_polarbear

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •