Wednesday, December 26, 2018

implementation of hash table


include<iostream>
using namespace std;
class hash
{
public:
int *a,sz,count;
hash(int n)
{
a=new int[sz=n];
for(int i=0;i<sz;i++)
{
a[i]=-1;
}
count=0;
}
void insert(int);
int delete1(int);
int search(int);
void display();
};
void hash::insert(int x)
{
if(count==sz)
{
cout<<"\n hash table is full ";
}
else
{
int key=x%sz;
int t=key;
if(a[key]==-1)
{
a[key]=x;
count++;
cout<<"\n"<<x<<"element inserted at "<<key+1;
}
else
{
xyz:
if(key==sz-1)
key=-1;
key=key+1;
if(key==t)
{
return ;
}
else
{
if(a[key]==-1)
{
a[key]=x;
count++;
cout<<"\n"<<x<<"element inserted at "<<key+1;
return ;
}
}
goto xyz;
}
}
}
int hash::delete1(int m)
{
int i;
int key=m%sz;
if(count==0)
{
return 0;
}
if(a[key]==m)
{
a[key]=-1;
count--;
return key+1;
}
else
{
for ( i=0;i<sz;i++)
{
if(a[i]==m)
{
a[i]=-1;
count--;
return i+1;
}
}
if(i == sz)
{
return 100;
}
}
}
int hash::search(int m)
{
int i;
int key=m%sz;
if(count==0)
{
return 0;
}
if(a[key]==m)
{
return key+1;
}
else
{
for (i=0;i<sz;i++)
{
if(a[i]==m)
{
return i+1;
}
}
if(i==sz)
{
return 100;
}
}
}
void hash::display()
{
for(int i=0;i<sz;i++)
{
if(a[i]!=-1)
{
cout<<"\n"<<i+1<<" "<<a[i];
}
}
}
int main()
{
int n,x,y,z,ch,fg;
cout<<"\n enter the size of hash table : \n";
cin>>n;
hash ob(n);
while(1)
{
cout<<"\n 1.insert\n2.delete\n3.search\n4.display\n5.exit\n enter your choice : ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\n enter an element to insert \n:";
cin>>x;
ob.insert(x);
break;
case 2:
cout<<"\n enter an element to delete \n :";
cin>>y;
fg=ob.delete1(y);
if(fg==100)
{
cout<<"element not in list\n ";
}
else if(fg==0)
{
cout<<"\n hash table is empty \n ";
}
else
{
cout<<"\n deleted element "<<y<<"at position"<<fg<<"\n";
}
break;
case 3:
cout<<"\n enter an element to search \n :";
cin>>z;
fg=ob.search(z);
if(fg==100)
{
cout<<"element not in list\n ";
}
else if(fg==0)
{
cout<<"\n hash table is empty \n ";
}
else
{
cout<<"\n"<<z<<"element searched at position "<<fg<<"\n";
}
break;
case 4:
ob.display();
break;
case 5:
exit(0);
default : cout<<"\n wrong choice \n";
}
}
return 0;
}
OUTPUT


Labels:

Wednesday, December 19, 2018

C implementation QuickSort

/* C implementation QuickSort */
#include<stdio.h>
  
// A utility function to swap two elements
void swap(int* a, int* b)
{
    int t = *a;
    *a = *b;
    *b = t;
}
  
/* This function takes last element as pivot, places
   the pivot element at its correct position in sorted
    array, and places all smaller (smaller than pivot)
   to left of pivot and all greater elements to right
   of pivot */
int partition (int arr[], int low, int high)
{
    int pivot = arr[high];    // pivot
    int i = (low - 1);  // Index of smaller element
  
    for (int j = low; j <= high- 1; j++)
    {
        // If current element is smaller than or
        // equal to pivot
        if (arr[j] <= pivot)
        {
            i++;    // increment index of smaller element
            swap(&arr[i], &arr[j]);
        }
    }
    swap(&arr[i + 1], &arr[high]);
    return (i + 1);
}
  
/* The main function that implements QuickSort
 arr[] --> Array to be sorted,
  low  --> Starting index,
  high  --> Ending index */
void quickSort(int arr[], int low, int high)
{
    if (low < high)
    {
        /* pi is partitioning index, arr[p] is now
           at right place */
        int pi = partition(arr, low, high);
  
        // Separately sort elements before
        // partition and after partition
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}
  
/* Function to print an array */
void printArray(int arr[], int size)
{
    int i;
    for (i=0; i < size; i++)
        printf("%d ", arr[i]);
    printf("n");
}
  
// Driver program to test above functions
int main()
{
    int arr[] = {10, 7, 8, 9, 1, 5};
    int n = sizeof(arr)/sizeof(arr[0]);
    quickSort(arr, 0, n-1);
    printf("Sorted array: n");
    printArray(arr, n);
    return 0;
}
Output:
Sorted array:
1 5 7 8 9 10

Labels: