Showing posts with label Sorting. Show all posts
Showing posts with label Sorting. Show all posts

Friday, 14 October 2011

Source code for Quick Sort


/********************************************************\
 * ***************                       ************** *
 * *             *       QUICK SORT      *            * *
 * ***************                       ************** *
/********************************************************/


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


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




void quick_sort(int a[], int p, int r)
{
   int q;
   if(p < r)
   {
       q = partition(a, p, r);
       quick_sort(a, p, q-1);
       quick_sort(a, q+1, r);
   }
}


void main()
{
int i, n, p = 0;
        int* a;
  clrscr();
  cout << "Enter  the no of elements: ";
  cin  >> n;
        a = (int*) malloc(sizeof(int)*n);
  cout << "\nEnter the elements:\n\n";
  for(i = 0; i < n; i++)
  {
    cin >> a[i];
  }
  quick_sort(a, p, n);
  cout << "\nSorted array is: ";
  for(i = 0;i < n; i++)
  {
    cout << a[i] << " ";
  }
   getch();
}


Output : 

Source code for Selection Sort


/********************************************************\
 * ***************                       ************** *
 * *             *    SELECTION SORT     *            * *
 * ***************                       ************** *
/********************************************************/


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


void selection_sort(int a[],int n); //Prototype of the calling function..


void main()
{
  int i,n;
  int* a;
  clrscr();
  cout << "Enter the no of elements: ";
  cin  >> n;
  a = (int*) malloc(sizeof(int)*n);
  cout << "\nEnter the  elements:\n\n";
  for(i = 0; i < n; i++)
  {
    cin >> a[i];
  }
  selection_sort(a,n);
  cout << "\nSorted array is:\n\n";
  for(i = 0; i < n; i++)
  {
    cout << "\t" << a[i] << "\n" ;
  }
  getch();
}


void selection_sort(int a[],int n)
{
  int i,j,temp,loc,min;
  for(i = 0; i < n; i++)
  {
    min = a[i];
    loc = i;
    for(j = i+1; j < n; j++)
    {
      if(a[j] < min)
      {
      min = a[j];
loc = j;
      }
    }
    if(loc != i)
    {
      temp = a[i];
      a[i] = a[loc];
      a[loc] = temp;
    }
  }
}


Output :

Source code for Insertion sort


/********************************************************\
 * ***************                       ************** *
 * *             *    INSERTION SORT     *            * *
 * ***************                       ************** *
/********************************************************/


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


void insert(int a[],int n);


void main()
{
  int i,n;
  int* a;
  clrscr();
  cout << "Enter the no. of elements: ";
  cin  >> n;
  a = (int*) malloc(sizeof(int)*n);
  cout << "\nEnter the elements :\n\n";
  for(i=0;i<n;i++)
  {
    cin >> a[i];
  }


  insert(a,n);
  cout << "\nSorted array is: ";
  for(i=0;i<n;i++)
  {
    cout << a[i] << " ";
  }
  getch();
}


void insert(int a[],int n)
{
  int j,k,t;
  for(k=1;k<n;k++)
  {
    t=a[k];
    j=k-1;
    while((t<a[j]) && (j>=0))
    {
      a[j+1]=a[j];
      j=j-1;
    }
    a[j+1]=t;
  }
}

Source code for Bubble Sort




/********************************************************\
 * ***************                       ************** *
 * *             *      BUBBLE SORT      *            * *
 * ***************                       ************** *
/********************************************************/


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


void bubble_sort(int a[], int n);


void main()
{
  int i,n;
  int* a;
  clrscr();
  cout << "Enter the no. of elements:";
  cin  >> n;
  a = (int*) malloc(sizeof(int)*n);   //dynamic array declearation
  cout << "\nEnter the elements :\n\n";
  for(i = 0; i < n; i++)
  {
    cin >> a[i];
  }
  bubble_sort(a, n);
  cout << "\nSorted array is: ";
  for(i = 0; i < n; i++)
  {
    cout << a[i] << " ";
  }
  getch();
}


void  bubble_sort(int a[], int n)
{


  int i,j,temp;
  for(i = 0; i < n; i++)
  {
     for(j = 0; j < n-i-1; j++)
     {
        if(a[j] > a[j+1])
      {
    temp = a[j];
    a[j] = a[j+1];
    a[j+1] = temp;
      }
     }
  }
}


