Implementing Sorted Vector in Java
Vector is a class that implements the List interface. It is a type of dynamic array that means the size of a vector can be grown or shrink during the execution of the program. The initial size of the vector is 10 and if we insert more than 10 elements then the size of the vector is increased by 100%, or we can say that it doubles the size. Like, initially the size of the vector is 10, and if we insert more than 10 elements the size of the vector is 20 and after inserting 20 elements in the vector, the size of the vector will be 40.
The Following diagram of the collection API
In which there is an Interface name Collection which is extended by Set, List, and Queue interfaces, and List interface is extended by ‘Vector’ class.
Approaches: Implementation for Descending Order Sorting
For Implementing SortedVector
- Extend the Vector. (Demonstrated here)
- Extend ArrayList.
- Extend LinkedList
- Directly implement the List interface.
Algorithm:
- Extend vector class for implementation of SortedVector.
- A private data member for storing data in SortedVector.
- For creating objects of SortedVector, we have to define all constructors.
- For adding elements in SortedVector, we created a method ‘addingElements’.
- Add elements.
- If the size of the data member is less than or equal to 1 then there is no need for sorting.
- If the size is greater than 1 then sort it.
- If the user entered strings then raising an error.
- In the end, add elements in a sorted vector.
- Clear SortedVector and after that add all data in sorted order.
Below is the implementation of the above approach:
Java
import java.util.*;
class SortedVector extends Vector<Object> {
private final Vector<Object> list = new Vector<>();
private final Comparator<? super Object> comparator
= Collections.reverseOrder();
public SortedVector() {}
public void addingElement(Object obj)
{
list.add(obj);
if (list.size() > 1 ) {
try {
list.sort(comparator);
}
catch (Exception e) {
Object recent = list.lastElement();
list.removeElementAt(list.size() - 1 );
int val;
try {
val = ( char )recent;
list.add(val);
list.sort(comparator);
}
catch (Exception e1) {
System.out.println(
"You entered Strings" );
}
}
}
addingElementsInSortedVector();
}
private void addingElementsInSortedVector()
{
clear();
addAll(list);
}
}
public class Main {
public static void main(String[] args)
{
SortedVector sorted_vector = new SortedVector();
sorted_vector.addingElement( 1 );
System.out.println( "After 1st element Insertion:" );
for (Object i : sorted_vector) {
System.out.println(i);
}
sorted_vector.addingElement( 99 );
System.out.println( "After 2nd element Insertion:" );
for (Object i : sorted_vector) {
System.out.println(i);
}
System.out.println( "After 3rd element Insertion:" );
sorted_vector.addingElement( 2 );
for (Object i : sorted_vector) {
System.out.println(i);
}
sorted_vector.addingElement( 0 );
System.out.println( "After 4th element Insertion:" );
for (Object i : sorted_vector) {
System.out.println(i);
}
}
}
|
Output
After 1st element Insertion:
1
After 2nd element Insertion:
99
1
After 3rd element Insertion:
99
2
1
After 4th element Insertion:
99
2
1
0
Time Complexity: O(n2 log n)
Last Updated :
14 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...