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

Saturday, February 28, 2015

Merge Sorting Program In C++

#include<iostream.h>
#include<conio.h>
void display(int *A,int s){
cout<<"\n";
for(int i=0;i<=s;i++){
cout<<A[i]<<"  ";
}}
void merge(int *A,int p,int q,int r)
{
int n1=q-p+1;
int n2=r-q;
int *L=new int[n1];
int *R=new int[n2];
{
for(int i=0;i<=n1;i++){
R[i]=A[p+i-1];
}

for(int j=0;j<=n2;j++){
R[j]=A[q+j];
}  }
int i=1,j=1;
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)
{
if(p<r){
int q=((p+r)/2);
merge_sort(A,p,q);
merge_sort(A,q+1,r);
merge(A,p,q,r);
}
}
void main()
{
clrscr();
int A[]={5,4,8,7,9,1,6,3};
merge_sort(A,0,7);
display(A,7);
getch();
}

Saturday, December 13, 2014

Shell Sort code in C++

 Other Type Of Sorting : Click here :: most important ::


#include<iostream.h>
#include<conio.h>
 void display(int *arr,int n)
{
 cout<<endl;
 for(int i=0;i<=n;i++){
 cout<<arr[i]<<"\t"; }
 cout<<endl;
}

void shellsort(int *arr,int n)
{
 int gap,i,j,temp;
 for(gap=n/2;gap>0;gap/=2)
 {
 for(i=gap;i<=n;i++)
 {
 for(j=i-gap;j>=0 && arr[j]>arr[j+gap];j-=gap)
 {
 display(arr,n);
 temp=arr[j];
 arr[j]=arr[j+gap];
 arr[j+gap]=temp;
 }
 }
 }
}

void main()
{
int x[]={12,7,3,8,1,5,2};
clrscr();
display(x,6);
shellsort(x,6);
display(x,6);
getch();
}

Quick Sort code in C++

 Other Type Of Sorting : Click here :: most important ::


#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
void display(int *x,int s)
{
cout<<endl;
for(int i=0;i<s;i++)
{
cout<<x[i]<<"  ";
}}


int partition(int* A, int p, int r)
{
 int pivot = r;
 int low = p;
 int high = r-1;
 while(low<high)
 {              display(A,9);
while(low<high && A[low]<A[pivot])
{
low++;
}
while(low<high && A[high]>A[pivot])
{
high--;
}
if(A[high]<A[low]){
int n=A[low];
A[low]=A[high];
A[high]=n;        }



 }   if(A[low]>A[pivot]) {
 int temp=A[low];
 A[low]=A[pivot];
 A[pivot]=temp;     }
 return low;
}
void quickSort(int* A, int p, int r)
{
if( p < r )
{
int q = partition(A, p, r);

quickSort(A, p, q-1);
quickSort(A, q+1, r);
}
}
void main()
{
clrscr();
int A[]={3,1,2,8,7,4,5,6,9};
quickSort(A, 0,8);

display(A,9);
getch();
}
---------------------------------
Another : -
----------------------------------
#include <iostream.h>
#include <conio.h>
void swap(int *a,int b,int c)
{
int temp=a[b];
a[b]=a[c];
a[c]=temp;
}
void display(int *a,int s)
{
cout<<endl;
for(int i=0;i<=s;i++){
cout<<a[i]<<"  ";  }
cout<<endl;
}

void quickSort(int *a,int,int);

int partition(int *, int,int);

void main()
{
    int A[] = {6,10,13,5,8,3,2,25,4,11};
    int p=0;
    int q=10;
    clrscr();
    cout<<"======Original======="<<endl;
    display(A,9);
    quickSort(A,p,q);
    cout<<"======Sorted======="<<endl;
    display(A,9);
    getch();
}


void quickSort(int  *A, int p,int q)
{
    int r;
    if(p<q)
    {   display(A,9);   getch();
    r=partition(A, p,q);
    quickSort(A,p,r);
    display(A,9);          getch();
    quickSort(A,r+1,q);
    display(A,9);
    }
}
int partition(int *A, int p,int q)
{
    int x= A[p];
    int i=p;
    int j;
    for(j=p+1; j<q; j++)
    {   display(A,9);  getch();
    if(A[j]<=x)
    {
        i=i+1;
        swap(A,i,j);
    }
    }
    swap(A,i,p);
    return i;
}

Selection Sort code in C++


 Other Type Of Sorting : Click here :: most important ::



#include<iostream.h>
#include<conio.h>
void display(int *arr,int size)
{
 cout<<endl;
 for(int i=0;i<=size;i++){
 cout<<arr[i]<<"\t";     }
}

