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

How to use bubble sort in a java program?

I was writing a program that involves the console inputting integers and strings. For my project i prompt the user to enter the number of strings he would like. Then i prompt the user to enter the strings so i could order them alphabetically. I am supposed to use any sorting algorithm but since im new to programming i dont know any.

import java.io.*; 

public class Sorting
{
  private static BufferedReader stdin =  new BufferedReader( new InputStreamReader( System.in ) );

  public static void main(String[] arguments) throws IOException
  {
    System.out.println("How many strings would you like to enter?");
    int stringCount = Integer.parseInt(stdin.readLine());
    String[] stringInput = new String[stringCount];
    String message = "";
    for(int i = 0; i < stringCount; i++)
    {
	System.out.print("Could you enter the strings here: \n");
        stringInput[i] = stdin.readLine();
        message = message + stringInput[i] + ", ";
    }
    System.out.println("So you entered:\n" + message);
     int j;
     boolean flag = true;   // set flag to true to begin first pass
     String temp = null;   //holding variable
     while ( flag )
     {
            flag = false;    //set flag to false awaiting a possible swap
            for (j=0; j<stringInput.length; j++){
                if(stringInput[j].compareTo(stringInput[j+1]) > 0)
                {
                    temp = stringInput[j];
                    stringInput[j] = stringInput[j+1];
                    stringInput[j=1] = temp;
                    flag = true;
                }
            }
            
     }
     System.out.println("The sorted strings are:" + temp);
  }
  public static void BubbleSort(String[] stringInput)
  {
     int j;
     boolean flag = true;   // set flag to true to begin first pass
     String temp = null;   //holding variable
     while ( flag )
     {
            flag = false;    //set flag to false awaiting a possible swap
            for (j=0; j<stringInput.length; j++){
                if(stringInput[j].compareTo(stringInput[j+1]) > 0)
                {
                    temp = stringInput[j];
                    stringInput[j] = stringInput[j+1];
                    stringInput[j=1] = temp;
                    flag = true;
                }
            }
            System.out.println("The sorted strings are:" + temp);
     }
  }
}

This is my code. I was using bubble sort but it didnt go to well. Could someone help.
P.S. I cant use Java.util at all.

vishal1949
Light Poster
42 posts since Jul 2011
Reputation Points: 21
Solved Threads: 1
 
it didnt go to well.


Can you copy and paste the programs output here and add some comments about what is wrong.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
package sorting;
import java.io.*; 

public class Sorting
{
    private static BufferedReader stdin =  new BufferedReader( new InputStreamReader( System.in ) );// this is for retrieving an input

    public static void BubbleSort(String[] stringInput)/* this is where bubble sort comes into handy but doesnt work at all and nothing runs it goes to the next thing*/
    {
        int j;
        boolean flag = true;   // set flag to true to begin first pass
        String temp = null;   //holding variable
        while ( flag )
        {
            flag = false;    //set flag to false awaiting a possible swap
            for (j=0; j<stringInput.length; j++){/* this is the part where i am totally lost */
                if(stringInput[j].compareTo(stringInput[j+1]) > 0)
                {
                    temp = stringInput[j];
                    stringInput[j] = stringInput[j+1];
                    stringInput[j+1] = temp;
                    flag = true;
                }
            }    
        }
        System.out.println("The sorted strings are:" + temp);// this doesnt even run when i want it to.
    }
    public static void main(String[] arguments) throws IOException
    {
        System.out.println("How many strings would you like to enter?");
        int stringCount = Integer.parseInt(stdin.readLine());// retrieving the numeber of string the user wants to input
        String[] stringInput = new String[stringCount];// Creating an array that contains stringCount strings
        String message = "";// using this so i can println
        for(int i = 0; i < stringCount; i++)// using for loop so that user could input as many times as he want
        {
	    System.out.print("Could you enter the strings here: \n");
            stringInput[i] = stdin.readLine();
            message = message + stringInput[i] + ", ";
        }
        System.out.println("So you entered:\n" + message);// this will show what the user in
    }
}
vishal1949
Light Poster
42 posts since Jul 2011
Reputation Points: 21
Solved Threads: 1
 

Can you copy and paste the program's output here and add some comments about what is wrong.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
Can you copy and paste the program's output here and add some comments about what is wrong.
package sorting;
import java.io.*; 

