afiqakraam

orderedarray

Mar 27th, 2021
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.52 KB | None | 0 0
  1. /**
  2.  * @afiqakraam
  3.  * @24/03/2021
  4.  */
  5.  
  6. class OrderedArray
  7. {
  8.     private long[] a;
  9.     private int nElems;
  10.    
  11.     public OrderedArray(int max){
  12.         a = new long[max];
  13.         nElems = 0;
  14.     }
  15.    
  16.     public int size(){
  17.         return nElems;
  18.     }
  19.    
  20.     public int find(long searchKey){
  21.         int lowerBound = 0;
  22.         int upperBound = nElems - 1;
  23.         int curln;
  24.        
  25.         while(true){
  26.             curln = (lowerBound + upperBound) / 2;
  27.                 if(a[curln] == searchKey)
  28.                     return curln;
  29.                 else if(lowerBound > upperBound)
  30.                     return nElems;
  31.                 else{
  32.                     if(a[curln] < searchKey)
  33.                         lowerBound = curln + 1;
  34.                     else
  35.                         upperBound = curln - 1;
  36.                     }
  37.                 }
  38.             }
  39.    
  40.     public void insert (long value){
  41.         int j;
  42.         for(j = 0; j < nElems; j++)
  43.             if(a[j] > value)
  44.                 break;
  45.                
  46.         for(int k = nElems; k > j; k--)
  47.             a[k] = a[k-1];
  48.             a[j] = value;
  49.             nElems++;
  50.         }
  51.    
  52.     public boolean delete(long value){
  53.         int j = find(value);
  54.         if(j == nElems)
  55.             return false;
  56.         else{
  57.             for(int k=j; k < nElems; k++)
  58.                 a[k] = a[k+1];
  59.             nElems--;
  60.             return true;
  61.         }
  62.     }
  63.     public void display(){
  64.         for(int j=0; j < nElems; j++)
  65.             System.out.print(a[j]+" ");
  66.         System.out.println(" ");
  67.     }
  68. }
  69. public class OrderedArrayApp{
  70.     public static void main(String[] args){
  71.         int maxSize = 100;
  72.         OrderedArray arr;
  73.         arr = new OrderedArray(maxSize);
  74.        
  75.         arr.insert(77);
  76.         arr.insert(99);
  77.         arr.insert(44);
  78.         arr.insert(55);
  79.         arr.insert(22);
  80.         arr.insert(88);
  81.         arr.insert(11);
  82.         arr.insert(0);
  83.         arr.insert(66);
  84.         arr.insert(33);
  85.        
  86.         //Search Element
  87.         int searchKey = 55;
  88.         if(arr.find(searchKey) != arr.size())
  89.             System.out.println("Found "+searchKey);
  90.         else
  91.             System.out.println("Can't find "+searchKey);
  92.            
  93.         //Display Elements
  94.         arr.display();
  95.        
  96.         //Delete Elements
  97.         arr.delete(0);
  98.         arr.delete(55);
  99.         arr.delete(99);
  100.        
  101.         //Display Elements After Removing Some Elements
  102.         arr.display();
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment