Bubble Sort Practice

The Catch:
Create a program that will sort ten numbers in a descending order.

Solution:
import java.io.*;
class bubblepractice
{
public static void main (String args[]) throws IOException
{
BufferedReader buff = new BufferedReader (new InputStreamReader (System.in));

System.out.println("Enter 10 numbers: ");
int[] numbers = new int [10]; // This signals that ten numbers will be entered by the user

for (int index=0; index<10; index ++) { numbers[index] = Integer.parseInt(buff.readLine()); // Every number entered by the user will be stored in the index of the array numbers } for (int total = 0; total < 10; total++) { for (int two = total + 1; two < 10; two++) // Variable two is the second number next to variable total { if (numbers[two] > numbers[total])
{
int container = numbers[two]; // Initialize the value of two in container if two is bigger than total
numbers[two] = numbers[total]; // store the value of total in two
numbers[total] = container; // store the value of total in the container
}
}
}

System.out.println("To put them in order...");

for (int display = 0; display<10; display++) { System.out.println(numbers[display]); } } }

2 comments:

  1. HI.. Grace. The codes are great.. But if you explain the codes in words, it will be better for your blogs. It will bring you more visitors. Especially Students... I will make a link to your blog from my blog http://beallpass.blogspot.com on this Monday. Requesting you to do the same.

    ReplyDelete
  2. these codes saved me from our EXAM !!

    ReplyDelete

Extended Search

Custom Search
Hello World