-2

I've heard about the Bucket Sorting. Could any one clarify how it gives the best performance when we sort millions of records? Is there any algorithm with n*log(n) or O(n) ?

I have 1000 employee objects each employee object has id, name, salary properties. I added these objects to ArrayList I want to sort these objects based on id property. Is there anyway? With out using Collections.sort() method. Considered below solutions, please anybody help which one gives best performance?

Thanks

MaheshVarma
  • 2,081
  • 7
  • 35
  • 58

6 Answers6

3
public class Employee implements Comparable<Employee> {

private int id;
private String name;
private String salary ;

public int compareTo(Employee val) {
      if(id>val.id){
      return 1;  
    }else if(id<val.id){
        return -1;
    } else {
        return 0;
    }
}

//getter and setters here
}

and now you can create a list

 List<Employee> emp= new ArrayList<Employee>();

Now you can use

Collections.sort(emp); 

to sort by id

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
3

The ArrayList can store element in the ArrayFormat. If you identify user employee by id, you can use HashTable class which uses the key and value pair, otherwise you can implement your class using Comparable interface.

public class Employee implements Comparable<Employee> {

    private String id;
    private String name;
    private int salary ;

    public int compareTo(Employee val) {
        return id.compareTo(val.id);
    }

    // getter and setters here...

    List<Employee> emp = new ArrayList<Employee>();
    Collections.sort(emp);
Mohsin Shaikh
  • 494
  • 1
  • 4
  • 11
3

Without Collections.sort() :

First implement Comparable<Employee> in Empoloyee's class and override compareTo

@Override
public int compareTo(Employee o) {

    return this.id.compareTo(o.id);
}

Pass your unsorted list to TreeSet and get set(sorted on id) then create new List with this set

List<Employee> list=new ArrayList<Employee>();
    list.add(new Employee(1, "A", Double.parseDouble("50")));
    list.add(new Employee(22, "B", Double.parseDouble("11")));
    list.add(new Employee(3, "C", Double.parseDouble("222")));
    list.add(new Employee(34, "D", Double.parseDouble("4")));

    SortedSet<Employee> set=new TreeSet<Employee>( list);

    List<Employee> l=new ArrayList<Employee>();
    l.addAll(set);

    System.out.println(l);

OutPut: Sorted on id withoutCollections.sort()

[Employee [id=1, name=A, price=50.0], Employee [id=3, name=C, price=222.0], Employee [id=22, name=B, price=11.0], Employee [id=34, name=D, price=4.0]]

Edit:

Employee class:

class Employee implements Comparable<Employee>{

Integer id;
String name;
Double price;
       -------

}
amicngh
  • 7,831
  • 3
  • 35
  • 54
  • My id property is an integer not a string but compareTo() method supports only string comparision. – MaheshVarma Aug 06 '13 at 11:26
  • `return this.id.compareTo(o.id);` is the section that uses a `String`. If you employ some common sense, this can EASILY be replaced with integer comparisons. – christopher Aug 06 '13 at 11:28
  • 1
    Added Employee class also where in is Integer not String . However compareTo methods is overriden in Integer,String and Date classes.Plz check the API – amicngh Aug 06 '13 at 12:06
1
My requirement is not to use Collections.sort() method

In that case you need to use some existing way of sorting. If the collection is static (no more items being added) I can recommend using either quicksorting or merge-sorting. If there will be items added, heapsorting or binary sorting is probably the best way to go.

bas
  • 1,678
  • 12
  • 23
0

You can use this code to sort the list:

Collections.sort(list, new Comparator() {
    public int compare(Object a, Object b) {
        Employee ea = (Employee) a;
        Employee eb = (Employee) b;
        return ea.getID() - eb.getID();
    }
}

This uses Collections.sort to sort the list with a custom comparator, which compares IDs.

If you can't use Collections.sort, then surely your teacher told you how to sort a list? Maybe it would pay off to pay attention in the lectures.

tbodt
  • 16,609
  • 6
  • 58
  • 83
-1

First convert it into List of your class:

List<Employee> list = new List<Employee>(arraylist);

var sorted = list.OrderBy(o => o.id).ToList();
K T
  • 169
  • 1
  • 4
  • 14