int find_min_index(int *arr,int start,int n)
{
 int posmin=start;
 int index;
 for(index=start;index<=n;index++){
 display(arr,n);
 if(arr[index]<arr[posmin]){
 posmin=index;
 cout<<"\n Min index = "<<posmin<<"\t Value = "<<arr[posmin]<<"\n";
 }
 }
return posmin;
}

void selection_sort(int *arr,int n)
{
 int posmin,count,temp;
 for(count=0;count<=n;count++)
 {
 posmin=find_min_index(arr,count,n);
 temp=arr[posmin];
 arr[posmin]=arr[count];
 arr[count]=temp;
 }
}

void main()
{
int x[]={12,5,7,1,9,4,8};
clrscr();
selection_sort(x,6);
display(x,6);
getch();
}

Insertion Sort Code in C++


 Other Type Of Sorting : Click here :: most important ::



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

void display(int *arr,int n)
{
 cout<<endl;
 for(int i=0;i<=n;i++){
 cout<<arr[i]<<"\t";  }
}

void insertion_sort(int *arr,int n)
{
 int pos,count,val;
 for(count=1;count<=n;count++)
 {
 val=arr[count];
 for(pos=count-1;pos>=0;pos--)
 {
 if(arr[pos]>val)     {
 cout<<"\n Swapped arr["<<pos+1<<"] with arr["<<pos<<"]\n";
 arr[pos+1]=arr[pos]; }
 else { break; };
 }
 arr[pos+1]=val;
 display(arr,6);
 }
}

void main()
{
int x[]={12,4,7,2,9,5,1};
clrscr();
display(x,6);
insertion_sort(x,6);
getch();
}


Bubble Sort Code in C++


 Other Type Of Sorting : Click here :: most important ::



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

void display(int *arr,int n)
{
 cout<<endl;
 for(int i=0;i<=n;i++){
 cout<<arr[i]<<"\t";  }
}

void bubblesort(int *arr,int n)
{
 int i,temp,bound=n-1;
 int swapped=1;
 while(swapped>0)
 {
 swapped=0;
 for(i=0;i<=bound;i++){ display(arr,6);
 if(arr[i]>arr[i+1]){
  cout<<endl<<"Swaped arr["<<i<<"] with arr["<<i+1<<"]\n";
 temp=arr[i];
 arr[i]=arr[i+1];
 arr[i+1]=temp;
 swapped=i;          }}
 bound=swapped;
 }
}

void main()
{
int x[]={12,7,5,4,11,9,8};
clrscr();
bubblesort(x,6);
display(x,6);
getch();
}

Sunday, November 30, 2014

Heap Sort (min heap)

Sorting Using min heap and arrange in decending order 


 Other Type Of Sorting : Click here :: most important ::


#include<iostream.h>
#include<conio.h>
#define max 7
class array{
int arr[max];
int count;
public:
array()
{
count=0;
for(int i=0;i<max;i++)
{ arr[i]=0; }
}
void add(int num){
if(count<max){
arr[count]=num; count++;}
else
{cout<<"\n Array is full"<<endl;}
}
void heapsort()
{
for(int i=count-1;i>0;i--)
{int ivalue=arr[i];
arr[i]=arr[0];
arr[0]=ivalue;
makeheap(i);}
}
void display()
{
for(int i=0;i<count;i++)
{ cout<<arr[i]<<"\t";}
}
void makeheap(int c)
{
for(int i=0;i<c;i++)
{
 int val=arr[i];
int s=i;
int f=(s-1)/2;
while(s>0 && arr[f]>val)
{ arr[s]=arr[f];
s=f;
f=(s-1)/2;}
arr[s]=val;
}
}
};
void main()
{
clrscr();
array a;
a.add(15);
a.add(19);
a.add(18);
a.add(7);
a.add(17);
a.add(16);
a.add(8);
cout<<"\n\n Values you insert in min order heap\n \n";
a.display();
a.makeheap(7);
cout<<"\n\n Values inserted in Heap tree\n\n";
a.display();
a.heapsort();
cout<<"\n\n Values after heap sort (min order)( desecnding )\n\n";
a.display();
getch();
}



Saturday, September 20, 2014

Data Structure Programs

Read Also:  


"All Data Structure Programs up-till now"

To get code Click on that program heading :) ☺ ☻ 

 ALL Type Of Sorting Click here :: most important :: 

-------------------

`Queues 

----------------

`Single Link-List:

*  How to create single Link-list in C++

*  Insertion at any point in link-list in C++

*  Concatenate or Merge two Link-lists in C++

Deletion At any point in Circular Link List in C++
------------------------------------

`Double Link-list:

=================

Stacks Data Structure:


Decimal to binary conversion using stack in c++
============ 

How to add two matrix size is define by use






Program to print counting 10 to 1 using Recursion

--------------------------------------------------