Java Program for Bubble Sort in Descending Order

public class BubbleSortDescendingOrderDemo
{

	public static void main(String a[])
	{
		//Numbers which need to be sorted
		int numbers[] = {23,5,23,1,7,12,3,34,0};

		//Displaying the numbers before sorting
		System.out.print("Before sorting, numbers are ");
		for(int i = 0; i < numbers.length; i++)
		{
			System.out.print(numbers[i]+" ");
		}
		System.out.println();

		//Sorting in descending order using bubble sort
		bubbleSortInDescendingOrder(numbers);

		//Displaying the numbers after sorting
		System.out.print("Before sorting, numbers are ");
		for(int i = 0; i < numbers.length; i++)
		{
			System.out.print(numbers[i]+" ");
		}

	}

	//This method sorts the input array in desecnding order
	public static void bubbleSortInDescendingOrder(int numbers[])
	{
		int temp;

		for(int i = 0; i < numbers.length; i++)
		{
			for(int j = 1; j < (numbers.length-i); j++)
			{
				//if numbers[j-1] < numbers[j], swap the elements
				if(numbers[j-1] < numbers[j])
				{
					temp = numbers[j-1];
					numbers[j-1]=numbers[j];
					numbers[j]=temp;
				}
			}
		}
	}


}

Output

Output of the Program to Sort in Descending Order Using Bubble Sort


Comments(2)


vinod 17 Apr 2013 3:02 PM
How to count repetetive numbers in array plz send me
Like (0)| Dislike (0)| Reply| Flag

placido adong 01 Aug 2011 12:55 PM
thanks to your simple program...
Like (0)| Dislike (0)| Reply| Flag

 
Add a new comment ...


Name   Email

Please answer the simple math question given below

10 + 2 =


close

Sign in