Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-08-2008, 08:24 PM
Member
 
Join Date: Jun 2008
Posts: 41
little_polarbear is on a distinguished road
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");


}
}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-08-2008, 09:35 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,475
Norm is on a distinguished road
" 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.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-08-2008, 10:17 PM
Member
 
Join Date: Jun 2008
Posts: 41
little_polarbear is on a distinguished road
there r no errors... i just cannot give out the sorted numbers on the console :-(
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-09-2008, 12:49 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,475
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-09-2008, 10:59 AM
Member
 
Join Date: Jun 2008
Posts: 41
little_polarbear is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-09-2008, 04:25 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,475
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-09-2008, 04:31 PM
Member
 
Join Date: Jun 2008
Posts: 41
little_polarbear is on a distinguished road
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);

}
}
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-09-2008, 04:42 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,475
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-09-2008, 04:49 PM
Member
 
Join Date: Jun 2008
Posts: 41
little_polarbear is on a distinguished road
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);


}
}
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-09-2008, 04:55 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,475
Norm is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 07-09-2008, 06:07 PM
Member
 
Join Date: Jun 2008
Posts: 41
little_polarbear is on a distinguished road
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);


}
}
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 07-09-2008, 07:12 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 1,475
Norm is on a distinguished road
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]
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 07-12-2008, 10:20 PM
Member
 
Join Date: Jun 2008
Posts: 41
little_polarbear is on a distinguished road
Hey,
just wanted to say thank you for ur help and advise...
i finally managed it :-)

little_polarbear
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +3. The time now is 05:33 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org