Skip to content

Commit

Permalink
Merge pull request ephremdeme#164 from Palak-137/heapsort
Browse files Browse the repository at this point in the history
Heapsort in cpp with better implementation using classes
  • Loading branch information
ephremdeme authored Oct 2, 2020
2 parents 83106a0 + af70380 commit 7242d85
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 63 deletions.
41 changes: 41 additions & 0 deletions Algorithms/KadaneAlgorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <iostream>

using namespace std;


class Kadane{
public:
int max_sum(int p[],int n){
int max_sum=p[0],max_in=p[0];
for(int i=1;i<n;i++){
max_sum = max(p[i],max_sum+p[i]);
max_in = max(max_in,max_sum);
}
return max_in;
}

void display(int p[],int n){
cout<<"Element in array "<<endl;
for(int i=0;i<n;i++){
cout<<p[i]<<" ";
}
}
};


int main()
{
Kadane algo;
int n;
cout<<"Enter the size of array ";
cin>>n;
int p[n];
for (int i=0;i<n;i++){
cout<<"Enter the element in array ";
cin>>p[i];
}
algo.display(p,n);
int a = algo.max_sum(p,n);
cout<<"\n Largest sum of subarray using kadane algorithm is "<<a;
return 0;
}
133 changes: 70 additions & 63 deletions sorting-algorithms/heapsort.cpp
Original file line number Diff line number Diff line change
@@ -1,67 +1,74 @@
#include<bits/stdc++.h>

using namespace std ;




void heapify(int arr[], int n, int i)
{
int largest = i;
int l = 2*i + 1;
int r = 2*i + 2;

if (l < n && arr[l] > arr[largest])
largest = l;

if (r < n && arr[r] > arr[largest])
largest = r;

if (largest != i)
{
swap(arr[i], arr[largest]);
heapify(arr, n, largest);
}
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;


void heapify(int n,int p[],int i)
{
//intilizing root
int root = i;
int left = i*2+1;
int right = i*2+2;


//finding largest element
if(p[right]>p[root]&& right<n){
root = right;
}
if(p[left]>p[root]&& left<n){
root = left;
}
if(root != i){
int temp = p[i];
p[i] = p[root];
p[root] = temp;


heapify(n,p,root);

}
}

void heap_sort(int p[],int n)
{
//starting heapify from bottom
for(int i=n/2-1;i>=0;i--){
heapify(n,p,i);
}
//deleting element from heap
for(int i= n-1;i>0;i--)
{
//swaping to the root
int temp= p[0];
p[0] = p[i];
p[i] = temp;
//now heapify again
heapify(i,p,0);
}

}

void heapSort(int arr[], int n)
{

for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);


for (int i=n-1; i>0; i--)
{

swap(arr[0], arr[i]);


heapify(arr, i, 0);
}
}
int main()
{
int n ;
cout<<"Enter Size of Array\n" ;
cin>>n ;
cout<<"Enter Numbers\n" ;
int arr[n] ;
for(int i = 0 ; i<n ; i++)
{
cin>>arr[i] ;
}
cout<<"Unsorted Array: " ;
for(int i = 0 ; i<n ; i++)
{
cout<<arr[i]<<" " ;
}
cout<<endl ;
cout<<"Sorted Array: " ;
heapSort(arr ,n ) ;
for(int i = 0 ; i<n ; i++)
{
cout<<arr[i]<<" " ;
}

}
int n;
cout <<"enter the size of array";
cin >>n;
int p[n];
for(int i=0;i<n;i++){
cout <<"Enter the element in array : " ;
cin >>p[i];
}

heap_sort(p,n);

for(int i=0;i<n;i++){
cout<<p[i]<<" ";
}
return 0;
}





0 comments on commit 7242d85

Please sign in to comment.