Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Driver Class:
- import java.util.Scanner;
- public class sort
- {
- public static void main(String args[])
- {
- Scanner scan = new Scanner(System.in);
- System.out.print("Enter number of integers: ");
- int numints = scan.nextInt();
- int[] intarray = new int[numints];
- for(int i = 0; i < numints; i++)
- {
- System.out.print("Enter integer " + (i + 1) + ": ");
- intarray[i] = scan.nextInt();
- }
- System.out.println();
- System.out.println("The integers you entered are: ");
- bubblesort.printArray(intarray);
- System.out.println();
- System.out.println("Your integers sorted in accending order are: ");
- bubblesort.ascending(intarray);
- bubblesort.printArray(intarray);
- System.out.println();
- System.out.println("Your integers sorted in descending order are: ");
- bubblesort.descending(intarray);
- bubblesort.printArray(intarray);
- }
- }
- Classes:
- public class bubblesort
- {
- public static void ascending(int[] intarray)
- {
- int i, j, temp;
- for(i = intarray.length - 1; i > 0; i--)
- {
- for(j = 0; j < i; j++)
- {
- if(intarray[j] > intarray[j+1])
- {
- temp = intarray[j];
- intarray[j] = intarray[j+1];
- intarray[j+1] = temp;
- }
- }
- }
- }
- public static void descending(int[] intarray)
- {
- int i, j, temp;
- for(i = intarray.length - 1; i > 0; i--)
- {
- for(j = 0; j < i; j++)
- {
- if(intarray[j] < intarray[j+1])
- {
- temp = intarray[j];
- intarray[j] = intarray[j+1];
- intarray[j+1] = temp;
- }
- }
- }
- }
- public static void printArray(int[] intarray)
- {
- for(int i = 0; i < intarray.length; i++)
- {
- System.out.println(intarray[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement