Here is a simple program to sort numbers in ascending as well as descending order using bubble sort
import java.io.*;
public class Bubble {
static int arr[] = new int[5];
static String str[] = new String[5];
static void display()
{
for(int i=0;i<arr.length;i++)
{
System.out.print(" "+arr[i]);
}
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
System.out.println("Enter 5 numbers to sort : ");
for(int i=0,j=1;i<arr.length;i++)
{
System.out.print(j+") ");
str[i] = new BufferedReader(new InputStreamReader(System.in)).readLine();
j++;
}
for(int i=0;i<str.length;i++)
{
String str1 = str[i];
int val = Integer.parseInt(str1);
arr[i] = val;
}
display();
System.out.print("
Select Sorting Order (ascending(a)/descending(d) : ");
String str2 = new BufferedReader(new InputStreamReader(System.in)).readLine();
if(str2.equalsIgnoreCase("a"))
{
for(int i=0;i<=arr.length-1;i++)
{
for(int j=0;j<=arr.length-1;j++)
{
if(arr[i] < arr[j])
{
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
display();
}
if(str2.equalsIgnoreCase("d"))
{
for(int i=0;i<=arr.length-1;i++)
{
for(int j=0;j<=arr.length-1;j++)
{
if(arr[i] > arr[j])
{
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
display();
}
}
}