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)
Sign in (optional)
showing 1-2 of 2 comments,   sorted newest to the oldest

alqbbany
2014-10-25 12:08:57 
well come

Write java programs :

1- code to read a file line by line ?

2- code to read a file Word by word ( Tokenizer ) ?

3- code to count the number of line in the file ?

4- code to read 10 number form the key board then store it in a file without delete the content of the file .
(0) (0) Reply
vinod
2013-04-17 12:32:19 
How to count repetitive numbers in array
(0) (0) Reply
Add a new comment ...
Use Discussion Board for posting new aptitude questions.


Name   Email (optional)

2 + 4 = (please answer the simple math question)

Post Your Comment