public class Sorting
{
    private static BufferedReader stdin =  new BufferedReader( new InputStreamReader( System.in ) );// this is for retrieving an input

    public static void BubbleSort(String[] stringInput)// this is where bubble sort comes into handy
    {
        int j;
        boolean flag = true;   // set flag to true to begin first pass
        String temp = null;   //holding variable
        while ( flag )
        {
            flag = false;    //set flag to false awaiting a possible swap
            for (j=0; j<stringInput.length; j++){/* this is the part where i am totally lost */
                if(stringInput[j].compareTo(stringInput[j+1]) > 0)
                {
                    temp = stringInput[j];
                    stringInput[j] = stringInput[j+1];
                    stringInput[j+1] = temp;
                    flag = true;
                }
            }    
        }
        System.out.println("The sorted strings are:" + temp);// this doesnt even run when i want it to.
    }
    public static void main(String[] arguments) throws IOException
    {
        System.out.println("How many strings would you like to enter?");
        int stringCount = Integer.parseInt(stdin.readLine());// retrieving the numeber of string the user wants to input
        String[] stringInput = new String[stringCount];// Creating an array that contains stringCount strings
        String message = "";// using this so i can println
        for(int i = 0; i < stringCount; i++)// using for loop so that user could input as many times as he want
        {
	    System.out.print("Could you enter the strings here: \n");
            stringInput[i] = stdin.readLine();
            message = message + stringInput[i] + ", ";
        }
        System.out.println("So you entered:\n" + message);// this will show what the user in
    }
}
vishal1949
Light Poster
42 posts since Jul 2011
Reputation Points: 21
Solved Threads: 1
 
This is my code. I was using bubble sort but it didnt go to well. Could someone help. P.S. I cant use Java.util at all.

Yor are almost correct except 2 logic mistakes..

In line no 17 :

if(stringInput[j].compareTo(stringInput[j+1]) > 0)

You are comparing with index j+1 . And you have used your for loop

for (j=0; j<stringInput.length; j++)


which is not matchable for comparision. It will throw ArrayIndexOutOfBoundsException at last.

So, reduce your array length in to -1 in for loop condition as like this.

for (j=0; j<stringInput.length-1; j++){


Before the end of main method call your BubbleSort method by passing required input.

In line no 26, you have printed temp variable which contains only 1 value.
Using for loop print your stringinput array variable which has been sorted..

I have modified your code here ..

import java.io.*; 

public class Sorting
{
    private static BufferedReader stdin =  new BufferedReader( new InputStreamReader( System.in ) );// this is for retrieving an input

    public static void BubbleSort(String[] stringInput)// this is where bubble sort comes into handy
    {
        int j;
        boolean flag = true;   // set flag to true to begin first pass
        String temp = null;   //holding variable
        while ( flag )
        {
            flag = false;    //set flag to false awaiting a possible swap
            for (j=0; j<stringInput.length-1; j++){/* this is the part where i am totally lost */
                if(stringInput[j].compareTo(stringInput[j+1]) > 0)
                {
                    temp = stringInput[j];
                    stringInput[j] = stringInput[j+1];
                    stringInput[j+1] = temp;
                    flag = true;
                }
            }    
        }
        System.out.println("The sorted strings are:" );// this doesnt even run when i want it to.
        for(int i=0;i<stringInput.length;i++){
            System.out.println(stringInput[i]);
        }
    }
    public static void main(String[] arguments) throws IOException
    {
        System.out.println("How many strings would you like to enter?");
        int stringCount = Integer.parseInt(stdin.readLine());// retrieving the numeber of string the user wants to input
        String[] stringInput = new String[stringCount];// Creating an array that contains stringCount strings
        String message = "";// using this so i can println
        for(int i = 0; i < stringCount; i++)// using for loop so that user could input as many times as he want
        {
	    System.out.print("Could you enter the strings here: \n");
            stringInput[i] = stdin.readLine();
            message = message + stringInput[i] + ", ";
        }
        System.out.println("So you entered:\n" + message);// this will show what the user in
        BubbleSort(stringInput);
    }
}
Muralidharan.E
Junior Poster in Training
74 posts since Apr 2008
Reputation Points: 32
Solved Threads: 6
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: