Saturday, May 18, 2013

Quick sort c++ code

For in-depth understanding of Data Structure and Algorithm concepts refer :

1) INTRODUCTION TO ALGORITHMS by Coremen Introduction to Algorithms, 3rd Edition From flipkart.com
Introduction to Algorithms, 3rd Edition From amazon.com
Introduction to Algorithms from amazon.in

2) DATA STRUCTURES USING C AND C++ by Tenenbaum M Aaron - DATA STRUCTURES USING C AND C++ by Tenenbaum M Aaron from flipkart.com
Data Structures Using C and C++ (2nd Edition) from amazon.com
Data Structures Using C and C++ from amazon.in

#include<iostream.h>
#include<conio.h>
int count=0;
int partition(int a[],int  p,int r)
{

    int x,j;
    x=a[r];
    int i=p-1;
    for(j=p;j<r-1;j++)
    {
        if(a[j]<=x)
        {
            i=i+1;
            int t=a[i];
            a[i]=a[j];
            a[j]=t;
        }
    }
    int g=a[i+1];
    a[i+1]=a[r];
    a[r]=g;
    return(i+1);
}


int quicksort( int A[],int p,int r)
{
    int q;
    if(p<r)
    {
        q=partition(A,p,r);
        quicksort(A,p,q-1);
        quicksort(A,q+1,r);
    }
}

void main()
{
    int a[20],n;
    cout<<"Enter the size of the array: "  ;
    cin>>n;
    cout<<"ENTER ARRAY ELEMENTS: \n";
    for(int i=0; i<n; i++)
        cin>>a[i];
    quicksort(a,0,n-1);
    cout<<"YOUR SORTED ARRAY IS: \n";
    for(int i=0;i<n;i++)
        cout<<a[i]<<endl;
    getch();
}

Bucket sort C++ code


For in-depth understanding of Data Structure and Algorithm concepts refer :

1) INTRODUCTION TO ALGORITHMS by Coremen Introduction to Algorithms, 3rd Edition From flipkart.com
Introduction to Algorithms, 3rd Edition From amazon.com
Introduction to Algorithms from amazon.in

2) DATA STRUCTURES USING C AND C++ by Tenenbaum M Aaron - DATA STRUCTURES USING C AND C++ by Tenenbaum M Aaron from flipkart.com
Data Structures Using C and C++ (2nd Edition) from amazon.com
Data Structures Using C and C++ from amazon.in

#include<iostream.h>
#include<stdio.h>
#include<conio.h>

class node
{
    public:
        float info;
        node *next,*prev;
        node(){next=prev=0;}
        node(int n, node *ptr=0, node *ptr1=0)
        {
            info=n;
            next=ptr;
            prev=ptr1;
        }
};

/* sort the link list of elements*/
void isort(node *head)
{
    if (head == 0)
        return;
    float key;
    node *j,*i, *q=0;

    for(i=head->next;i!=0;i=i->next)
    {
        q=0;
        key = i->info;
        j=i->prev;
        while(j!=0 && key < (j->info) )
        {
            j->next->info=j->info;
            q = j;
            j=j->prev;
        }

        if(q!=0)
            q->info = key;
    }

}


void bucket_sort(float *a,int n)
{
    /* create buckets */
    node* *b = new node* [n];
    for(int i=0; i<n;i++)
        b[i]=0;

    /* step 2 begins - insert a[i] into buckets */
    for(int i=0;i<n;i++)
    {
        int index=n*a[i];

        node *temp=new node(sizeof(node));
        temp->info=a[i];

        if(b[index]==NULL)
        {
            b[index]=temp;
        }
        else
        {
            temp->next=b[index];
            b[index]->prev=temp;
            b[index]=temp;
        }
    } // end of for
    /* step 2 ends */

    /* step 3 begins - sort each bucket */
    for(int i=0; i<n;i++)
    {
        isort(b[i]);
    }
    /* step 3 ends */

    /* step 4 begins - concatenate all the buckets */
    node *trav=0;
    cout<<"\n\nSorted array is: \n";
    for(int i=0; i<n;i++)
    {
        trav = b[i];
        while(trav != 0)
        {
            cout<<trav->info<<" ";
            trav = trav->next;
        }
    }
    /* step 4 ends */
}

void main()
{
    clrscr();

    int n;
    float *a;

    cout<<"\n\t Enter the size of array  :  ";
    cin>>n;

    cout<<"\n\t Enter the elements of an array  :\n";
    a=new float[n];
    for(int l=0;l<n;l++)
        cin>>a[l];

    cout<<"\n\t Unsorted array is..\n";
    for(l=0;l<n;l++)
        cout<<a[l]<<" ";
    cout<<"\n" ;

    /* sort lements using buckt sort */
    bucket_sort(a,n);

    getch();
}

Radix sort c++ code

For in-depth understanding of Data Structure and Algorithm concepts refer :

1) INTRODUCTION TO ALGORITHMS by Coremen Introduction to Algorithms, 3rd Edition From flipkart.com
Introduction to Algorithms, 3rd Edition From amazon.com
Introduction to Algorithms from amazon.in

