/* Java program on Arrays - Bubble sort on numbers
*/
import java.io.*;
class Bubble
{
public static void main(String args[]) throws IOException
{
int num;
System.out.println("Program to demonstrate bubble sort");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of elements to be entered <10");
num=Integer.parseInt(br.readLine());
System.out.println("Enter the numbers to be sorted");
int arr[]=new int[10];
for(int i=0;i< num;i++)
{
arr[i]=Integer.parseInt(br.readLine());
}
System.out.println("The number you have entered:");
for(int i=0;i< num;i++)
{
System.out.println(arr[i]);
}
for(int i=0;i< num;i++)
{
for(int j=i+1;j< num;j++)
{
if(arr[i]>arr[j])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.println("The sorted numbers are");
for(int i=0;i< num;i++)
{
System.out.println(arr[i]);
}
}
}
/********* Output **********
Program to demonstrate bubble sort
Enter the number of elements to be entered <10
5
Enter the numbers to be sorted
8
65
14
23
2
The number you have entered:
8
65
14
23
2
The sorted numbers are
2
8
14
23
65
*/
Labels: Core Java
Subscribe to:
Post Comments (Atom)
hi sir...
as i study your code...
it seems that it can only sort the given numbers ascending...
how about descending??will it be posible?
and one thing more...
what if you put a part where the user is asked whether to sort the numbers ascending or descending, will it be posible?
yes it is possible .....
for decending the logic will be :
for(int i=0;i< num;i++)
{
for(int j=i+1;j< num;j++)
{
if(arr[i]< arr[j])
{
int temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
you can print a menu and ask the user to enter an option and select either ascending or decending ...
thank you for the sample code sir!!.. and also to lionel for the comments, you know what?, i can't really understand the logic of using array...
with this sample, i was able to understand the relationship of array with one another,...
Please continue it......
Thank you very much!!!!
yr welcome .....
by d way dis is my blog ....
the person whom u r referring as sir is me.
hi sir.. i tried to copy and paste your code but after inputting the 2nd number, i gives me some error msgs...:
--------------------Configuration: --------------------
Program to demonstrate bubble sort
Enter the number of elements to be entered <10
2
Enter the numbers to be sorted
5
6
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.parseInt(Integer.java:499)
at Bubble.main(Bubble.java:16)
Process completed.
hi.. i tried using your code but im prompted w/ an error msgs after inputting the 2nd integer.. need your help pls.. :(
--------------------Configuration: --------------------
Program to demonstrate bubble sort
Enter the number of elements to be entered <10
2
Enter the numbers to be sorted
5
4
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.parseInt(Integer.java:499)
at Bubble.main(Bubble.java:17)
Process completed.