Skip to content

Commit 695be28

Browse files
author
Shriya Madan
authored
Merge pull request #1 from Zircoz/master
updating
2 parents d2ff15e + 95db079 commit 695be28

10 files changed

+944
-5
lines changed

Heapsort.md

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
Heap Sort Algorithm:-
2+
3+
#Heap Sort is a popular and efficient sorting algorithm in computer programming. Learning how to write the heap sort algorithm requires knowledge of two types of data structures - arrays and trees.
4+
#The initial set of numbers that we want to sort is stored in an array e.g. [10, 3, 76, 34, 23, 32] and after sorting, we get a sorted array [3,10,23,32,34,76]
5+
#Heap sort works by visualizing the elements of the array as a special kind of complete binary tree called heap.
6+
7+
What is a complete Binary Tree?
8+
9+
Binary Tree:-
10+
A binary tree is a tree data structure in which each parent node can have at most two children
11+
12+
Full Binary Tree:-
13+
A full Binary tree is a special type of binary tree in which every parent node has either two or no children.
14+
15+
Complete binary tree:-##
16+
A complete binary tree is just like a full binary tree, but with two major differences:-
17+
18+
1.Every level must be completely filled
19+
2.All the leaf elements must lean towards the left.
20+
3.The last leaf element might not have a right sibling i.e. a complete binary tree doesn’t have to be a full binary tree.
21+
22+
23+
How to create a complete binary tree from an unsorted list (array)?
24+
25+
1.Select first element of the list to be the root node. (First level - 1 element)
26+
2.Put the second element as a left child of the root node and the third element as a right child. (Second level - 2 elements)
27+
3.Put next two elements as children of left node of second level. Again, put the next two elements as children of right node of second level (3rd level - 4 elements).
28+
4.Keep repeating till you reach the last element.
29+
30+
#A complete binary tree can be built by adding elements from left to right
31+
32+
Relationship between array indexes and tree elements:-
33+
34+
Complete binary tree has an interesting property that we can use to find the children and parents of any node.
35+
If the index of any element in the array is i, the element in the index 2i+1 will become the left child and element in 2i+2 index will become the right child. Also, the parent of any element at index i is given by the lower bound of (i-1)/2.
36+
on the left, there is a graph and on the right there is an array representation of the same graph to compare equivalent indices
37+
38+
Let’s test it out,
39+
40+
Left child of 1 (index 0)
41+
= element in (2*0+1) index
42+
= element in 1 index
43+
= 12
44+
45+
Right child of 1
46+
= element in (2*0+2) index
47+
= element in 2 index
48+
= 9
49+
50+
Similarly,
51+
Left child of 12 (index 1)
52+
= element in (2*1+1) index
53+
= element in 3 index
54+
= 5
55+
56+
Right child of 12
57+
= element in (2*1+2) index
58+
= element in 4 index
59+
= 6
60+
61+
#Let us also confirm that the rules holds for finding parent of any node
62+
63+
Parent of 9 (position 2)
64+
= (2-1)/2
65+
= ½
66+
= 0.5
67+
~ 0 index
68+
= 1
69+
70+
Parent of 12 (position 1)
71+
= (1-1)/2
72+
= 0 index
73+
= 1
74+
75+
#Understanding this mapping of array indexes to tree positions is critical to understanding how the Heap Data Structure works and how it is used to implement Heap Sort.
76+
77+
What is Heap Data Structure ?
78+
79+
Heap is a special tree-based data structure. A binary tree is said to follow a heap data structure if
80+
81+
a)it is a complete binary tree
82+
b)All nodes in the tree follow the property that they are greater than their children i.e. the largest element is at the root and both its children and smaller than the root and so on. Such a heap is called a max-heap. If instead all nodes are smaller than their children, it is called a min-heap
83+
84+
Max heap Min heap comparison:-
85+
86+
How to “heapify” a tree?
87+
88+
Starting from a complete binary tree, we can modify it to become a Max-Heap by running a function called heapify on all the non-leaf elements of the heap.
89+
Since heapfiy uses recursion, it can be difficult to grasp. So let’s first think about how you would heapify a tree with just three elements.
90+
heapify(array):
91+
Root = array[0]
92+
Largest = largest( array[0] , array [2*0 + 1]. array[2*0+2])
93+
if(Root != Largest)
94+
Swap(Root, Largest)
95+
96+
#he example above shows two scenarios - one in which the root is the largest element and we don’t need to do anything. And another in which root had larger element as a child and we needed to swap to maintain max-heap property.
97+
98+
#f you’re worked with recursive algorithms before, you’ve probably identified that this must be the base case.
99+
100+
Now let’s think of another scenario in which there are more than one levels.
101+
102+
The top element isn’t a max-heap but all the sub-trees are max-heaps.
103+
104+
To maintain the max-heap property for the entire tree, we will have to keep pushing 2 downwards until it reaches its correct position.
105+
106+
#Steps to heapify root element when all its subtees are actually Max-Heaps:-
107+
108+
Thus, to maintain the max-heap property in a tree where both sub-trees are max-heaps, we need to run heapify on the root element repeatedly until it is larger than its children or it becomes a leaf node.
109+
110+
We can combine both these conditions in one heapify function as
111+
112+
void heapify(int arr[], int n, int i)
113+
{
114+
int largest = i;
115+
int l = 2*i + 1;
116+
int r = 2*i + 2;
117+
if (l < n && arr[l] > arr[largest])
118+
largest = l;
119+
if (right < n && arr[r] > arr[largest])
120+
largest = r;
121+
if (largest != i)
122+
{
123+
swap(arr[i], arr[largest]);
124+
// Recursively heapify the affected sub-tree
125+
heapify(arr, n, largest);
126+
}
127+
}
128+
129+
This function works for both the base case and for a tree of any size. We can thus move the root element to the correct position to maintain the max-heap status for any tree size as long as the sub-trees are max-heaps.
130+
Build max-heap
131+
132+
To build a max-heap from any tree, we can thus start heapifying each sub-tree from the bottom up and end up with a max-heap after the function is applied on all the elements including the root element.
133+
134+
In the case of complete tree, the first index of non-leaf node is given by n/2 - 1. All other nodes after that are leaf-nodes and thus don’t need to be heapified.
135+
136+
So, we can build a maximum heap as
137+
138+
// Build heap (rearrange array)
139+
for (int i = n / 2 - 1; i >= 0; i--)
140+
heapify(arr, n, i);
141+
142+
Steps to build MaxHeap for Heapsort:-
143+
144+
As show in the above diagram, we start by heapifying the lowest smallest trees and gradually move up until we reach the root element.
145+
146+
If you’ve understood everything till here, congratulations, you are on your way to mastering the Heap sort.
147+
Procedures to follow for Heapsort
148+
149+
Since the tree satisfies Max-Heap property, then the largest item is stored at the root node.
150+
Remove the root element and put at the end of the array (nth position) Put the last item of the tree (heap) at the vacant place.
151+
Reduce the size of the heap by 1 and heapify the root element again so that we have highest element at root.
152+
The process is repeated until all the items of the list is sorted.
153+
154+
procedures for implementing Heapsort
155+
156+
The code below shows the operation.
157+
158+
for (int i=n-1; i>=0; i--)
159+
{
160+
// Move current root to end
161+
swap(arr[0], arr[i]);
162+
// call max heapify on the reduced heap
163+
heapify(arr, i, 0);
164+
}
165+
166+
Performance:-
167+
168+
Heap Sort has O(nlogn) time complexities for all the cases ( best case, average case and worst case).
169+
170+
Let us understand the reason why. The height of a complete binary tree containing n elements is log(n)
171+
172+
As we have seen earlier, to fully heapify an element whose subtrees are already max-heaps, we need to keep comparing the element with its left and right children and pushing it downwards until it reaches a point where both its children are smaller than it.
173+
174+
In the worst case scenario, we will need to move an element from the root to the leaf node making a multiple of log(n) comparisons and swaps.
175+
176+
During the build_max_heap stage, we do that for n/2 elements so the worst case complexity of the build_heap step is n/2*log(n) ~ nlogn.
177+
178+
During the sorting step, we exchange the root element with the last element and heapify the root element. For each element, this again takes logn worst time because we might have to bring the element all the way from the root to the leaf. Since we repeat this n times, the heap_sort step is also nlogn.
179+
180+
Also since the build_max_heap and heap_sort steps are executed one after another, the algorithmic complexity is not multiplied and it remains in the order of nlogn.
181+
182+
Also it performs sorting in O(1) space complexity. Comparing with Quick Sort, it has better worst case ( O(nlogn) ). Quick Sort has complexity O(n^2) for worst case. But in other cases, Quick Sort is fast. Introsort is an alternative to heapsort that combines quicksort and heapsort to retain advantages of both: worst case speed of heapsort and average speed of quicksort.
183+
Application of Heap Sort
184+
185+
Systems concerned with security and embedded system such as Linux Kernel uses Heap Sort because of the O(n log n) upper bound on Heapsort's running time and constant O(1) upper bound on its auxiliary storage.
186+
187+
Although Heap Sort has O(n log n) time complexity even for worst case, it doesn’t have more applications ( compared to other sorting algorithms like Quick Sort, Merge Sort ). However, its underlying data structure, heap, can be efficiently used if we want to extract smallest (or largest) from the list of items without the overhead of keeping the remaining items in the sorted order. For e.g Priority Queues.
188+
189+
#The above algorithm for Heap Sort i.e. the Heap Sorting Algorithm is proposed by Argho Chakraborty!!!
190+
#Have a great fun.Love Coding,CHEERS!!!!

Heapsort.py

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
41+
# Driver code to test above
42+
arr = [ 12, 11, 13, 5, 6, 7]
43+
heapSort(arr)
44+
n = len(arr)
45+
print ("Sorted array is")
46+
for i in range(n):
47+
print ("%d" %arr[i]),
48+
# This code is contributed by Argho Chakraborty!!!

QuickSort.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Python program for implementation of Quicksort Sort
2+
3+
# This function takes last element as pivot, places
4+
# the pivot element at its correct position in sorted
5+
# array, and places all smaller (smaller than pivot)
6+
# to left of pivot and all greater elements to right
7+
# of pivot
8+
def partition(arr,low,high):
9+
i = ( low-1 ) # index of smaller element
10+
pivot = arr[high] # pivot
11+
12+
for j in range(low , high):
13+
14+
# If current element is smaller than or
15+
# equal to pivot
16+
if arr[j] <= pivot:
17+
18+
# increment index of smaller element
19+
i = i+1
20+
arr[i],arr[j] = arr[j],arr[i]
21+
22+
arr[i+1],arr[high] = arr[high],arr[i+1]
23+
return ( i+1 )
24+
25+
# The main function that implements QuickSort
26+
# arr[] --> Array to be sorted,
27+
# low --> Starting index,
28+
# high --> Ending index
29+
30+
# Function to do Quick sort
31+
def quickSort(arr,low,high):
32+
if low < high:
33+
34+
# pi is partitioning index, arr[p] is now
35+
# at right place
36+
pi = partition(arr,low,high)
37+
38+
# Separately sort elements before
39+
# partition and after partition
40+
quickSort(arr, low, pi-1)
41+
quickSort(arr, pi+1, high)
42+
43+
# Driver code to test above
44+
arr = [10, 7, 8, 9, 1, 5]
45+
n = len(arr)
46+
quickSort(arr,0,n-1)
47+
print ("Sorted array is:")
48+
for i in range(n):
49+
print ("%d" %arr[i]),
50+
51+
# This code is contributed by Abdul Aziz

README.md

+10-5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33
[![first-timers-only](https://img.shields.io/badge/first--timers--only-friendly-blue.svg?style=flat-square)](https://www.firsttimersonly.com/)
44

55
## Guidelines (IMPORTANT)
6-
- Mention the appropiate issue number in your pull request.
6+
- Mention the appropiate [issue number](https://help.github.com/en/articles/autolinked-references-and-urls#issues-and-pull-requests) in your pull request.
7+
- If the algo you want to add is not in issues or Proposed List yet, make a new issue.
78
- Please test your script before sumitting pull requests.
9+
- Please use 3.x python only.
810

911
## Current List of Sorting Algos:
10-
12+
- [Quick Sort](https://github.com/Zircoz/Sorting-Algorithms-in-Python/blob/master/QuickSort.py)
13+
- [Selection Sort](https://github.com/Zircoz/Sorting-Algorithms-in-Python/blob/master/Selection_Sort.py)
14+
- [Insertion Sort](https://github.com/Zircoz/Sorting-Algorithms-in-Python/blob/master/insertion_sort.py)
15+
- [Merge Sort](https://github.com/Zircoz/Sorting-Algorithms-in-Python/blob/master/merge_sort.py)
16+
- [Bubble Sort](https://github.com/Zircoz/Sorting-Algorithms-in-Python/blob/master/bubbleSort.py)
17+
- [Counting Sort](https://github.com/Zircoz/Sorting-Algorithms-in-Python/blob/master/counting_sort.py)
18+
- [Heap Sort](https://github.com/blackeye735/Sorting-Algorithms-in-Python/blob/master/Heapsort.py)
1119

1220
## Proposed List of Sorting Algos:
13-
*(With hyperlinks to their respective issues)*
1421

15-
- [Bubble Sort](https://github.com/Zircoz/Sorting-Algorithms-in-Python/issues/1)
16-
- [Quick Sort](https://github.com/Zircoz/Sorting-Algorithms-in-Python/issues/3)

0 commit comments

Comments
 (0)