Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. import lk.gugsi.pp02.Student;
  2.  
  3. public class BubbleSort<T extends Comparable<T>> {
  4.  
  5.     public static void main(String[] args) {
  6.         Student[] arr = new Student[6];
  7.         arr[0] = new Student("Gugsi", "Poravi", "0777", "Wellawatta","2017000", "DevilSE");
  8.         arr[1] = new Student("Madhavi", "R", "0777", "Gampaha","2016033", "BEngSE");
  9.         arr[2] = new Student("Ihan", "L", "0777", "Dehiwala","2016030", "BEngSE");
  10.         arr[3] = new Student("Thamindu", "W", "0777", "Dehiwala","2017104", "BEngSE");
  11.         arr[4] = new Student("Vikum", "K", "0777", "Athul Kotte","2017116", "BEngSE");
  12.         arr[5] = new Student("Gigara", "H", "0777", "Matara","2017136", "BEngSE");
  13.         BubbleSort<Student> obj = new BubbleSort<>();
  14.         obj.bubbleSort(arr);
  15.        
  16.         System.out.println("print original array");
  17.         for (int i = 0; i < arr.length; i++) {
  18.             System.out.println(arr[i]);
  19.  
  20.         }
  21.        
  22.     }
  23.    
  24.     public  void bubbleSort(T[] numbers) {
  25.         for (int i = 0; i < (numbers.length - 1); i++) {
  26.             boolean swapped = false;
  27.  
  28.             for (int j = 0; j < (numbers.length - 1); j++) {
  29.                 if (numbers[j].compareTo(numbers[j + 1] )> 0) {
  30.                     swapped = true;
  31.                     T temp = numbers[j];
  32.                     numbers[j] = numbers[j + 1];
  33.                     numbers[j + 1] = temp;
  34.                 }
  35.             }
  36.  
  37.             if (swapped == false) {
  38.                 break;
  39.  
  40.             }
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement