Vous êtes sur la page 1sur 15

heap sorting

/*
* 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;

for (i = n; i >= 2; i--)


{
temp = a[i];
a[i] = a[1];
a[1] = temp;
max_heapify(a, 1, i - 1);
}
}
void build_maxheap(int *a, int n)
{
int i;
for(i = n/2; i >= 1; i--)
{
max_heapify(a, i, n);
}
}
int main()
{
int n, i, x;
cout<<"enter no of elements of array\n";
cin>>n;
int a[20];
for (i = 1; i <= n; i++)
{
cout<<"enter element"<<(i)<<endl;
cin>>a[i];
}
build_maxheap(a,n);
heapsort(a, n);
cout<<"sorted output\n";

for (i = 1; i <= n; i++)


{
cout<<a[i]<<endl;
}
getch();
}
(output)
Output
enter no of elements of array
7
enter element1
34
enter element2
45
enter element3
12
enter element4
40
enter element5
6
enter element6
75
enter element7
36
sorted output
6
12
34
36
40
45
75
////////////////////////////////////////////////////////
# include <iostream.h>
# include <conio.h>
void main()
{
clrscr();
int a[50],size;
int p,c;
cout<<"Enter the Size of an Array :";
cin>>size;
cout<<"Enter Value of A[1] :";
cin>>a[1];
for(int i=2;i<=size;i++)
{
cout<<"Enter Value of A["<<i<<"] :";
cin>>a[i];
p=i/2;
c=i;

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

// less than p recursively. How can I separate bot


h calls
// one for left and one for right sub array of the
pivot.
quickSort(A,(r+1),q);
}
}
int partition(int *A, int p,int q)
{
int x= A[p];
int i=p;
int temp;
int j;
for(j=p+1; j<q; j++)
{
if(A[j]<=x)
{
i=i+1;
temp= A[j];
A[j]=A[i];
A[i]=temp;
}
}
temp= A[p];
A[p]=A[i];
A[i]=temp;
return i;
}
/////////////////////////////////////////////////////////////////
QUICK SORTING
#include <iostream.h>
#include <conio.h>
void main()
{
void srt(int[],int,int);
int a[10],count=0,n;
clrscr();
cout<<"Ener 10 values in unsorted order : \n";
for (n=0;n<10;n++)
{
cout<<"value no.: "<<(n+1)<<"\t";
cin>>a[n];
count++;
}
n=0;
clrscr();
srt(a,n,count-1);
clrscr();
cout<<"\t\tThe Sorted order is : \n";
for (n=0;n<10;n++)

{
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);

using namespace std;


int main()
{
int test[] = { 7, -13, 1, 3, 10, 5, 2, 4 };
int N = sizeof(test)/sizeof(int);
cout << "Size of test array :" << N << endl;
cout << "Before sorting : " << endl;
print(test, N);
quickSort(test, 0, N-1);
cout << endl << endl << "After sorting : " << endl;
print(test, N);
return 0;
}
/**
* Quicksort.
* @param a - The array to be sorted.
* @param first - The start of the sequence to be sorted.
* @param last - The end of the sequence to be sorted.
*/
void quickSort( int a[], int first, int last )
{
int pivotElement;
if(first < last)
{
pivotElement = pivot(a, first, last);
quickSort(a, first, pivotElement-1);
quickSort(a, pivotElement+1, last);
}
}
/**
* Find and return the index of pivot element.
* @param a - The array.
* @param first - The start of the sequence.
* @param last - The end of the sequence.
* @return - the pivot element
*/
int pivot(int a[], int first, int last)
{
int p = first;
int pivotElement = a[first];
for(int i = first+1 ; i <= last ; i++)
{
/* If you want to sort the list in the other order, change "<=" to ">" *
/
if(a[i] <= pivotElement)
{
p++;
swap(a[i], a[p]);
}

}
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 );

void mergesort(int *a, int low, int high)


{
int mid;
if (low < high)
{
mid=(low+high)/2;
mergesort(a,low,mid);
mergesort(a,mid+1,high);
merge(a,low,high,mid);
}
return;
}
void merge(int *a, int low, int high, int mid)
{
int i, j, k, c[50];
i = low;
k = low;
j = mid + 1;
while (i <= mid && j <= high)
{
if (a[i] < a[j])
{
c[k] = a[i];
k++;
i++;
}
else
{
c[k] = a[j];
k++;

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";

for (i = 0; i < 5; i++)


{
cout<<a[i];
}
cout<<"enter the elements\n";
for (i = 0; i < 5; i++)
{
cin>>b[i];
}
mergesort(b, 0, 4);
cout<<"sorted array\n";
for (i = 0; i < 5; i++)
{
cout<<b[i];
}
getch();
}

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++)
{

// Loop for Pass

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

void bubble_sort(int arr[], int size) {


bool not_sorted = true;
int j=1,tmp;
while (not_sorted) {
not_sorted = false;
j++;
for (int i = 0; i < size - j; i++) {
if (arr[i] > arr[i + 1]) {
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
not_sorted = true;
}//end of if
print_array(arr,5);
}//end of for loop
}//end of while loop
}//end of bubble_sort
int main() {
int array[5]= {5,4,3,2,1};
print_array(array,5);
bubble_sort(array,5);
return 0;
}//end of main
Program output
bubble sort steps: 5 4 3 2 1
bubble sort steps: 4 5 3 2 1
bubble sort steps: 4 3 5 2 1
bubble sort steps: 4 3 2 5 1
bubble sort steps: 4 3 2 1 5
bubble sort steps: 3 4 2 1 5
bubble sort steps: 3 2 4 1 5
bubble sort steps: 3 2 1 4 5
bubble sort steps: 2 3 1 4 5
bubble sort steps: 2 1 3 4 5
bubble sort steps: 1 2 3 4 5
/////////////////////////////////////////IN
INSERTION SOSRTING

Vous aimerez peut-être aussi