forked from ephremdeme/data-structure-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ephremdeme#164 from Palak-137/heapsort
Heapsort in cpp with better implementation using classes
- Loading branch information
Showing
2 changed files
with
111 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
|
||
|
||
|
||
|