1

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;
}

3 Answers 3

1

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());

           }
Sign up to request clarification or add additional context in comments.

5 Comments

In fact I would prefer to use Bubblesort instead of Collection.sort()
I see that this is some kind of homework so use bubble sort if you have to. In real project i dont see reason not to use Collections.sort(). It would be much faster and save you lot of time(and lines of code) :)
I have to analyze efficiency of many sorting algorithms. BubbleSort is just one of them I used for example as implementation of the rest will be identical. But I'm stuck in that stupid main
Ok so your approach shoud be creating more comparator classes e.g StudenBubbleSortComparator, StudentInsertSortComparator etc. and use them in main like this Collections.sort(list,new StudentBubbleSortComparator()),Collections.sort(list,new StudentInsertSortComparator())
thank you very much. I used some different way and used BubbleSort bubble = new BubbleSort(new Rates()); bubble.sort(list); System.out.println(list); where "Rates" is comparator of student's rates and my program runs perfectly :) It's different than yours, but your examples gave me idea :)
1

Two points to make here:
a) You are not calling sort at all. You need to instantiate your BubbleSort class and actually call the method. list = new BubbleSort(new Comparator(){...}).sort(list); <-- This syntax also calls for the sort method to be static so that you don't need to make a new object for every sort. The example below sorts by index.

list = new BubbleSort(new Comparator<Student>() {
    @Override
    public compare(Student a, Student b) {
        return a.getIndeks() - b.getIndeks();
    }
}).sort(list);

Btw, this also assumes that BubbleSort is made generic, since it's easier (and kinda makes sense anyway)
b) I hope this is some kind of project where you have to show your ability to make a sorting algorithm, otherwise you should use library methods for these things

Also, while the code is not bad, you might want to show it to someone with professional Java experience (it does not conform to a lot of standards and many things can be improved and made consistent with each other), or post it to https://codereview.stackexchange.com/

3 Comments

c) yes, it is. a) you're wrong :) It prints all elements of list :) b)can you tell something more about this new Comparator(){...} fragment? :) How should this constructor look like in my main exactly? Sry for asking exact idea, but I'm stuck and mindblown :D
Right you are, I was thinking about an older version of the language. I've edited my answer with an example of a comparator
Finally I used thank you very much. I used some different way and used BubbleSort bubble = new BubbleSort(new Rates()); bubble.sort(list); System.out.println(list); where "Rates" is comparator of student's rates and my program runs perfectly :) Thanks for that example. It was very helpful.
0

I dont see you calling the bubblesort class anywhere. A list will not automatically sort its elements. Please go through this link. You ll find it handy. http://www.programcreek.com/2013/03/hashset-vs-treeset-vs-linkedhashset/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.