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


it's silly problem. have own comparator interface, class student - it's objects sorted, class bubblesort bubblesorting algorithm , main. think every class except main written quite well, have problem implementation of them in main make sorting start :/ i've created arraylist of random students want sorted, have problem bubblesort class , have no idea, how start.

in future (i hope today :)) same classes containing sorting algorithms bubblesort here. think implementation in main 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();         (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();         (int pass = 1; pass < size; ++pass) {             (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; } 

your code looks little bit strange. didnt mention if have to use bubble sort write both ideas

1.without explicitly using bubble sort

you can use collections.sort() combined overridencompareto() method

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

} , in 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();           (int i=0; i<elements; i++) {                 list.add(new student(rand.nextint(4)+2, rand.nextint(900000)));             }           collections.sort(list, new studentcomparator());             } 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -