m2skills

qhash java

Apr 4th, 2017
8,744
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.46 KB | None | 0 0
  1. // Program to implement Hashing with Quadratic Probing
  2. import java.util.Scanner;
  3.  
  4. class hashTable{
  5.    
  6.     private int size, elementCount;
  7.     private int[] table;
  8.     public Scanner sc = new Scanner(System.in);
  9.    
  10.     // initialize hash Table
  11.     hashTable(){
  12.         System.out.print("Enter the size of the Table : ");
  13.         this.size = sc.nextInt();
  14.        
  15.         this.table = new int[size];
  16.        
  17.         for(int i=0; i < size; i++){
  18.             this.table[i] = 0;
  19.         }
  20.         this.elementCount = 0;
  21.     }
  22.    
  23.    
  24.     // method that checks if the hash table is full or not
  25.     boolean isFull(){
  26.         if(this.elementCount == this.size){
  27.             return true;
  28.         }else{
  29.             return false;
  30.         }
  31.     }
  32.    
  33.    
  34.     // method that returns position for a given element
  35.     int hashFunction(int element){
  36.         return element % this.size;
  37.     }  
  38.    
  39.     // method that finds the next empty position using quadratic probing
  40.     int quadraticProbing(int element, int position){
  41.         int limit = 50, i = 1;
  42.         int newPosition = -1;
  43.         while(i <= 50){
  44.             newPosition = this.hashFunction(position + (int)Math.pow(i, 2));
  45.             if(this.table[newPosition] == 0){
  46.                 break;
  47.             }
  48.             else{
  49.                 i++;
  50.             }
  51.         }
  52.         return newPosition;
  53.     }
  54.    
  55.     // method that inserts element inside the hash table
  56.     void insert(int element){
  57.         int position;
  58.         // checking if the this.table is full
  59.         if(this.isFull()){
  60.             System.out.println("Hash Table Full");
  61.             return;
  62.         }  
  63.        
  64.         boolean isStored = false;
  65.        
  66.         position = this.hashFunction(element);
  67.            
  68.         // checking if the position is empty
  69.         if(this.table[position] == 0){
  70.             this.table[position] = element;
  71.             System.out.println("Element " + element + " at position " + position);
  72.             isStored = true;
  73.             this.elementCount += 1;
  74.         }  
  75.        
  76.         // collision occured hence we do quadratic probing
  77.         else{
  78.             System.out.println("Collision occured for Element " + element + " at position " + position + "Finding new position.");
  79.             position = this.quadraticProbing(element, position);
  80.             if(position != -1){
  81.                 this.table[position] = element;
  82.                 this.elementCount += 1;
  83.                 System.out.println("Element " + element + " stored at position " + position);
  84.             }
  85.         }  
  86.         return;
  87.     }  
  88.  
  89.     // method that searches for an element in the table
  90.     // returns position of element if found
  91.     // else returns false
  92.     int search(int element){
  93.         boolean found = false;
  94.        
  95.         int position = this.hashFunction(element);
  96.        
  97.         if(this.table[position] == element){
  98.             found = true;
  99.             return position;
  100.         }  
  101.        
  102.         // if element is not found at position returned by hash function
  103.         // then we search element using quadratic probing
  104.         else{
  105.             int limit = 50, i = 1;
  106.             int newPosition = -1;
  107.             while(i <= 50){
  108.                 newPosition = this.hashFunction(position + (int)Math.pow(i, 2));
  109.                 if(this.table[newPosition] == element){
  110.                     found = true;
  111.                     break;
  112.                 }
  113.                 else{
  114.                     i++;
  115.                 }
  116.             }
  117.             if(found){
  118.                 return newPosition;
  119.             }
  120.             else{
  121.                 System.out.println("Element not found");
  122.                 return -1;
  123.             }
  124.         }
  125.     }  
  126.    
  127.    
  128.     // method to remove an element from the table      
  129.     void remove(int element){
  130.         int position = this.search(element);
  131.         if(position != -1){
  132.             this.table[position] = 0;
  133.             System.out.println("Element " + element + " is Deleted");
  134.             this.elementCount -= 1;
  135.         }else{
  136.             System.out.println("Element is not present in the Hash Table");
  137.         }
  138.         return;
  139.     }  
  140.    
  141.     // method to display the hash table
  142.     void display(){
  143.         for (int i = 0; i < this.size; i++){
  144.             System.out.println(i + " = " + this.table[i]);
  145.         }  
  146.         System.out.println("The number of element is the Table are : " + this.elementCount);
  147.     }  
  148. };     
  149.  
  150. public class hash{
  151.     public static void main(String arg[]){
  152.        
  153.         hashTable table1 = new hashTable();
  154.  
  155.         // storing elements in this.table
  156.         table1.insert(12);
  157.         table1.insert(26);
  158.         table1.insert(31);
  159.         table1.insert(17);
  160.         table1.insert(90);
  161.         table1.insert(28);
  162.         table1.insert(88);
  163.         table1.insert(40);
  164.         table1.insert(77);      // element that causes collision at position 0
  165.  
  166.         // displaying the Table
  167.         table1.display();
  168.  
  169.         // printing position of elements
  170.         System.out.println("The position of element 31 is : " + table1.search(31));
  171.         System.out.println("The position of element 28 is : " + table1.search(28));
  172.         System.out.println("The position of element 90 is : " + table1.search(90));
  173.         System.out.println("The position of element 77 is : " + table1.search(77));
  174.         System.out.println("The position of element 1 is : " + table1.search(1));
  175.  
  176.  
  177.         table1.remove(90);
  178.         table1.remove(12);
  179.  
  180.         table1.display();
  181.     }
  182. }
Add Comment
Please, Sign In to add comment