Write a program to sort the number with different sorts in
one program ??



Write a program to sort the number with different sorts in one program ?? ..

Answer / Bhupinder Kaur

In Object-Oriented Programming (OOPS), you can create multiple sorting algorithms for a single class. Here's an example in Java using interfaces and a generic SortableList class:

1. Create an interface Sortable with the compare() method:

```
public interface Sortable<T> {
int compare(T o1, T o2);
}
```
2. Create a generic SortableList class implementing Comparable and Comparator interfaces:

```
import java.util.*;

public class SortableList<T extends Sortable<?>> implements List<T> {
private ArrayList<T> list = new ArrayList<>();

public void add(T element) {
list.add(element);
}

@Override
public int compareTo(SortableList<?> o) {
if (o == null || !(o instanceof SortableList)) return -1;
Collections.sort(this, new Comparator<T>() {
@Override
public int compare(T t1, T t2) {
return t1.compare(t2, t1);
}
});
Collections.sort((SortableList<?>) o, new Comparator<T>() {
@Override
public int compare(T t1, T t2) {
return t1.compare(t2, t1);
}
});
return this.size() - o.size();
}

// Implement other List methods here...
}
```
3. Create a custom class implementing the Sortable interface and define the compare() method to use different sorting algorithms:

```
public class MyNumber implements Sortable<MyNumber> {
private int number;

public MyNumber(int number) {
this.number = number;
}

@Override
public int compare(MyNumber o1, MyNumber o2) {
// sort by absolute value for example:
return Math.abs(o1.number) - Math.abs(o2.number);
}
}
```
4. Now you can create a SortableList and add sorted numbers to it:

```
SortableList<MyNumber> list = new SortableList<>();
list.add(new MyNumber(-5));
list.add(new MyNumber(3));
list.add(new MyNumber(-7));
list.add(new MyNumber(2));
// list is now sorted by absolute value: [-7, -5, 2, 3]
```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More OOPS Interview Questions

Write a java applet that computes and displays the squares of values between 25 and 1 inclusive and displays them in a TextArea box

1 Answers  


Write 7 differences between "Public" function and "Private" function?

2 Answers   IBM, Wipro,


How to call a non virtual function in the derived class by using base class pointer

4 Answers   HCL,


in the following, A D B G E C F Each of the seven digits from 0,1,2,3,4,5,6,7,8,9 is: a)Represented by a different letter in abov fig: b)Positioned in the fig abov so tht A*B*C,B*G*E,D*E*F are equal. wch does g represents? C

1 Answers   IonIdea,


What is the difference between class and object?

4 Answers   Apple, Heinz,


Can main method override?

1 Answers  


what do you mean by static member variable?

2 Answers  


i had specified the access specifier for abstarct class member like (pure virtual function) as private.But it can be accessed by the derived class.How the private member of one class is accessed by other class.if any body face this problem and found the solution plz reply to me.

1 Answers   Syntel,


write a program to enter a string like"sunil is a good boy and seeking for a job" not more than 10 characters including space in one line,rest characters should b in other line.if the next line starts from in between the previous word,then print whole word to next line.

1 Answers   Wipro,


What is stream in oop?

1 Answers  


What is methods in oop?

1 Answers  


What is the outcome of the line of code "cout<<abs(- 16.5);"? 1) 16 2) 17 3) 16.5

16 Answers   TCS,


Categories