Skip to content

Commit 402cf8e

Browse files
Merge pull request #137 from mujtaba1747/master
Added Heap Sort in Python and C++
2 parents f856dc4 + 6023b59 commit 402cf8e

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// C++ program for implementation of Heap Sort
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
// To heapify a subtree rooted with node i which is
7+
// an index in arr[]. n is size of heap
8+
void heapify(int arr[], int n, int i)
9+
{
10+
int largest = i; // Initialize largest as root
11+
int l = 2*i + 1; // left = 2*i + 1
12+
int r = 2*i + 2; // right = 2*i + 2
13+
14+
// If left child is larger than root
15+
if (l < n && arr[l] > arr[largest])
16+
largest = l;
17+
18+
// If right child is larger than largest so far
19+
if (r < n && arr[r] > arr[largest])
20+
largest = r;
21+
22+
// If largest is not root
23+
if (largest != i)
24+
{
25+
swap(arr[i], arr[largest]);
26+
27+
// Recursively heapify the affected sub-tree
28+
heapify(arr, n, largest);
29+
}
30+
}
31+
32+
// main function to do heap sort
33+
void heapSort(int arr[], int n)
34+
{
35+
// Build heap (rearrange array)
36+
for (int i = n / 2 - 1; i >= 0; i--)
37+
heapify(arr, n, i);
38+
39+
// One by one extract an element from heap
40+
for (int i=n-1; i>=0; i--)
41+
{
42+
// Move current root to end
43+
swap(arr[0], arr[i]);
44+
45+
// call max heapify on the reduced heap
46+
heapify(arr, i, 0);
47+
}
48+
}
49+
50+
/* A utility function to print array of size n */
51+
void printArray(int arr[], int n)
52+
{
53+
for (int i=0; i<n; ++i)
54+
cout << arr[i] << " ";
55+
cout << "\n";
56+
}
57+
int main()
58+
{
59+
int arr[] = {12, 11, 13, 5, 6, 7};
60+
int n = sizeof(arr)/sizeof(arr[0]);
61+
62+
heapSort(arr, n);
63+
64+
cout << "Sorted array is \n";
65+
printArray(arr, n);
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Python program for implementation of heap Sort
2+
3+
# To heapify subtree rooted at index i.
4+
# n is size of heap
5+
def heapify(arr, n, i):
6+
largest = i # Initialize largest as root
7+
l = 2 * i + 1 # left = 2*i + 1
8+
r = 2 * i + 2 # right = 2*i + 2
9+
10+
# See if left child of root exists and is
11+
# greater than root
12+
if l < n and arr[i] < arr[l]:
13+
largest = l
14+
15+
# See if right child of root exists and is
16+
# greater than root
17+
if r < n and arr[largest] < arr[r]:
18+
largest = r
19+
20+
# Change root, if needed
21+
if largest != i:
22+
arr[i],arr[largest] = arr[largest],arr[i] # swap
23+
24+
# Heapify the root.
25+
heapify(arr, n, largest)
26+
27+
# The main function to sort an array of given size
28+
def heapSort(arr):
29+
n = len(arr)
30+
31+
# Build a maxheap.
32+
for i in range(n, -1, -1):
33+
heapify(arr, n, i)
34+
35+
# One by one extract elements
36+
for i in range(n-1, 0, -1):
37+
arr[i], arr[0] = arr[0], arr[i] # swap
38+
heapify(arr, i, 0)
39+
40+
# Code to test above function
41+
arr = [ 12, 11, 13, 5, 6, 7]
42+
heapSort(arr)
43+
n = len(arr)
44+
print ("Sorted array is")
45+
for i in range(n):
46+
print ("%d" %arr[i]),

0 commit comments

Comments
 (0)