Java Program to Implement Sorted Vector

«
»
This is a Java Program to Implement Sorted Vector. Here Sorted vector is implemented using a vector and each inserted element is placed at correct position in the vector by insertion sort.

Here is the source code of the Java Program to Implement Sorted Vector. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /**
  2.  **  Java Program to Implement Sorted Vector
  3.  **/
  4.  
  5. import java.util.Scanner;
  6. import java.util.Vector;
  7. import java.util.Collections;
  8.  
  9. class SortedVector
  10. {
  11.     private Vector<Integer> vect;    
  12.  
  13.     /**  Constructor  **/
  14.     public SortedVector()
  15.     {
  16.         vect = new Vector<Integer>();        
  17.     }    
  18.     /**  Function to check if vector is empty  **/
  19.     public boolean isEmpty()
  20.     {
  21.         return vect.size() == 0 || vect == null;
  22.     }
  23.     /** Function to clear vector  **/
  24.     public void clear()
  25.     {
  26.         vect = new Vector<Integer>();        
  27.     }
  28.     /**  Function to get size of vector  **/
  29.     public int size()
  30.     {
  31.         return vect.size();
  32.     }
  33.     /**  Function to add element to vector  **/
  34.     public void add(int ele)
  35.     {
  36.         int pos = vect.size();
  37.         vect.add(ele);                    
  38.         while (pos > 0 && ele < vect.get(pos - 1))
  39.         {
  40.             vect.set(pos, vect.get(pos - 1));
  41.             pos--;
  42.         }
  43.         vect.set(pos, ele);        
  44.     }
  45.     /** Function to remove element at index **/
  46.     public void remove(int ind)
  47.     {
  48.         vect.remove(ind);
  49.     }
  50.     /** Function to perform binary search  **/
  51.     public int binarySearch(int ele)
  52.     {
  53.         return Collections.binarySearch(vect, ele);
  54.     }
  55.     /**  Function to check if element is present in vector  **/
  56.     public boolean contains(int ele)
  57.     {
  58.         return binarySearch(ele) >= 0;
  59.     }
  60.     /** Function to string  **/
  61.     public String toString()
  62.     {
  63.         return vect.toString();
  64.     }    
  65. }
  66.  
  67. /**  Class SortedVector  **/
  68. public class SortedVectorTest
  69. {    
  70.     public static void main(String[] args)
  71.     {             
  72.         Scanner scan = new Scanner(System.in);
  73.         /* Creating object of class SortedVector */
  74.         SortedVector sv = new SortedVector(); 
  75.  
  76.         System.out.println("Sorted Vector Test\n");          
  77.         char ch;
  78.         /*  Perform vector operations  */
  79.         do
  80.         {
  81.             System.out.println("\nSorted Vector Operations\n");
  82.             System.out.println("1. insert");
  83.             System.out.println("2. remove ");
  84.             System.out.println("3. binary search");
  85.             System.out.println("4. contains");
  86.             System.out.println("5. check empty");
  87.             System.out.println("6. get size");  
  88.             System.out.println("7. clear");             
  89.             int choice = scan.nextInt();            
  90.             switch (choice)
  91.             {
  92.             case 1 : 
  93.                 System.out.println("Enter integer element to add");
  94.                 sv.add( scan.nextInt() );                     
  95.                 break;                          
  96.             case 2 : 
  97.                 System.out.println("Enter index");
  98.                 sv.remove(scan.nextInt() );                     
  99.                 break;                         
  100.             case 3 : 
  101.                 System.out.println("Enter integer element to search");
  102.                 System.out.println("Binary search result : "+ sv.binarySearch(scan.nextInt() ));
  103.                 break;                                          
  104.             case 4 : 
  105.                 System.out.println("Enter integer element ");
  106.                 System.out.println("Contains result : "+ sv.contains(scan.nextInt() ));                
  107.                 break;
  108.             case 5 : 
  109.                 System.out.println("Empty status = "+ sv.isEmpty());
  110.                 break;                   
  111.             case 6 : 
  112.                 System.out.println("Size = "+ sv.size() +" \n");
  113.                 break;  
  114.             case 7 : 
  115.                 System.out.println("Sorted Vector cleared");
  116.                 sv.clear();
  117.                 break;                       
  118.             default : 
  119.                 System.out.println("Wrong Entry \n ");
  120.                 break;   
  121.             }
  122.             /*  Display Vector  */ 
  123.             System.out.println(sv);
  124.  
  125.             System.out.println("\nDo you want to continue (Type y or n) \n");
  126.             ch = scan.next().charAt(0);                        
  127.         } while (ch == 'Y'|| ch == 'y');               
  128.     }
  129. }

advertisement
Sorted Vector Test
 
 
Sorted Vector Operations
 
1. insert
2. remove
3. binary search
4. contains
5. check empty
6. get size
7. clear
1
Enter integer element to add
24
[24]
 
Do you want to continue (Type y or n)
 
y
 
Sorted Vector Operations
 
1. insert
2. remove
3. binary search
4. contains
5. check empty
6. get size
7. clear
1
Enter integer element to add
5
[5, 24]
 
Do you want to continue (Type y or n)
 
y
 
Sorted Vector Operations
 
1. insert
2. remove
3. binary search
4. contains
5. check empty
6. get size
7. clear
1
Enter integer element to add
63
[5, 24, 63]
 
Do you want to continue (Type y or n)
 
y
 
Sorted Vector Operations
 
1. insert
2. remove
3. binary search
4. contains
5. check empty
6. get size
7. clear
1
Enter integer element to add
94
[5, 24, 63, 94]
 
Do you want to continue (Type y or n)
 
y
 
Sorted Vector Operations
 
1. insert
2. remove
3. binary search
4. contains
5. check empty
6. get size
7. clear
1
Enter integer element to add
19
[5, 19, 24, 63, 94]
 
Do you want to continue (Type y or n)
 
y
 
Sorted Vector Operations
 
1. insert
2. remove
3. binary search
4. contains
5. check empty
6. get size
7. clear
1
Enter integer element to add
1
[1, 5, 19, 24, 63, 94]
 
Do you want to continue (Type y or n)
 
y
 
Sorted Vector Operations
 
1. insert
2. remove
3. binary search
4. contains
5. check empty
6. get size
7. clear
1
Enter integer element to add
17
[1, 5, 17, 19, 24, 63, 94]
 
Do you want to continue (Type y or n)
 
y
 
Sorted Vector Operations
 
1. insert
2. remove
3. binary search
4. contains
5. check empty
6. get size
7. clear
1
Enter integer element to add
14
[1, 5, 14, 17, 19, 24, 63, 94]
 
Do you want to continue (Type y or n)
 
y
 
Sorted Vector Operations
 
1. insert
2. remove
3. binary search
4. contains
5. check empty
6. get size
7. clear
2
Enter index
3
[1, 5, 14, 19, 24, 63, 94]
 
Do you want to continue (Type y or n)
 
y
 
Sorted Vector Operations
 
1. insert
2. remove
3. binary search
4. contains
5. check empty
6. get size
7. clear
6
Size = 7
 
[1, 5, 14, 19, 24, 63, 94]
 
Do you want to continue (Type y or n)
 
y
 
Sorted Vector Operations
 
1. insert
2. remove
3. binary search
4. contains
5. check empty
6. get size
7. clear
3
Enter integer element to search
14
Binary search result : 2
[1, 5, 14, 19, 24, 63, 94]
 
Do you want to continue (Type y or n)
 
y
 
Sorted Vector Operations
 
1. insert
2. remove
3. binary search
4. contains
5. check empty
6. get size
7. clear
3
Enter integer element to search
45
Binary search result : -6
[1, 5, 14, 19, 24, 63, 94]
 
Do you want to continue (Type y or n)
 
y
 
Sorted Vector Operations
 
1. insert
2. remove
3. binary search
4. contains
5. check empty
6. get size
7. clear
4
Enter integer element
24
Contains result : true
[1, 5, 14, 19, 24, 63, 94]
 
Do you want to continue (Type y or n)
 
y
 
Sorted Vector Operations
 
1. insert
2. remove
3. binary search
4. contains
5. check empty
6. get size
7. clear
7
Sorted Vector cleared
[]
 
Do you want to continue (Type y or n)
 
y
 
Sorted Vector Operations
 
1. insert
2. remove
3. binary search
4. contains
5. check empty
6. get size
7. clear
5
Empty status = true
[]
 
Do you want to continue (Type y or n)
 
n

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.