Skip to content
The Chat Archive
o1-mini-2024-09-12· OpenAI WildChat-4.8M (AI2)

// C++ implementation of the Quadratic Probing #include <bits/stdc++.h> using namespace…

  1. User

    // C++ implementation of the Quadratic Probing #include <bits/stdc++.h> using namespace std;

    // Function to print an array void printArray(vector &arr) { // Iterating and printing the array for (int i = 0; i < arr.size(); i++) { cout << arr[i] << " "; } }

    // Function to implement the quadratic probing void hashing(vector &hashTable, vector &arr) { int n = arr.size(); int tSize = hashTable.size();

    // Iterating through the array
    for (int i = 0; i < n; i++) {
      
        // Computing the hash value
        int hv = arr[i] % tSize;
    
        // Insert in the table if there
        // is no collision
        if (hashTable[hv] == -1)
            hashTable[hv] = arr[i];
        else {
            // If there is a collision
            // iterating through all
            // possible quadratic values
            for (int j = 1; j <= tSize; j++) {
                // Computing the new hash value
                int t = (hv + j * j) % tSize;
                if (hashTable[t] == -1) {
                    // Break the loop after
                    // inserting the value
                    // in the table
                    hashTable[t] = arr[i];
                    break;
                }
            }
        }
    }
    printArray(hashTable);
    

    }

    int main() { vector arr = { 50, 700, 76, 85, 92, 73, 101 };

    // Initializing the hash table
    vector<int> hashTable(11,-1);
    hashing(hashTable,arr);
    return 0;
    

    }

    Give me code in Python, Java, C#, JavaScript and C

    Remember like Cpp is for C++ and at start replace Cpp with language name

    without changing variable name and also do not change comments

    have one line space after code when comment is written.

    don’t use class if needed then name it GfG

    don’t use public if not needed.

    and In C program don’t use #define and malloc and calloc instead use simple array

  2. Assistant

    Python

    # Function to print an array
    def printArray(arr):
        # Iterating and printing the array
        for i in range(len(arr)):
            print(arr[i], end=" ")
        print()
    
    # Function to implement the quadratic probing
    def hashing(hashTable, arr):
        n = len(arr)
        tSize = len(hashTable)
      
        # Iterating through the array
        for i in range(n):
          
            # Computing the hash value
            hv = arr[i] % tSize
    
            # Insert in the table if there
            # is no collision
            if hashTable[hv] == -1:
                hashTable[hv] = arr[i]
            else:
                # If there is a collision
                # iterating through all
                # possible quadratic values
                for j in range(1, tSize + 1):
                    # Computing the new hash value
                    t = (hv + j * j) % tSize
                    if hashTable[t] == -1:
                        # Break the loop after
                        # inserting the value
                        # in the table
                        hashTable[t] = arr[i]
                        break
        printArray(hashTable)
    
    def main():
        arr = [50, 700, 76, 85, 92, 73, 101]
     
        # Initializing the hash table
        hashTable = [-1] * 11
        hashing(hashTable, arr)
    
    if __name__ == "__main__":
        main()
    

    Java

    import java.util.*;
    
    class GfG {
    
        // Function to print an array
        static void printArray(int[] arr) {
            // Iterating and printing the array
            for(int i = 0; i < arr.length; i++) {
                System.out.print(arr[i] + " ");
            }
        }
    
        // Function to implement the quadratic probing
        static void hashing(int[] hashTable, int[] arr) {
            int n = arr.length;
            int tSize = hashTable.length;
          
            // Iterating through the array
            for(int i = 0; i < n; i++) {
              
                // Computing the hash value
                int hv = arr[i] % tSize;
    
                // Insert in the table if there
                // is no collision
                if(hashTable[hv] == -1)
                    hashTable[hv] = arr[i];
                else {
                    // If there is a collision
                    // iterating through all
                    // possible quadratic values
                    for(int j = 1; j <= tSize; j++) {
                        // Computing the new hash value
                        int t = (hv + j * j) % tSize;
                        if(hashTable[t] == -1) {
                            // Break the loop after
                            // inserting the value
                            // in the table
                            hashTable[t] = arr[i];
                            break;
                        }
                    }
                }
            }
            printArray(hashTable);
        }
    
        public static void main(String[] args) {
            int[] arr = {50, 700, 76, 85, 92, 73, 101};
         
            // Initializing the hash table
            int[] hashTable = new int[11];
            Arrays.fill(hashTable, -1);
            hashing(hashTable, arr);
        }
    }
    

    C#

    using System;
    
    class GfG
    {
        // Function to print an array
        static void printArray(int[] arr)
        {
            // Iterating and printing the array
            for(int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.WriteLine();
        }
    
        // Function to implement the quadratic probing
        static void hashing(int[] hashTable, int[] arr)
        {
            int n = arr.Length;
            int tSize = hashTable.Length;
          
            // Iterating through the array
            for(int i = 0; i < n; i++)
            {
              
                // Computing the hash value
                int hv = arr[i] % tSize;
    
                // Insert in the table if there
                // is no collision
                if(hashTable[hv] == -1)
                    hashTable[hv] = arr[i];
                else
                {
                    // If there is a collision
                    // iterating through all
                    // possible quadratic values
                    for(int j = 1; j <= tSize; j++)
                    {
                        // Computing the new hash value
                        int t = (hv + j * j) % tSize;
                        if(hashTable[t] == -1)
                        {
                            // Break the loop after
                            // inserting the value
                            // in the table
                            hashTable[t] = arr[i];
                            break;
                        }
                    }
                }
            }
            printArray(hashTable);
        }
    
        static void Main(string[] args)
        {
            int[] arr = {50, 700, 76, 85, 92, 73, 101};
         
            // Initializing the hash table
            int[] hashTable = new int[11];
            for(int i = 0; i < hashTable.Length; i++)
                hashTable[i] = -1;
            hashing(hashTable, arr);
        }
    }
    

    JavaScript

    // Function to print an array
    function printArray(arr) {
        // Iterating and printing the array
        let output = "";
        for(let i = 0; i < arr.length; i++) {
            output += arr[i] + " ";
        }
        console.log(output);
    }
    
    // Function to implement the quadratic probing
    function hashing(hashTable, arr) {
        let n = arr.length;
        let tSize = hashTable.length;
      
        // Iterating through the array
        for(let i = 0; i < n; i++) {
          
            // Computing the hash value
            let hv = arr[i] % tSize;
    
            // Insert in the table if there
            // is no collision
            if(hashTable[hv] === -1)
                hashTable[hv] = arr[i];
            else {
                // If there is a collision
                // iterating through all
                // possible quadratic values
                for(let j = 1; j <= tSize; j++) {
                    // Computing the new hash value
                    let t = (hv + j * j) % tSize;
                    if(hashTable[t] === -1) {
                        // Break the loop after
                        // inserting the value
                        // in the table
                        hashTable[t] = arr[i];
                        break;
                    }
                }
            }
        }
        printArray(hashTable);
    }
    
    function main() {
        let arr = [50, 700, 76, 85, 92, 73, 101];
     
        // Initializing the hash table
        let hashTable = new Array(11).fill(-1);
        hashing(hashTable, arr);
    }
    
    main();
    

    C

    #include <stdio.h>
    
    // Function to print an array
    void printArray(int arr[], int size) {
        // Iterating and printing the array
        for(int i = 0; i < size; i++) {
            printf("%d ", arr[i]);
        }
        printf("\n");
    }
    
    // Function to implement the quadratic probing
    void hashing(int hashTable[], int arr[], int n, int tSize) {
        // Iterating through the array
        for(int i = 0; i < n; i++) {
          
            // Computing the hash value
            int hv = arr[i] % tSize;
    
            // Insert in the table if there
            // is no collision
            if(hashTable[hv] == -1)
                hashTable[hv] = arr[i];
            else {
                // If there is a collision
                // iterating through all
                // possible quadratic values
                for(int j = 1; j <= tSize; j++) {
                    // Computing the new hash value
                    int t = (hv + j * j) % tSize;
                    if(hashTable[t] == -1) {
                        // Break the loop after
                        // inserting the value
                        // in the table
                        hashTable[t] = arr[i];
                        break;
                    }
                }
            }
        }
        printArray(hashTable, tSize);
    }
    
    int main() {
        int arr[] = {50, 700, 76, 85, 92, 73, 101};
        int n = sizeof(arr)/sizeof(arr[0]);
     
        // Initializing the hash table
        int hashTable[11];
        for(int i = 0; i < 11; i++)
            hashTable[i] = -1;
        hashing(hashTable, arr, n, 11);
        return 0;
    }