Java Program to Implement Sorted List

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

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

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
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.