# Quicksort

• 07-08-2008, 07:24 PM
little_polarbear
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
{

throws FileNotFoundException

{
int[] array = null;

try
{

String zeile = null;
int line = 0;

line++;
in.close();

array = new int[line];

for(int v = 0; v<line; v++)
{
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();

}
}
• 07-08-2008, 08:35 PM
Norm
" 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.
• 07-08-2008, 09:17 PM
little_polarbear
there r no errors... i just cannot give out the sorted numbers on the console :-(
• 07-08-2008, 11:49 PM
Norm
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.
• 07-09-2008, 09:59 AM
little_polarbear
ok, i chankged my public static void main method into

public static void main(String[] args)
throws FileNotFoundException
{
quicksort app = new quicksort();
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
• 07-09-2008, 03:25 PM
Norm
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.
• 07-09-2008, 03:31 PM
little_polarbear
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
{
}

throws FileNotFoundException

{
String line = null;
int track = 0;

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

for(int v = 0; v<track; v++)
{
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);

}
}
• 07-09-2008, 03:42 PM
Norm
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.
• 07-09-2008, 03:49 PM
little_polarbear
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
{
}

throws FileNotFoundException

{
String line = null;
int track = 0;

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

for(int v = 0; v<track; v++)
{
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);

}
}
• 07-09-2008, 03:55 PM
Norm
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.
• 07-09-2008, 05:07 PM
little_polarbear
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

at java.io.FileInputStream.<init>(FileInputStream.jav a:103)
at java.io.FileInputStream.<init>(FileInputStream.jav a:66)
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
{
}

throws FileNotFoundException

{
String line = null;
int track = 0;

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

for(int v = 0; v<track; v++)
{
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);

}
}
• 07-09-2008, 06:12 PM
Norm
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?