Skip to content

Commit 54f0c87

Browse files
authored
Create Heap Sort in C++ (#364)
* Create Heap Sort in C++ * Codeforces solution of problem 49A
1 parent a890514 commit 54f0c87

File tree

2 files changed

+105
-0
lines changed

2 files changed

+105
-0
lines changed

49a.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
int main(){
4+
5+
string s;
6+
getline(cin,s);
7+
8+
int l=s.length();
9+
int f=0;
10+
for (int i=l;i=0;i--){
11+
if (s[i]!=' ' || s[i]!='?'){
12+
if (s[i]=='A' || s[i]=='E' || s[i]=='I' || s[i]=='O' || s[i]=='U' || s[i]=='Y' || s[i]=='a' || s[i]=='e' || s[i]=='i' || s[i]=='o' || s[i]=='u' || s[i]=='y' ){
13+
f=1;
14+
break;
15+
16+
}
17+
else
18+
f=0;}}
19+
20+
if (f==1){
21+
cout<<"YES"<<endl;
22+
}
23+
else
24+
cout<<"NO"<<endl;
25+
26+
27+
return 0;
28+
}

CPP/sorting/Heap Sort in C++

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
// To heapify a subtree rooted with node i
5+
// which is an index in arr[].
6+
// n is size of heap
7+
void heapify(int arr[], int N, int i)
8+
{
9+
10+
// Initialize largest as root
11+
int largest = i;
12+
13+
// left = 2*i + 1
14+
int l = 2 * i + 1;
15+
16+
// right = 2*i + 2
17+
int r = 2 * i + 2;
18+
19+
// If left child is larger than root
20+
if (l < N && arr[l] > arr[largest])
21+
largest = l;
22+
23+
// If right child is larger than largest
24+
// so far
25+
if (r < N && arr[r] > arr[largest])
26+
largest = r;
27+
28+
// If largest is not root
29+
if (largest != i) {
30+
swap(arr[i], arr[largest]);
31+
32+
// Recursively heapify the affected
33+
// sub-tree
34+
heapify(arr, N, largest);
35+
}
36+
}
37+
38+
// Main function to do heap sort
39+
void heapSort(int arr[], int N)
40+
{
41+
42+
// Build heap (rearrange array)
43+
for (int i = N / 2 - 1; i >= 0; i--)
44+
heapify(arr, N, i);
45+
46+
// One by one extract an element
47+
// from heap
48+
for (int i = N - 1; i > 0; i--) {
49+
50+
// Move current root to end
51+
swap(arr[0], arr[i]);
52+
53+
// call max heapify on the reduced heap
54+
heapify(arr, i, 0);
55+
}
56+
}
57+
58+
// A utility function to print array of size n
59+
void printArray(int arr[], int N)
60+
{
61+
for (int i = 0; i < N; ++i)
62+
cout << arr[i] << " ";
63+
cout << "\n";
64+
}
65+
66+
// Driver's code
67+
int main()
68+
{
69+
int arr[] = { 12, 11, 13, 5, 6, 7 };
70+
int N = sizeof(arr) / sizeof(arr[0]);
71+
72+
// Function call
73+
heapSort(arr, N);
74+
75+
cout << "Sorted array is \n";
76+
printArray(arr, N);
77+
}

0 commit comments

Comments
 (0)