954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help BubbleSort Array wont run?

I'm just learning java and this is my first sort program. I can't get it to run. I sure could use some help.


public class SortCharArray
{
public static void main(String[] args) throws Exception
{
char[] someChars = new char[10];
int x;



for(x = 0; x < someChars.length; ++x)
{
System.out.print("Enter a character ");
someChars[x] = (char)System.in.read();
System.in.read(); System.in.read();

}

System.out.println("Before sort");
for(x = 0; x < someChars.length; ++x)
System.out.print(someChars[x] + " ");

bubbleSort(someChars, someChars.length);

System.out.println("\nAfter sort");
for(x = 0; x < someChars.length; ++x)
System.out.print(someChars[x] + " ");
System.out.println();

}

public static void bubbleSort(char[] array, int len)
{
int a, b;
char temp;
int highSubscript = len - 1;
for(a = 0; a < highSubscript; ++a)
{
for(b = 0; b < highSubscript; ++b)
if(array[b] > array[b = 1])
{
temp = array[b];
array[b] = array[b + 1];
array[b + 1] = temp;
}
}
}

}

Big John
Newbie Poster
1 post since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

Well, supplying the errors you are getting would be helpful.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

Please use code tags when posting code. Read the information in the links below.

This is how I code it in c/c++:

for(a = 0; a < highSubscript-1; ++a)
{
    for(b = 0; b < highSubscript; ++b)
    {
            if(array[a] > array[b])
            {
                temp = array[a];
                array[a] = array[b];
                array[b] = temp;
             }
      }
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I think you may be best off using KeyboardReader to take input.
And you definitely have to use Ancient Dragon's suggestion for the bubble sort.

kimbokasteniv
Junior Poster in Training
50 posts since Nov 2006
Reputation Points: 13
Solved Threads: 4
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You