2) DATA STRUCTURES USING C AND C++ by Tenenbaum M Aaron - DATA STRUCTURES USING C AND C++ by Tenenbaum M Aaron from flipkart.com
Data Structures Using C and C++ (2nd Edition) from amazon.com
Data Structures Using C and C++ from amazon.in

#include<iostream.h>
#include<conio.h>
#include<string.h>

void countingsort(char s[][20],int a[],char b[][20],int n,int k)
{   int c[100];
    int l,j,i;
    for( l=0;l<=k;l++)
        c[l]=0;
    for(j=0;j<n;j++)
        c[a[j]]=c[a[j]]+1;
    for(i=1;i<=k;i++)
        c[i]=c[i]+c[i-1];
    for(j=n-1;j>=0;j--)
    {
        strcpy(b[c[a[j]]-1],s[j]);
        c[a[j]]=c[a[j]]-1;
    }
    for(i=0;i<n;i++)
    {
        strcpy(s[i],b[i]);
    }
}

void radixsort(char s[][20],int d,int n)
{  int z[20];
    char b[20][20];
    int i,j,k;
    j=d-1;
    for(i=1;i<=d;i++)
    {
        for(k=0;k<n;k++)
        {
            z[k]=s[k][j]-48;
        }
        --j;

        countingsort(s,z,b,n,9);
    }
    cout<<"Output"<<endl;
    for(i=0;i<n;i++)
        cout<<b[i]<<endl;
}


void main()
{
    int n,i,d;
    char a[20][20];
    cout<<"Entr the size of the array"<<endl;
    cin>>n;
    cout<<"Enter the no. of digits"<<endl;
    cin>>d;
    cout<<"Enter its element"<<endl;
    for(i=0;i<n;i++)
        cin>>a[i];
    radixsort(a,d,n);

    getch();
}

priority queue c++ code

For in-depth understanding of Data Structure and Algorithm concepts refer :

1) INTRODUCTION TO ALGORITHMS by Coremen Introduction to Algorithms, 3rd Edition From flipkart.com
Introduction to Algorithms, 3rd Edition From amazon.com
Introduction to Algorithms from amazon.in

2) DATA STRUCTURES USING C AND C++ by Tenenbaum M Aaron - DATA STRUCTURES USING C AND C++ by Tenenbaum M Aaron from flipkart.com
Data Structures Using C and C++ (2nd Edition) from amazon.com
Data Structures Using C and C++ from amazon.in

#include<iostream.h>
#include<conio.h>
#include<process.h>
int n;
void maxheapinsert(int [],int key);
void heapincreasekey(int a[],int i,int key);
int heapextractmax(int a[]);
int prioritymaximum(int a[]);
void max_heapify(int a[20],int n1);
int heapsize;
int left(int i)
{
    return(2*i);
}
int right(int i)
{
    return((2*i)+1);
}
void max_heapify(int A[20],int i)
{
    int l,r,largest,s;
    l=left(i);
    r=right(i);

    if(l<=heapsize && A[l]>A[i])
        largest=l;
    else
        largest=i;

    if(r<=heapsize && A[r]>A[largest])
        largest=r;

    if(largest!=i)
    {
        s=A[i];
        A[i]=A[largest];
        A[largest]=s;
        max_heapify(A,largest);
    }
}
int prioritymaximum(int a[])
{

    return a[1];
}
int heapextractmax(int a[])
{
    if(n<0)
        cout<<"Underflow"<<endl;
    int max=a[1];
    a[1]=a[n];
    n--;
    max_heapify(a,1);
    return max;
}

void heapincreasekey(int a[],int i,int k)
{
    if(k<a[i])
        cout<<"New key is smaller than current key"<<endl;
    a[i]=k;
    while(i>1&&a[i/2]<a[i])
    {
        int t=a[i];
        a[i]=a[i/2];
        a[i/2]=t;
        i=i/2;
    }
}
void maxheapinsert(int a[],int k)
{
    n=n+1;
    a[n]=-999;
    heapincreasekey(a,n,k);
}

void main()
{
    int x,i2,c,key,a[20];
    a[0]=-999;
    int m;
    for(  ;  ; )
    {
        cout<<"Menu:"<<endl;
        cout<<"1:To get the maximum value"<<endl;
        cout<<"2:To extract maximum value"<<endl;
        cout<<"3:To increase key value"<<endl;
        cout<<"4:To insert new value"<<endl;
        cout<<"5:Exit:"<<endl;
        cout<<"Enter your choice"<<endl;
        cin>>c;
        switch(c)
        {
            case 1:
                x=prioritymaximum(a);
                cout<<x<<endl;
                break;
            case 2:
                m=heapextractmax(a);cout<<"process with priority "<<m<<" is being processed";
                break;
            case 3:
                cout<<"Enter the index value that is to modified "<<endl;
                cin>>i2;
                cout<<"Enter the new value"<<endl;
                cin>>key;
                heapincreasekey(a,i2,key);
                cout<<"Value modified"<<endl;
                break;
            case 4:
                cout<<"Enter the new value"<<endl;
                cin>>key;
                maxheapinsert(a,key);
                break;
            case 5:
                exit(0);
            default:
                cout<<"Wrong code"<<endl;
        }
    }
    getch();
}

