Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import lk.gugsi.pp02.Student;
- public class BubbleSort<T extends Comparable<T>> {
- public static void main(String[] args) {
- Student[] arr = new Student[6];
- arr[0] = new Student("Gugsi", "Poravi", "0777", "Wellawatta","2017000", "DevilSE");
- arr[1] = new Student("Madhavi", "R", "0777", "Gampaha","2016033", "BEngSE");
- arr[2] = new Student("Ihan", "L", "0777", "Dehiwala","2016030", "BEngSE");
- arr[3] = new Student("Thamindu", "W", "0777", "Dehiwala","2017104", "BEngSE");
- arr[4] = new Student("Vikum", "K", "0777", "Athul Kotte","2017116", "BEngSE");
- arr[5] = new Student("Gigara", "H", "0777", "Matara","2017136", "BEngSE");
- BubbleSort<Student> obj = new BubbleSort<>();
- obj.bubbleSort(arr);
- System.out.println("print original array");
- for (int i = 0; i < arr.length; i++) {
- System.out.println(arr[i]);
- }
- }
- public void bubbleSort(T[] numbers) {
- for (int i = 0; i < (numbers.length - 1); i++) {
- boolean swapped = false;
- for (int j = 0; j < (numbers.length - 1); j++) {
- if (numbers[j].compareTo(numbers[j + 1] )> 0) {
- swapped = true;
- T temp = numbers[j];
- numbers[j] = numbers[j + 1];
- numbers[j + 1] = temp;
- }
- }
- if (swapped == false) {
- break;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement