Search code examples
javaalgorithmsortingcomparatorbubble-sort

Sorting algorithms - how to implement my classes in main :)


It's silly problem. I have my own comparator interface, class Student - it's objects will be sorted, class BubbleSort with bubblesorting algorithm and main. I think every class except from main is written quite well, but I have problem with implementation of them in main to make my sorting to start :/ I've just created ArrayList of random Students I want to be sorted, but I have problem with BubbleSort class and have no idea, how to start.

In future (I hope it will be today :)) I will do exactly the same with another classes containing sorting algorithms like BubbleSort here. I think their implementation in main will be identical.

import java.util.Random;
import java.util.ArrayList;

public class Program {

    public static void main(String[] args) {
        int elements = 100000;
        ArrayList<Student> list = new ArrayList<Student>();
        Random rand = new Random();
        for (int i=0; i<elements; i++) {
            list.add(new Student(rand.nextInt(4)+2, rand.nextInt(900000)));
        }
        System.out.println(list);
    }
}

.

import java.util.ArrayList;

public class BubbleSort {

    private final Comparator comparator;

    public BubbleSort(Comparator comparator) { 
        this.comparator = comparator; 
    }

    public ArrayList<Student> sort(ArrayList<Student> list) {
        int size = list.size();
        for (int pass = 1; pass < size; ++pass) {
            for (int left = 0; left < (size - pass); ++left) {
                int right = left + 1;
                if (comparator.compare(list.get(left), list.get(right)) > 0)
                    swap(list, left, right);
            }
        }
        return list;
    }

    public int compare(Object left, Object right) throws ClassCastException
            { return comparator.compare(left, right); }


    private void swap(ArrayList list, int left, int right) {
        Object temp = list.get(left);
        list.set(left, list.get(right));
        list.set(right, temp);
    }
}

.

public class Student implements Comparator<Student> {

    int rate;
    int indeks;

    public Student(int ocena, int index) {
        this.rate = ocena;
        indeks = index;
    }

    public String toString() {
        return "Numer indeksu: " + indeks + " ocena: " + rate + "\n";
    }

    public int getIndeks() {
        return indeks;
    }

    public int getRate() {
        return rate;
    }

    public int compare(Student left, Student right) {
        if (left.getIndeks()<right.getIndeks()) {
            return -1;
        }
        if (left.getIndeks() == right.getIndeks()) {
            return 0;
        }
        else {
            return 1;
        }
    }

}

.

public interface Comparator<T> {
    public int compare(T left, T right) throws ClassCastException;
}

Solution

  • Your code looks little bit strange. You didnt mention if you have to use bubble sort so i write both my ideas

    1.Without explicitly using bubble sort

    You can use Collections.sort() combined with overridencompareTo() method

    So your code will look like this

    class Student implements Comparable<Student>{
    //variables constructor methods go here
    private index;
    @Override
    public int compareTo(Students s) {
        int index = s.index;
        if (this.index > index) {
            return 1;
        } else if (this.index == index) {
            return 0;
        } else {
            return -1;
        }
    }
    

    } And in your main class Collections.sort(myStudents)

    2.Explicitly using bubble sort

    Student class

        class Student{
        //class variables methods constructor goes here
        }
    

    Comparator class

        class StudentComparator implements Comparator<Student>{
            @Override
            public int compare(Student a, Student b) {
               //bubble sort code goes here
            }}
    

    Main class

         class MyMainClass{
              public static void main(String[] args) {
              public int elements = 100000;
              ArrayList<Student> list = new ArrayList<Student>();
              Random rand = new Random();
              for (int i=0; i<elements; i++) {
                    list.add(new Student(rand.nextInt(4)+2, rand.nextInt(900000)));
                }
              Collections.sort(list, new StudentComparator());
    
               }