Output :



Wednesday, 12 October 2011

Source code for heap sort


// HeapSort.cpp : Defines the entry point for the console application.
#include <iostream>
#include <conio.h>
#include <malloc.h>

void Heap_Sort(int [], int, int);
void Max_Heapify(int [], int, int);
void Build_Max_Heap(int [], int);

void Heap_Sort(int A[], int length, int heapsize)
{
 Build_Max_Heap(A, length);
 for(int i = length; i >= 2; i--)
 {
  int temp;
  temp = A[1];
  A[1] = A[i];
  A[i] = temp;
  heapsize = heapsize-1;
  Max_Heapify(A, 1, heapsize);
 }
}


void Build_Max_Heap(int A[], int length)
{
 int heapsize;
 heapsize = length;
 for(int i = (length/2); i >= 1; i--)
  Max_Heapify(A, i, heapsize);
}

void Max_Heapify(int A[], int i, int heapsize)
{
 int l, r, largest;
 l = 2*i;
 r = l+1;
 if(l <= heapsize && A[l] > A[i])
  largest = l;
 else
  largest = i;
 if(r <= heapsize && A[r] > A[largest])
  largest = r;
 if(largest != i)
 {
  int temp;
  temp = A[i];
  A[i] = A[largest];
  A[largest] = temp;
  Max_Heapify(A, largest, heapsize);
 }

}
int main()
{
 int length,heapsize;
 int A[10];
 cout << "Enter the no of elements: ";
 cin >> length;
 heapsize =length;
 //A = (int*) malloc(sizeof(int)*length);
 cout << "\nEnter the elements :\n\n";
 for(int i=1; i <= length; i++)
  cin >> A[i];
 Heap_Sort(A, length, heapsize);
 cout << "\nSorted array is : ";
 for(int i = 1; i <= length; i++)
  cout << A[i] << " ";
 getch();
 return 0;
}

Saturday, 1 October 2011

Source code of Merge Sort


/***************************************************************/
/******************** MERGE ** SORT    *************************/
/***************************************************************/


#include <iostream>
#include <conio.h>
#include <malloc.h>
#define INF 2000


void Merge(int [],int ,int ,int);
void Merge_sort(int [], int, int);


int main()
{
int A[10];
int p = 0, n;
cout << "Enter the no of elements: ";
cin >> n;
cout << "Enter the elements:\n";
for(int i=0; i <n; i++)
{
cin >> A[i];
}
Merge_sort(A,p,n);               //calling of Merge sort function
cout << "The sorted array is : ";
for(int i=0; i <n; i++)
{
cout << A[i] << " ";
}
getch();
return 0;
}


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 Merge(int A[], int p, int q, int r)
{
int n1 = (q-p+1), n2 = (r-q);
int L[10];
int R[10];
for(int i = 1; i <= n1; i++)
L[i] = A[p+i-1];
for(int j = 1; j <= n2; j++)
R[j] = A[q+j];
L[n1 + 1] = INF;
R[n2 + 1] = INF;
int i = 1;
int j = 1;
for(int k = p; k <= r; k++)
{
if(L[i] <= R[j])
{
A[k] = L[i];
i = i + 1;
}
else
{
A[k] = R[j];
j = j + 1;
}
}
}

Counting Sort source codes


#include <iostream.h>                
#include <conio.h>
void Counting_sort(int [], int, int);
void main()
{
    int n,k = 0, A[15];
    cout << "Enter the number of input : ";
    cin  >> n;
    cout << "\nEnter the elements to be sorted :\n";
    for ( int i = 1; i <= n; i++)
    {
         cin >> A[i];
         if(A[i] > k)
         {
            k = A[i];
         }
    }
    Counting_sort(A, k, n);
    getch();
}
void Counting_sort(int A[], int k, int n)
{
    int i, j;
    int B[15], C[100];
    for(i = 0; i <= k; i++)
            C[i] = 0;
    for(j =1; 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; j >= 1; j--)
    {
            B[C[A[j]]] = A[j];
            C[A[j]] = C[A[j]] - 1;
    }
    cout << "\nThe Sorted array is : ";
    for(i = 1; i <= n; i++)
            cout << B[i] << "  " ;
}