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.
- 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:
After 1nd element Insertion: 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)
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.