Solved

bubble sort program sorting numbers in ascending order using java

Posted on 2007-11-27
16
673 Views
Last Modified: 2013-11-23
How to solve a problem where the program must arrange integers input by the user in ascending order. The user is prompted to input an amount of integers to be sorted & then the actual numbers. This program is a bubble sort problem
0
Comment
Question by:s4suxs
  • 4
  • 3
  • 3
  • +3
16 Comments
 
LVL 26

Accepted Solution

by:
ksivananth earned 250 total points
ID: 20356409
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 20356410
Study the following for your assignment before Java implementation:

http://en.wikipedia.org/wiki/Bubble_sort
0
 
LVL 92

Expert Comment

by:objects
ID: 20356418
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20356432
>>here u go

Why not just supply ALL the code? ;-)


You'll need to read standard input http://www.exampledepot.com/egs/java.io/ReadFromStdIn.html
0
 
LVL 92

Expert Comment

by:objects
ID: 20356446
you caqn adapt the following to read user input into a file

http://www.objects.com.au/java/qa/908090699.html

just replace the file stream with stdin
and change the test condition for example to look for an empty line

the follwoing can be used to convert string to an int

http://www.objects.com.au/java/qa/590040453.html

let me know if u have any questions :)
0
 
LVL 9

Expert Comment

by:ysnky
ID: 20356528
0
 

Author Comment

by:s4suxs
ID: 20356798
this is what i got so far, but after i enter how many numbers i want to enter, it just enters all 0's.  for example, if i say that i want to input 5 numbers, it will automatically input 5 zeros and put them in order as the output, how do i fix it?
class SortArray

{

public static void main(String args[])

{

 Scanner in = new Scanner(System.in);

        int n = in.nextInt();

        int arr1[] = new int[n];
 

 int entry[] = new int[x];

        BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
 

          for(int i = 0; i < entry.length; i++)

          {

               strEntry = dataIn.readLine();

               entry[i] = Integer.parseInt(strEntry);

               System.out.println();

          }
 
 

for (int i=0;i<arr1.length;i++)

System.out.println(arr1[i]);

for(int i=0;i<(arr1.length-1);i++)

{

for (int j=i+1;j<arr1.length;j++)

{

if(arr1[i]>arr1[j])

{

int x,y;

x=arr1[i];

y=arr1[j];

arr1[i] = y;

arr1[j] = x;

}

}

}
 

System.out.println("Array after sorting in ascending order");

System.out.println();

for (int i=0;i<arr1.length;i++)

System.out.println(arr1[i]);
 

}

}

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 26

Expert Comment

by:ksivananth
ID: 20356816
>>          for(int i = 0; i < entry.length; i++)
          {
               strEntry = dataIn.readLine();
               entry[i] = Integer.parseInt(strEntry);
               System.out.println();
          }
>>

change to

          for(int i = 0; i < arr1.length; i++)
          {
               strEntry = dataIn.readLine();
               arr1[i] = Integer.parseInt(strEntry);
               System.out.println();
          }
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 20356825
and you don't need entry array at all unless you want to have the user entry for some purpose!
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 20356829
even if you want to have, you have to make a copy...
0
 

Author Comment

by:s4suxs
ID: 20361481
this is what i have so far, but i still cant figure out how to get the user to input their own numbers, who can help?
import java.util.Scanner;

import java.io.*;
 

class sam

{

public static void main(String args[])

{
 

         Scanner in = new Scanner(System.in);

         int n = in.nextInt();

         int arr1[] = new int[n];
 

        int entry[] = new int[n];

        BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
 
 
 

        for (int i=0;i<arr1.length;i++)

        System.out.println(arr1[i]);
 

                for(int i=0;i<(arr1.length-1);i++)

        {

                        for (int j=i+1;j<arr1.length;j++)

                {

                        if(arr1[i]>arr1[j])

                        {

                        int x,y;

                        x=arr1[i];

                        y=arr1[j];

                        arr1[i] = y;

                        arr1[j] = x;

                        }

                }

        }
 

                System.out.println("Array after sorting in ascending order");

                System.out.println();

                for (int i=0;i<arr1.length;i++) System.out.println(arr1[i]);
 

}

}

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 20361543
use something like:

for (int i=0; i<n; i++)
{
   arr1[i] = Integer.parseInt(dataIn.readLine());
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 20362007
You don't need a Scanner *and* a BufferedReader. You can get the numbers in a separate method:

    static int[] getNumbers() {

        Scanner s = new Scanner(System.in);

        int i = 0;

        int[] numbers = new int[256];

        System.out.print("Enter number (q to quit): ");

        while (s.hasNextInt()) {

            System.out.print("Enter number (q to quit): ");

            numbers[i++] = s.nextInt();

        }

        int[] truncated = new int[i];

        System.arraycopy(numbers, 0, truncated, 0, i);

        return truncated;

    }

Open in new window

0
 

Expert Comment

by:blurterboy
ID: 37673764
How do I change this program so that it sorts numbers that the user inputs from the command line instead of the numbers that I have specified? Here is what I have.


public class BubbleSort{
 public static void main(String[] args)
 {
   
   for (String str: args) {
     System.out.println(str);
   }
 
   int i;
   int array[] = {5, 10, 297, 3, 18, 4, 5};
   
   for(i = 0; i < array.length; i++)
     System.out.print( array[i]+"  ");
   System.out.println();
   bubbleSort(array, array.length);
   
   for(i = 0; i < array.length; i++)
     System.out.print(array[i]+"  ");
   System.out.println();
  }
 
 public static void bubbleSort( int a[], int n ){
 int i, j,t=0;
 for(i = 0; i < n; i++){
 
   for(j = 1; j < (n-i); j++){
     if(a[j-1] > a[j]){
       t = a[j-1];
       a[j-1]=a[j];
       a[j]=t;
 }
 }
 }
 }
}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Eclipse IDE - Cannot copy/paste from console output 8 124
Securing Jmx Console and web console 2 64
Java Timer (static) 9 36
windows explorer path to command prompt 5 27
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now