Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. /**
  8.  *
  9.  * @author HieuDuong
  10.  */
  11. import java.util.Scanner;
  12. public class Ex2Tut5 {
  13.     public static void bubbleSort(String[] stringArray) {
  14.         int n = stringArray.length;
  15.         String temp;
  16.         for (int i = 0; i < n; i++) {
  17.             for (int j = 1; j < (n - i); j++) {
  18.                 if (stringArray[j - 1].compareTo( stringArray[j] ) > 0) {
  19.                     temp = stringArray[j - 1];
  20.                     stringArray[j - 1] = stringArray[j];
  21.                     stringArray[j] = temp;
  22.                 }
  23.  
  24.             }
  25.         }
  26.     }
  27.     public static void main( String[] args ) {
  28.         Scanner sc = new Scanner(System.in);
  29.         System.out.println("Enter the size of array: ");
  30.         int size = sc.nextInt();
  31.         String[] Arrays = new String[size];
  32.         for(int i = 0; i < size; i++) {
  33.             if (Arrays[i].length() != size) {
  34.                 System.out.println("String index not equal!");
  35.                 }
  36.             Arrays[i]= sc.nextLine();
  37.             }
  38.         bubbleSort(Arrays);
  39.         for(int i = 0; i < size; i++) {
  40.             System.out.println("The sorted array is: " + Arrays[i]);
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement