Skip to content

Commit 633983e

Browse files
Multiple Input in min Heap Implemented
1 parent ec32e6a commit 633983e

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

input.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
5
2+
10 60 40 50 30

input_in_min_heap.cpp

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int main()
5+
{
6+
vector<int> v;
7+
int n;
8+
cin >> n;
9+
10+
while (n--)
11+
{
12+
int x;
13+
cin >> x;
14+
v.push_back(x);
15+
int current_idx = v.size() - 1;
16+
17+
while (current_idx != 0)
18+
{
19+
int parent_idx = (current_idx - 1) / 2;
20+
if (v[parent_idx] > v[current_idx])
21+
swap(v[parent_idx], v[current_idx]);
22+
else
23+
break;
24+
current_idx = parent_idx;
25+
}
26+
}
27+
28+
for (int val : v)
29+
cout << val << " ";
30+
31+
return 0;
32+
}

input_in_min_heap.exe

77.3 KB
Binary file not shown.

output.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
10 30 40 60 50

0 commit comments

Comments
 (0)