aspose file tools*
The moose likes Beginning Java and the fly likes BubbleSort Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "BubbleSort" Watch "BubbleSort" New topic
Author

BubbleSort

Rajani kanth Pandiri
Greenhorn

Joined: Jan 28, 2006
Posts: 13
problem with the following code can anyone help


import java.io.*;
class BubbleSortDemo
{
public static void main(String a[])
{
DataInpuStream dis=new DataInputStream();
System.out.pritnln("Enter the size of Array.");
int size=Integer.parseInt(d.readLine());
int[] arr1=new int[size];
System.out.println("Enter the elements:");
for(int i=0;i<arr.length;i++)
{
arr1[i]=Integer.parseInt(d.readLine());
}
System.out.println("Elements are:");
myprint(arr1);
sort(arr1);
System.out.println("Sorted list Elements are:");
myprint(arr1);
}
static void sort(int l[])
{
for (int j=l.length-1;j>=0;j--)
{
for (int k=0;k<j;k++)
{
if(l[k]>[k+1])
{
l[k]=l[k]+l[k+1];
l[k+1]=l[k]-l[k+1];
l[k]=l[k]-l[k+1];
}
}
}
}
static void myprint(int m[])
{
for (int p=0;p<m.length;p++)
{
System.out.println(" "+m[p]);
}
}
}
Tom McCann
Ranch Hand

Joined: Aug 27, 2005
Posts: 31
On line 27, you seem to be missing an 'l'.

The line if(l[k]>[k+1])

should read: if(l[k]>l[k+1])

Hope this helps.

Tom
Tom McCann
Ranch Hand

Joined: Aug 27, 2005
Posts: 31
Also, there is no void constructor for DataInputStream. i.e. you cannot say



Instead, you must give it an InputStream as a parameter. e.g.



where 'instr' is a previously opened InputStream.
Tom McCann
Ranch Hand

Joined: Aug 27, 2005
Posts: 31
Also, there are spelling mistakes all over the place. Such as:


which should be


and


which should be


Also, the input code should be surrounded by a try/catch block.

Also, DataInputStream.readLine() has been deprecated. This means that it's use is being discouraged and may be unsupported in future versions of the Java api.

See this note from the API documentation:

Deprecated. This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method. Programs that use the DataInputStream class to read lines can be converted to use the BufferedReader class by replacing code of the form:
DataInputStream d = new DataInputStream(in);
with:
BufferedReader d
= new BufferedReader(new InputStreamReader(in));


I tried to clean up the code as much as I could. There are still problems, which I will leave for you to sort out, but this is much healthier version of the code. (see below)

Good Luck,

Tom

Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
problem with the following code can anyone help


Welcome to the Ranch! It looks like some others have taken the time to figure out what problems you might mean. For future reference, you can help us help you more easily (and quickly) if you tell us what problems you have encountered. For example, if you are getting errors when you compile your code, you should post the error messages that you get. The more specific your questions and the more details you give us, the better we will be able to help answer the right question rather than guessing and getting off track on something you might already understand. For more suggestions on how to asks questions to get the answers you want and need, take a look at this page in our FAQ.

Layne


Java API Documentation
The Java Tutorial
 
wood burning stoves
 
subject: BubbleSort