Merge sort C++ code

For in-depth understanding of Data Structure and Algorithm concepts refer :

1) INTRODUCTION TO ALGORITHMS by Coremen Introduction to Algorithms, 3rd Edition From flipkart.com
Introduction to Algorithms, 3rd Edition From amazon.com
Introduction to Algorithms from amazon.in

2) DATA STRUCTURES USING C AND C++ by Tenenbaum M Aaron - DATA STRUCTURES USING C AND C++ by Tenenbaum M Aaron from flipkart.com
Data Structures Using C and C++ (2nd Edition) from amazon.com
Data Structures Using C and C++ from amazon.in

#include<iostream.h>
#include<conio.h>
void merge(int a[], int p, int q, int r)
{
    int i,j;
    int n1, n2;
    n1=q-p+1;
    n2=r-q;
    int L[21], R[21];
    for(int i=0; i<n1; i++)
        L[i]=a[p+i];
    for(int j=0; j<n2; j++)
        R[j]=a[q+j+1];
    L[n1]=999;
    R[n2]=999;
    i=0; j=0;
    for(int k=p; k<=r; k++)
    {
        if(L[i]<=R[j])
        {
            a[k]=L[i];
            i++;
        }

        else
        {
            a[k]=R[j];
            j++;
        }
    }
}
void merge_sort(int a[], int p,int r)
{
    int q;
    if(p<r)
    {
        q=(p+r)/2;
        merge_sort(a, p, q);
        merge_sort(a, q+1, r);
        merge(a, p, q, r);
    }
}

void main()
{
    int a[20],n;
    cout<<"Enter the size of the array: "  ;
    cin>>n;
    cout<<"ENTER ARRAY ELEMENTS: \n";
    for(int i=0; i<n; i++)
        cin>>a[i];
    merge_sort(a,0,n-1);
    cout<<"YOUR SORTED ARRAY IS: \n";
    for(int i=0;i<n;i++)
        cout<<a[i]<<endl;
    getch();}



Insertion sort C++ code

For in-depth understanding of Data Structure and Algorithm concepts refer :

1) INTRODUCTION TO ALGORITHMS by Coremen Introduction to Algorithms, 3rd Edition From flipkart.com
Introduction to Algorithms, 3rd Edition From amazon.com
Introduction to Algorithms from amazon.in

2) DATA STRUCTURES USING C AND C++ by Tenenbaum M Aaron - DATA STRUCTURES USING C AND C++ by Tenenbaum M Aaron from flipkart.com
Data Structures Using C and C++ (2nd Edition) from amazon.com
Data Structures Using C and C++ from amazon.in

#include<iostream.h>
#include<conio.h>
void insertion_sort(int a[10],int n)
{
int j,key,i;
for( j=1;j<n;j++)
{
 key=a[j];
 i=j-1;
 while(i>=0&&a[i]>key)
   {
     a[i+1]=a[i];
       i=i-1;
   }
   a[i+1]=key;
}
}
void main()
{

 int a[20],n;

 cout<<"enter the size of array\n";
 cin>>n;
 cout<<"enter the elements\n";
 for(int i=0;i<n;i++)
  cin>>a[i];

  insertion_sort(a,n);

 cout<<"sorted array is\n";
  for(int i=0;i<n;i++)
    cout<<a[i]<<",";
   getch();
 }

Tuesday, May 14, 2013

Time/Space complexity of sorting algorithms

For in-depth understanding of Data Structure and Algorithm concepts refer :

1) INTRODUCTION TO ALGORITHMS by Coremen Introduction to Algorithms, 3rd Edition From flipkart.com
Introduction to Algorithms, 3rd Edition From amazon.com
Introduction to Algorithms from amazon.in

2) DATA STRUCTURES USING C AND C++ by Tenenbaum M Aaron - DATA STRUCTURES USING C AND C++ by Tenenbaum M Aaron from flipkart.com
Data Structures Using C and C++ (2nd Edition) from amazon.com
Data Structures Using C and C++ from amazon.in

Sorting

Algorithm Data Structure Time Complexity Worst Case Auxiliary Space Complexity


Best Average Worst Worst
Quicksort Array O(n log(n)) O(n log(n)) O(n^2)         O(log(n))
Mergesort Array O(n log(n)) O(n log(n)) O(n log(n))           O(n)
Heapsort Array O(n log(n)) O(n log(n)) O(n log(n))           O(1)
Bubble Sort Array O(n) O(n^2) O(n^2)           O(1)
Insertion Sort Array O(n) O(n^2) O(n^2)           O(1)
Select Sort Array O(n^2) O(n^2) O(n^2)           O(1)
Bucket Sort Array O(n+k) O(n+k) O(n^2)           O(nk)
Radix Sort Array O(nk) O(nk) O(nk)           O(n+k)