Open In App

Implementing Sorted Vector in Java

Improve
Improve
Like Article
Like
Save
Share
Report

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

  1. Extend the Vector. (Demonstrated here)
  2. Extend ArrayList.
  3. Extend LinkedList
  4. Directly implement the List interface.

Algorithm:

  1. Extend vector class for implementation of SortedVector.
  2. A private data member for storing data in SortedVector.
  3. For creating objects of SortedVector, we have to define all constructors.
  4. For adding elements in SortedVector, we created a method ‘addingElements’.
    1. Add elements.
    2. If the size of the data member is less than or equal to 1 then there is no need for sorting.
    3. If the size is greater than 1 then sort it.
    4. If the user entered strings then raising an error.
  5. In the end, add elements in a sorted vector.
  6. Clear SortedVector and after that add all data in sorted order.

Below is the implementation of the above approach:

Java




// We are going to implement sorted vector
// from inbuilt Vector class.
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() {}
 
    // method for adding elements in data
    // member of 'SortedVector'
 
    public void addingElement(Object obj)
    {
 
        list.add(obj);
 
        // if list size is less than or equal to one
        // element then there is no need of sorting.
        // here we are sorting elements
 
        if (list.size() > 1) {
 
            // If we are getting character as input then
            // Exceptions occurs in 'Collections.sort'.
            // So, we are type casting character to int.
            // and sorting character as integer.
 
            try {
 
                list.sort(comparator);
            }
            catch (Exception e) {
 
                Object recent = list.lastElement();
 
                list.removeElementAt(list.size() - 1);
 
                int val;
 
                // If we are getting string as input then
                // we are handling this exception here.
 
                try {
 
                    val = (char)recent;
 
                    list.add(val);
 
                    list.sort(comparator);
                }
                catch (Exception e1) {
 
                    System.out.println(
                        "You entered Strings");
                }
            }
        }
 
        addingElementsInSortedVector();
    }
 
    // adding element in object of 'SortedVector'
 
    private void addingElementsInSortedVector()
    {
 
        // clear all values of "SortedVector's" object
 
        clear();
 
        // adding values in object of 'SortedVector'
 
        addAll(list);
    }
}
 
// ours code starts from here
 
public class Main {
 
    public static void main(String[] args)
    {
 
        // creating an object of Sorted vector
 
        SortedVector sorted_vector = new SortedVector();
 
        // we have a method for adding object in
        //'SortedVector' class called 'addingElement'.
        // adding element in object of '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
Previous
Next
Share your thoughts in the comments
Similar Reads