Mergeshort solution(1)
#include<iostream.h>
#include<conio.h>
int arr[10];
int brr[20];
int L=1;
void merge_function(int arr[],int L1,int LB1,int L2,int LB2,int brr[],int LB3)
{
int a=1,b=1,j=LB3;
while((a<=L1)&&(b<=L2))
{
if(arr[LB1]<arr[LB2])
{
brr[j]=arr[LB1];
a++;
j++;
LB1++;
}
else
{
brr[j]=arr[LB2];
b++;
j++;
LB2++;
}
}
if(b>L2)
{
while(a<=L1)
{
brr[j]=arr[LB1];
a++;
j++;
LB1++;
}
}
if(a>L1)
{
while(b<=L2)
{
brr[j]=arr[LB2];
b++;
j++;
LB2++;
}
}
}
void merge_pass(int arr[],int n,int L,int brr[])
{
int s,r,q,LB;
q=int(n/(2*L));
s=2*L*q;
r=n-s;
for(int j=1;j<=q;j++)
{
LB=1+(j-1)*2*L;
merge_function(arr,L,LB,L,LB+L,brr,LB);
}
if(r<=L)
{
for(int k=1;k<=r;k++)
{
brr[s+k]=arr[s+k];
}
}
else
{
merge_function(arr,L,s+1,r-L,s+L+1,brr,s+1);
}
}
void main()
{
int n;
clrscr();
cout<<" how many data do you want to take:";
cin>>n;
cout<<"\n enter the data:";
for(int i=1;i<=n;i++)
cin>>arr[i];
while(L<n)
{
merge_pass(arr,n,L,brr);
merge_pass(brr,n,2*L,arr);
L=L*4;
}
cout<<"\n the sorted array: ";
for(int m=1;m<=n;m++)
cout<<arr[m]<<endl;
getch();
}
Mergeshort solution(2)
#include<iostream.h>
#include<conio.h>
int arr[10];
int brr[20];
int L=1;
void merge_function(int arr[],int L1,int LB1,int L2,int LB2,int brr[],int LB3)
{
int a=1,b=1,j=LB3;
while((a<=L1)&&(b<=L2))
{
if(arr[LB1]<arr[LB2])
{
brr[j]=arr[LB1];
a++;
j++;
LB1++;
}
else
{
brr[j]=arr[LB2];
b++;
j++;
LB2++;
}
}
if(b>L2)
{
while(a<=L1)
{
brr[j]=arr[LB1];
a++;
j++;
LB1++;
}
}
if(a>L1)
{
while(b<=L2)
{
brr[j]=arr[LB2];
b++;
j++;
LB2++;
}
}
}
void merge_pass(int arr[],int n,int L,int brr[])
{
int s,r,q,LB;
q=int(n/(2*L));
s=2*L*q;
r=n-s;
for(int j=1;j<=q;j++)
{
LB=1+(j-1)*2*L;
merge_function(arr,L,LB,L,LB+L,brr,LB);
}
if(r<=L)
{
for(int k=1;k<=r;k++)
{
brr[s+k]=arr[s+k];
}
}
else
{
merge_function(arr,L,s+1,r-L,s+L+1,brr,s+1);
}
}
void main()
{
int n;
clrscr();
cout<<" how many data do you want to take:";
cin>>n;
cout<<"\n enter the data:";
for(int i=1;i<=n;i++)
cin>>arr[i];
while(L<n)
{
merge_pass(arr,n,L,brr);
merge_pass(brr,n,2*L,arr);
L=L*4;
}
cout<<"\n the sorted array: ";
for(int m=1;m<=n;m++)
cout<<arr[m]<<endl;
getch();
}
Mergeshort solution(3)
#include<stdio.h>
#include<conio.h>
void main()
{
int A[20], N, Temp, i, j;
clrscr();
printf("\n\n\t ENTER THE NUMBER OF TERMS...: ");
scanf("%d", &N);
printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:");
for(i=0; i<N; i++)
{
gotoxy(25,11+i);
scanf("\n\t\t%d", &A[i]);
}
for(i=1; i<N; i++)
{
Temp = A[i];
j = i-1;
while(Temp<A[j] && j>=0)
{
A[j+1] = A[j];
j = j-1;
}
A[j+1] = Temp;
}
printf("\n\tTHE ASCENDING ORDER LIST IS...:\n");
for(i=0; i<N; i++)
printf("\n\t\t\t%d", A[i]);
getch();
}
#include <iostream.h>
#include<conio.h>
const int MAX = 30 ;
class array
{
private :
int arr[MAX] ;
int count ;
public :
array( ) ;
void add ( int item ) ;
int getcount( ) ;
static int split ( int *, int, int ) ;
void quiksort ( int lower, int upper ) ;
void display( ) ;
} ;
array :: array( )
{
count = 0 ;
for ( int i = 0 ; i < MAX ; i++ )
arr[i] = 0 ;
}
void array :: add ( int item )
{
if ( count < MAX )
{
arr[count] = item ;
count++ ;
}
else
cout << "\nArray is full" << endl ;
}
int array :: getcount( )
{
return count ;
}
void array :: quiksort ( int lower, int upper )
{
if ( upper > lower )
{
int i = split ( arr, lower, upper ) ;
quiksort ( lower, i - 1 ) ;
quiksort ( i + 1, upper ) ;
}
}
int array :: split ( int *a, int lower, int upper )
{
int i, p, q, t ;
p = lower + 1 ;
q = upper ;
i = a[lower] ;
while ( q >= p )
{
while ( a[p] < i )
p++ ;
while ( a[q] > i )
q-- ;
if ( q > p )
{
t = a[p] ;
a[p] = a[q] ;
a[q] = t ;
}
}
t = a[lower] ;
a[lower] = a[q] ;
a[q] = t ;
return q ;
}
void array :: display( )
{
for ( int i = 0 ; i < count ; i++ )
cout << arr[i] << " " ;
cout << endl ;
}
void main( )
{
clrscr();
array a ;
int data,n;
cout<<"Enter no of Data to be sorted = ";
cin>>n;
cout<<"\nEnter Data Items..............";
for(int u=0;u<n;u++)
{
cin>>data;
a.add(data);
}
cout << "\nQuik sort.\n" ;
cout << "\nArray before sorting:" << endl ;
a.display( ) ;
int c = a.getcount( ) ;
a.quiksort ( 0, c - 1 ) ;
cout << "\nArray after quick sorting:" << endl ;
a.display( ) ;
getch();
}
#include<iostream.h>
#include<conio.h>
int arr[10];
int brr[20];
int L=1;
void merge_function(int arr[],int L1,int LB1,int L2,int LB2,int brr[],int LB3)
{
int a=1,b=1,j=LB3;
while((a<=L1)&&(b<=L2))
{
if(arr[LB1]<arr[LB2])
{
brr[j]=arr[LB1];
a++;
j++;
LB1++;
}
else
{
brr[j]=arr[LB2];
b++;
j++;
LB2++;
}
}
if(b>L2)
{
while(a<=L1)
{
brr[j]=arr[LB1];
a++;
j++;
LB1++;
}
}
if(a>L1)
{
while(b<=L2)
{
brr[j]=arr[LB2];
b++;
j++;
LB2++;
}
}
}
void merge_pass(int arr[],int n,int L,int brr[])
{
int s,r,q,LB;
q=int(n/(2*L));
s=2*L*q;
r=n-s;
for(int j=1;j<=q;j++)
{
LB=1+(j-1)*2*L;
merge_function(arr,L,LB,L,LB+L,brr,LB);
}
if(r<=L)
{
for(int k=1;k<=r;k++)
{
brr[s+k]=arr[s+k];
}
}
else
{
merge_function(arr,L,s+1,r-L,s+L+1,brr,s+1);
}
}
void main()
{
int n;
clrscr();
cout<<" how many data do you want to take:";
cin>>n;
cout<<"\n enter the data:";
for(int i=1;i<=n;i++)
cin>>arr[i];
while(L<n)
{
merge_pass(arr,n,L,brr);
merge_pass(brr,n,2*L,arr);
L=L*4;
}
cout<<"\n the sorted array: ";
for(int m=1;m<=n;m++)
cout<<arr[m]<<endl;
getch();
}
Mergeshort solution(2)
#include<iostream.h>
#include<conio.h>
int arr[10];
int brr[20];
int L=1;
void merge_function(int arr[],int L1,int LB1,int L2,int LB2,int brr[],int LB3)
{
int a=1,b=1,j=LB3;
while((a<=L1)&&(b<=L2))
{
if(arr[LB1]<arr[LB2])
{
brr[j]=arr[LB1];
a++;
j++;
LB1++;
}
else
{
brr[j]=arr[LB2];
b++;
j++;
LB2++;
}
}
if(b>L2)
{
while(a<=L1)
{
brr[j]=arr[LB1];
a++;
j++;
LB1++;
}
}
if(a>L1)
{
while(b<=L2)
{
brr[j]=arr[LB2];
b++;
j++;
LB2++;
}
}
}
void merge_pass(int arr[],int n,int L,int brr[])
{
int s,r,q,LB;
q=int(n/(2*L));
s=2*L*q;
r=n-s;
for(int j=1;j<=q;j++)
{
LB=1+(j-1)*2*L;
merge_function(arr,L,LB,L,LB+L,brr,LB);
}
if(r<=L)
{
for(int k=1;k<=r;k++)
{
brr[s+k]=arr[s+k];
}
}
else
{
merge_function(arr,L,s+1,r-L,s+L+1,brr,s+1);
}
}
void main()
{
int n;
clrscr();
cout<<" how many data do you want to take:";
cin>>n;
cout<<"\n enter the data:";
for(int i=1;i<=n;i++)
cin>>arr[i];
while(L<n)
{
merge_pass(arr,n,L,brr);
merge_pass(brr,n,2*L,arr);
L=L*4;
}
cout<<"\n the sorted array: ";
for(int m=1;m<=n;m++)
cout<<arr[m]<<endl;
getch();
}
Mergeshort solution(3)
#include"stdio.h"
#include"conio.h"
#include"iostream.h"
#define N 10
void merge(int,int,int);
int arr[N],brr[N],n;
void mergesort(int low,int high)
{
if(low<high)
{
int mid=(low+high)/2;
mergesort(low,mid);
mergesort(mid+1,high);
merge(low,mid,high);
}
}
void merge(int low,int mid,int high)
{
int h=low;
int i=low;
int j=mid+1;
while(h<=mid && j<=high)
{
if(arr[h]<=arr[j])
{
brr[i]=arr[h];
h=h+1;
}
else
{
brr[i]=arr[j];
j=j+1;
}
i=i+1;
}
if(h>mid)
for(int k=j;k<=high;k++)
{
brr[i]=arr[k];
i=i+1;
}
else
{
for(int k=h;k<=mid;k++)
{brr[i]=arr[k];
i=i+1; }
}
for(int k=1;k<=high;k++)
arr[k]=brr[k];
}
void main()
{
clrscr();
cout<<"Number of Element=";
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>arr[i];
}
int l=1,h=n;
mergesort(l,h);
for(i=1;i<=n;i++)
{
cout<<arr[i]<<endl;
}
getch();
}
#include"conio.h"
#include"iostream.h"
#define N 10
void merge(int,int,int);
int arr[N],brr[N],n;
void mergesort(int low,int high)
{
if(low<high)
{
int mid=(low+high)/2;
mergesort(low,mid);
mergesort(mid+1,high);
merge(low,mid,high);
}
}
void merge(int low,int mid,int high)
{
int h=low;
int i=low;
int j=mid+1;
while(h<=mid && j<=high)
{
if(arr[h]<=arr[j])
{
brr[i]=arr[h];
h=h+1;
}
else
{
brr[i]=arr[j];
j=j+1;
}
i=i+1;
}
if(h>mid)
for(int k=j;k<=high;k++)
{
brr[i]=arr[k];
i=i+1;
}
else
{
for(int k=h;k<=mid;k++)
{brr[i]=arr[k];
i=i+1; }
}
for(int k=1;k<=high;k++)
arr[k]=brr[k];
}
void main()
{
clrscr();
cout<<"Number of Element=";
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>arr[i];
}
int l=1,h=n;
mergesort(l,h);
for(i=1;i<=n;i++)
{
cout<<arr[i]<<endl;
}
getch();
}
Selection Sort
#include<iostream.h>
#include<conio.h>
int arr[10];
int n,loc,temp,count=0;
void minimum(int k)
{
int min=arr[k];
loc=k;
for(int j=k+1;j<=n;j++)
{
if(min>arr[j])
{
min=arr[j];
loc=j;
}
count++;
}
}
void selection()
{
for(int i=1;i<n;i++)
{
minimum(i);
temp=arr[i];
arr[i]=arr[loc];
arr[loc]=temp;
}
}
void main()
{
clrscr();
cout<<"\n\thow many data do you want to take:";
cin>>n;
cout<<"\n\t enter the data:";
for(int i=1;i<=n;i++)
cin>>arr[i];
selection();
cout<<"\n\t the sorted array:";
for(int j=1;j<=n;j++)
cout<<arr[j];
cout<<"\n\t the number of comparision:"<<count;
getch();
}
#include<conio.h>
int arr[10];
int n,loc,temp,count=0;
void minimum(int k)
{
int min=arr[k];
loc=k;
for(int j=k+1;j<=n;j++)
{
if(min>arr[j])
{
min=arr[j];
loc=j;
}
count++;
}
}
void selection()
{
for(int i=1;i<n;i++)
{
minimum(i);
temp=arr[i];
arr[i]=arr[loc];
arr[loc]=temp;
}
}
void main()
{
clrscr();
cout<<"\n\thow many data do you want to take:";
cin>>n;
cout<<"\n\t enter the data:";
for(int i=1;i<=n;i++)
cin>>arr[i];
selection();
cout<<"\n\t the sorted array:";
for(int j=1;j<=n;j++)
cout<<arr[j];
cout<<"\n\t the number of comparision:"<<count;
getch();
}
Bubble Sort
#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
int item[10];
int count;
cout << "Enter number of item : ";
cin >> count;
for(int i=0; i<count; i++)
cin >> item[i];
int a, b, t;
for(a=1; a<=count; a++)
for(b=count-1; b>=a; b--) {
if(item[b-1] > item[b]) {
t= item[b-1];
item[b-1] = item[b];
item[b] = t;
}
}
for( i=0; i<count; i++)
cout << item[i] << " ";
cout << "\n";
getch();
}
#include <conio.h>
int main()
{
clrscr();
int item[10];
int count;
cout << "Enter number of item : ";
cin >> count;
for(int i=0; i<count; i++)
cin >> item[i];
int a, b, t;
for(a=1; a<=count; a++)
for(b=count-1; b>=a; b--) {
if(item[b-1] > item[b]) {
t= item[b-1];
item[b-1] = item[b];
item[b] = t;
}
}
for( i=0; i<count; i++)
cout << item[i] << " ";
cout << "\n";
getch();
}
Insertion sort
#include<stdio.h>
#include<conio.h>
void main()
{
int A[20], N, Temp, i, j;
clrscr();
printf("\n\n\t ENTER THE NUMBER OF TERMS...: ");
scanf("%d", &N);
printf("\n\t ENTER THE ELEMENTS OF THE ARRAY...:");
for(i=0; i<N; i++)
{
gotoxy(25,11+i);
scanf("\n\t\t%d", &A[i]);
}
for(i=1; i<N; i++)
{
Temp = A[i];
j = i-1;
while(Temp<A[j] && j>=0)
{
A[j+1] = A[j];
j = j-1;
}
A[j+1] = Temp;
}
printf("\n\tTHE ASCENDING ORDER LIST IS...:\n");
for(i=0; i<N; i++)
printf("\n\t\t\t%d", A[i]);
getch();
}
Quick sort
#include <iostream.h>
#include<conio.h>
const int MAX = 30 ;
class array
{
private :
int arr[MAX] ;
int count ;
public :
array( ) ;
void add ( int item ) ;
int getcount( ) ;
static int split ( int *, int, int ) ;
void quiksort ( int lower, int upper ) ;
void display( ) ;
} ;
array :: array( )
{
count = 0 ;
for ( int i = 0 ; i < MAX ; i++ )
arr[i] = 0 ;
}
void array :: add ( int item )
{
if ( count < MAX )
{
arr[count] = item ;
count++ ;
}
else
cout << "\nArray is full" << endl ;
}
int array :: getcount( )
{
return count ;
}
void array :: quiksort ( int lower, int upper )
{
if ( upper > lower )
{
int i = split ( arr, lower, upper ) ;
quiksort ( lower, i - 1 ) ;
quiksort ( i + 1, upper ) ;
}
}
int array :: split ( int *a, int lower, int upper )
{
int i, p, q, t ;
p = lower + 1 ;
q = upper ;
i = a[lower] ;
while ( q >= p )
{
while ( a[p] < i )
p++ ;
while ( a[q] > i )
q-- ;
if ( q > p )
{
t = a[p] ;
a[p] = a[q] ;
a[q] = t ;
}
}
t = a[lower] ;
a[lower] = a[q] ;
a[q] = t ;
return q ;
}
void array :: display( )
{
for ( int i = 0 ; i < count ; i++ )
cout << arr[i] << " " ;
cout << endl ;
}
void main( )
{
clrscr();
array a ;
int data,n;
cout<<"Enter no of Data to be sorted = ";
cin>>n;
cout<<"\nEnter Data Items..............";
for(int u=0;u<n;u++)
{
cin>>data;
a.add(data);
}
cout << "\nQuik sort.\n" ;
cout << "\nArray before sorting:" << endl ;
a.display( ) ;
int c = a.getcount( ) ;
a.quiksort ( 0, c - 1 ) ;
cout << "\nArray after quick sorting:" << endl ;
a.display( ) ;
getch();
}
Is This Helpful For You ????????????????????????????
কোন মন্তব্য নেই:
একটি মন্তব্য পোস্ট করুন