Skip to content

Commit 7242d85

Browse files
authored
Merge pull request ephremdeme#164 from Palak-137/heapsort
Heapsort in cpp with better implementation using classes
2 parents 83106a0 + af70380 commit 7242d85

File tree

2 files changed

+111
-63
lines changed

2 files changed

+111
-63
lines changed

Algorithms/KadaneAlgorithm.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
6+
class Kadane{
7+
public:
8+
int max_sum(int p[],int n){
9+
int max_sum=p[0],max_in=p[0];
10+
for(int i=1;i<n;i++){
11+
max_sum = max(p[i],max_sum+p[i]);
12+
max_in = max(max_in,max_sum);
13+
}
14+
return max_in;
15+
}
16+
17+
void display(int p[],int n){
18+
cout<<"Element in array "<<endl;
19+
for(int i=0;i<n;i++){
20+
cout<<p[i]<<" ";
21+
}
22+
}
23+
};
24+
25+
26+
int main()
27+
{
28+
Kadane algo;
29+
int n;
30+
cout<<"Enter the size of array ";
31+
cin>>n;
32+
int p[n];
33+
for (int i=0;i<n;i++){
34+
cout<<"Enter the element in array ";
35+
cin>>p[i];
36+
}
37+
algo.display(p,n);
38+
int a = algo.max_sum(p,n);
39+
cout<<"\n Largest sum of subarray using kadane algorithm is "<<a;
40+
return 0;
41+
}

sorting-algorithms/heapsort.cpp

Lines changed: 70 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,74 @@
1-
#include<bits/stdc++.h>
2-
3-
using namespace std ;
4-
5-
6-
7-
8-
void heapify(int arr[], int n, int i)
9-
{
10-
int largest = i;
11-
int l = 2*i + 1;
12-
int r = 2*i + 2;
13-
14-
if (l < n && arr[l] > arr[largest])
15-
largest = l;
16-
17-
if (r < n && arr[r] > arr[largest])
18-
largest = r;
19-
20-
if (largest != i)
21-
{
22-
swap(arr[i], arr[largest]);
23-
heapify(arr, n, largest);
24-
}
1+
#include <iostream>
2+
using namespace std;
3+
#include <iostream>
4+
using namespace std;
5+
6+
7+
void heapify(int n,int p[],int i)
8+
{
9+
//intilizing root
10+
int root = i;
11+
int left = i*2+1;
12+
int right = i*2+2;
13+
14+
15+
//finding largest element
16+
if(p[right]>p[root]&& right<n){
17+
root = right;
18+
}
19+
if(p[left]>p[root]&& left<n){
20+
root = left;
21+
}
22+
if(root != i){
23+
int temp = p[i];
24+
p[i] = p[root];
25+
p[root] = temp;
26+
27+
28+
heapify(n,p,root);
29+
30+
}
31+
}
32+
33+
void heap_sort(int p[],int n)
34+
{
35+
//starting heapify from bottom
36+
for(int i=n/2-1;i>=0;i--){
37+
heapify(n,p,i);
38+
}
39+
//deleting element from heap
40+
for(int i= n-1;i>0;i--)
41+
{
42+
//swaping to the root
43+
int temp= p[0];
44+
p[0] = p[i];
45+
p[i] = temp;
46+
//now heapify again
47+
heapify(i,p,0);
48+
}
49+
2550
}
2651

27-
void heapSort(int arr[], int n)
28-
{
29-
30-
for (int i = n / 2 - 1; i >= 0; i--)
31-
heapify(arr, n, i);
32-
33-
34-
for (int i=n-1; i>0; i--)
35-
{
36-
37-
swap(arr[0], arr[i]);
38-
39-
40-
heapify(arr, i, 0);
41-
}
42-
}
4352
int main()
4453
{
45-
int n ;
46-
cout<<"Enter Size of Array\n" ;
47-
cin>>n ;
48-
cout<<"Enter Numbers\n" ;
49-
int arr[n] ;
50-
for(int i = 0 ; i<n ; i++)
51-
{
52-
cin>>arr[i] ;
53-
}
54-
cout<<"Unsorted Array: " ;
55-
for(int i = 0 ; i<n ; i++)
56-
{
57-
cout<<arr[i]<<" " ;
58-
}
59-
cout<<endl ;
60-
cout<<"Sorted Array: " ;
61-
heapSort(arr ,n ) ;
62-
for(int i = 0 ; i<n ; i++)
63-
{
64-
cout<<arr[i]<<" " ;
65-
}
66-
67-
}
54+
int n;
55+
cout <<"enter the size of array";
56+
cin >>n;
57+
int p[n];
58+
for(int i=0;i<n;i++){
59+
cout <<"Enter the element in array : " ;
60+
cin >>p[i];
61+
}
62+
63+
heap_sort(p,n);
64+
65+
for(int i=0;i<n;i++){
66+
cout<<p[i]<<" ";
67+
}
68+
return 0;
69+
}
70+
71+
72+
73+
74+

0 commit comments

Comments
 (0)