Académique Documents
Professionnel Documents
Culture Documents
/*
* C++ Program to Implement Heap Sort
*/
#include <iostream>
#include <conio.h>
using namespace std;
void max_heapify(int *a, int i, int n)
{
int j, temp;
temp = a[i];
j = 2*i;
while (j <= n)
{
if (j < n && a[j+1] > a[j])
j = j+1;
if (temp > a[j])
break;
else if (temp <= a[j])
{
a[j/2] = a[j];
j = 2*j;
}
}
a[j/2] = temp;
return;
}
void heapsort(int *a, int n)
{
int i, temp;
while(1)
{
if( a[c] > a[p])
{
int t=a[c];
a[c]=a[p];
a[p]=t;
}
c=p;
p=p/2;
if(p<1)
{
break;
}
}
}
cout<<endl<<"Heap ..."<<endl;
for(i=1;i<=size;i++)
{
cout<<endl;
cout<<"Arr["<<i<<"] :"<<a[i];
}
int j=size;
int lc,rc;
while(j>1)
{
if(a[1] > a[j])
{
int t=a[1];
a[1]=a[j];
a[j]=t;
j--;
}
else
{
j--;
continue;
}
p=1;
while(p < j)
{
lc=p*2;
rc=p*2 + 1;
if(lc>=j || rc >=j)
{
break;
}
if(a[p] < a[lc] && a[lc] > a[rc])
{
int temp=a[lc];
a[lc]=a[p];
a[p]=temp;
p=lc;
}
elseif (a[p] < a[rc] && a[rc] > a[lc])
{
int temp=a[rc];
a[rc]=a[p];
a[p]=temp;
p=rc;
}
else
{
break;
}
}
}
cout<<endl<<"Sorted List ..."<<endl;
for(i=1;i<=size;i++)
{
cout<<endl;
cout<<"Arr["<<i<<"] :"<<a[i];
}
getch();
}
//////////////////////////////////////////
QUICK SORTING
#include <iostream>
using namespace std;
void quickSort(int *,int,int);
int partition(int *, int,int);
int main()
{
int A[10]={6,10,13,5,8,3,2,25,4,11};
int p=0,q=10;
cout<<"======Original======="<<endl;
for(int f=0; f<10; f++)
cout<<A[f]<<endl;
quickSort(A,p,q);
cout<<"======Sorted======="<<endl;
for(int f=0; f<10; f++)
cout<<A[f]<<endl;
}
void quickSort(int *A, int p,int q)
{
int r;
if(p<q)
{
r=partition(A, p,q);
quickSort(A,p,(r-1)); //I think the problem is here this first quickSort
call
// is reducing the value of r and hence value of q
becomes
{
cout<<"\t\tposition : "<<(n+1)<<"\t"<<a[n]<<"\n";
}
getch();
}
void srt(int k[20],int lb,int ub)
{
int i,j,key,flag=0,temp;
clrscr();
if (lb<ub)
{
i=lb;
j=ub+1;
key=k[i];
while(flag!=1)
{
i++;
while(k[i]<key)
{
i++;
}
j--;
while(k[j]>key)
{
j--;
}
if (i<j)
{
temp=k[i];
k[i]=k[j];
k[j]=temp;
}
else
{
flag=1;
temp=k[lb];
k[lb]=k[j];
k[j]=temp;
}
}
srt(k,lb,j-1);
srt(k,j+1,ub);
}
}
//////////////////////////////////////////////////
MERGE SORTING
#include <iostream>
void quickSort(int a[], int first, int last);
int pivot(int a[], int first, int last);
void swap(int& a, int& b);
void swapNoTemp(int& a, int& b);
void print(int array[], const int& N);
}
swap(a[p], a[first]);
return p;
}
/**
* Swap the parameters.
* @param a - The first parameter.
* @param b - The second parameter.
*/
void swap(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
/**
* Swap the parameters without a temp variable.
* Warning! Prone to overflow/underflow.
* @param a - The first parameter.
* @param b - The second parameter.
*/
void swapNoTemp(int& a, int& b)
{
a -= b;
b += a;// b gets the original value of a
a = (b - a);// a gets the original value of b
}
/**
* Print an array.
* @param a - The array.
* @param N - The size of the array.
*/
void print(int a[], const int& N)
{
for(int i = 0 ; i < N ; i++)
cout << "array[" << i << "] = " << a[i] << endl;
}
////////////////////////////////////////////////////////////////////////////////
///////////////////////////
/*
* C++ Program to Implement Merge Sort
*/
#include <iostream>
using namespace std;
#include <conio.h>
void merge(int *,int, int , int );
j++;
}
}
while (i <= mid)
{
c[k] = a[i];
k++;
i++;
}
while (j <= high)
{
c[k] = a[j];
k++;
j++;
}
for (i = low; i < k; i++)
{
a[i] = c[i];
}
}
int main()
{
int a[20], i, b[20];
cout<<"enter the elements\n";
for (i = 0; i < 5; i++)
{
cin>>a[i];
}
mergesort(a, 0, 4);
cout<<"sorted array\n";
Output
enter the elements
78
45
80
32
67
sorted array
32
45
67
78
80
///////////////////////////////////////////////////////
BUBBLE SORTING
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[100],i,n,p,j,temp;
cout<<"\n------------ BUBBLE SORT ------------ \n\n";
cout<<"Enter No. of Elements : ";
cin>>n;
cout<<"\nEnter Elements : \n";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
for(p=1;p<=n-1;p++)
{
for(j=1;j<=n-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
// Interchange Values
}
cout<<"\nAfter Sorting : \n";
for(i=1;i<=n;i++)
{
cout<<a[i]<<endl;
}
getch();
}
//////////////////////////////////
BUBBLE SORTING
#include <iostream.h>
#include <conio.h>
#define MAX 10
class bubsort{
int arr[MAX],n;
public:
void getdata();
void showdata();
void sortLogic();
};
void bubsort :: getdata(){
cout<<"How many elements you require : ";
cin>>n;
for(int i=0;i<n;i++)
cin>>arr[i];
}
void bubsort :: showdata(){
cout<<"\n--Display--\n";
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
}
void bubsort :: sortLogic(){
int temp;
for(int i=0;i<n;i++){
for(int j=0,exchange=0;j<n;j++){
if(arr[j] > arr[j+1]){
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
exchange++;
cout<<"\n arr[j] = "<<arr[j]<<" arr[j+1] = "<<arr[j+1];
}
}
cout<<endl;
if(exchange==0)
break;
}
}
void main(){
clrscr();
cout<<"\n*****Bubble Sort*****\n";
bubsort obj;
obj.getdata();
obj.sortLogic();
obj.showdata();
getch();
}
//////////////////////////////////////////////////////////////////////
BUBBLE SORTING
/*
* File: bubble_sort.cpp
* Author: MyCodingLab
* Code: bubble sort algorithm
*/
#include <cstdlib>
#include <iostream>
using namespace std;
void print_array(int array[], int size) {
cout<< "buble sort steps: ";
int j;
for (j=0; j<size;j++)
cout <<" "<< array[j];
cout << endl;
}//end of print_array