Skip to content

Commit a41bb88

Browse files
committed
Add heapsort algorithm
1 parent 29a65ed commit a41bb88

File tree

7 files changed

+119
-7
lines changed

7 files changed

+119
-7
lines changed

Diff for: README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,14 @@ List of Programs related to data structures and algorithms
253253

254254
## Sorting
255255

256-
1. Bubble sort: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/bubbleSort/bubbleSort.js)
256+
1. Bubble sort: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/bubbleSort/bubbleSort.js) [Playground](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/bubbleSort/bubbleSort.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/bubbleSort/bubbleSort.md)
257257

258-
2. Selection sort: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/selectionSort/selectionSort.js)
258+
2. Selection sort: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/selectionSort/selectionSort.js) [Playground](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/selectionSort/selectionSort.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/selectionSort/selectionSort.md)
259259

260-
3. Insertion sort: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/insertionSort/insertionSort.js)
260+
3. Insertion sort: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/insertionSort/insertionSort.js) [Playground](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/insertionSort/insertionSort.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/insertionSort/insertionSort.md)
261261

262-
4. Merge sort: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/mergeSort/mergeSort.js)
262+
4. Merge sort: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/mergeSort/mergeSort.js) [Playground](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/mergeSort/mergeSort.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/mergeSort/mergeSort.md)
263263

264-
5. Quick sort: [JavaScript](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/quickSort/quickSort.js)
264+
5. Quick sort: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/quickSort/quickSort.js) [Playground](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/quickSort/quickSort.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/quickSort/quickSort.md)
265+
266+
6. Heap sort: [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/heapSort/heapSort.js) [Playground](https://livecodes.io/?console&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/heapSort/heapSort.js) [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/sorting/heapSort/heapSort.md)

Diff for: src/java1/sorting/heapSort/HeapSort.java

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package java1.sorting.heapSort;
2+
3+
import java.util.Arrays;
4+
5+
public class HeapSort {
6+
7+
private static void heapSort(int[] arr) {
8+
int size = arr.length;
9+
for(int i = (int)Math.floor(size/2 - 1); i>=0; i--) {
10+
heapify(arr, arr.length, i);
11+
}
12+
13+
for (int i = size -1; i > 0 ; i--) {
14+
swap(arr, i, 0);
15+
heapify(arr, i, 0);
16+
}
17+
}
18+
19+
private static void heapify(int[] arr, int size, int parentIndex) {
20+
int largest = parentIndex;
21+
int leftIndex = 2 * parentIndex + 1;
22+
int rightIndex = 2 * parentIndex + 2;
23+
24+
if(leftIndex < size && arr[leftIndex] > arr[largest]) {
25+
largest = leftIndex;
26+
}
27+
28+
if(rightIndex < size && arr[rightIndex] > arr[largest]) {
29+
largest = rightIndex;
30+
}
31+
32+
if(parentIndex != largest) {
33+
swap(arr, parentIndex, largest);
34+
heapify(arr, size, largest);
35+
}
36+
}
37+
38+
private static void swap(int[] arr, int first, int second) {
39+
int temp = arr[first];
40+
arr[first] = arr[second];
41+
arr[second] = temp;
42+
}
43+
44+
public static void main(String[] args) {
45+
int[] arr = { 40, 50, 20, 0, -10, 30, 10 };
46+
heapSort(arr);
47+
System.out.println(Arrays.toString(arr));
48+
}
49+
}

Diff for: src/java1/sorting/heapSort/heapSort.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Heap Sort
2+
HeapSort is an in-place comparison based sorting algorithm that uses **binary heap** data structure. The main idea behind this heap sort is to find either the highest value(max heap) or lowest value(min heap) and place it at the end, repeating the process until all the values are sorted.
3+
4+
## How it works
5+
1. Build a max heap based on given input array.
6+
2. Switch the top root with the last node and then remove it from the heap.
7+
3. Rebuild the max heap again and repeat until there is only one element remaining in the heap.
8+
9+
## Complexity
10+
11+
| Best | Average | Worst | Space (Memory) | Stable | Advantage |
12+
| :-------------: | :-----------------: | :-----------------: | :-------: | :-------: | :-------- |
13+
| Ω(n log(n)) | θ(n log(n)) | O(n log(n)) | O(1) | Yes | Sort efficiently large data sets |

Diff for: src/java1/sorting/quickSort/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ QuickSort is a sorting algorithm based on **Divide and Conquer technique** that
1212

1313
| Best | Average | Worst | Space (Memory) | Stable | Advantage |
1414
| :-------------: | :-----------------: | :-----------------: | :-------: | :-------: | :-------- |
15-
| Ω(n log(n)) | θ(n log(n)) | O(n<sup>2</sup>) | O(1) | Yes | Efficient alogorithm except for almost sorted arrays |
15+
| Ω(n log(n)) | θ(n log(n)) | O(n<sup>2</sup>) | O(log n) | Yes | Efficient alogorithm except for almost sorted arrays |

Diff for: src/javascript/sorting/heapSort/heapSort.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function heapSort(arr) {
2+
let size = arr.length;
3+
4+
for (let i = Math.floor(size/2 - 1); i >= 0; i--) {
5+
heapify(arr, size, i);
6+
}
7+
8+
for (let i = size - 1; i >= 0; i--) {
9+
[arr[i], arr[0]] = [arr[0], arr[i]];
10+
heapify(arr, i, 0);
11+
}
12+
}
13+
14+
function heapify(arr, size, parentIndex) {
15+
let largest = parentIndex;
16+
let leftChild = 2 * parentIndex + 1;
17+
let rightChild = 2 * parentIndex + 2;
18+
19+
if(leftChild < size && arr[leftChild] > arr[largest]) {
20+
largest = leftChild;
21+
}
22+
23+
if(rightChild < size && arr[rightChild] > arr[largest]) {
24+
largest = rightChild;
25+
}
26+
27+
if(parentIndex !== largest) {
28+
[arr[parentIndex], arr[largest]] = [arr[largest], arr[parentIndex]];
29+
heapify(arr, size, largest);
30+
}
31+
}
32+
33+
let arr = [40, 50, 20, 0, -10, 30, 10];
34+
heapSort(arr);
35+
console.log(arr);

Diff for: src/javascript/sorting/heapSort/heapSort.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Heap Sort
2+
HeapSort is an in-place comparison based sorting algorithm that uses **binary heap** data structure. The main idea behind this heap sort is to find either the highest value(max heap) or lowest value(min heap) and place it at the end, repeating the process until all the values are sorted.
3+
4+
## How it works
5+
1. Build a max heap based on given input array.
6+
2. Switch the top root with the last node and then remove it from the heap.
7+
3. Rebuild the max heap again and repeat until there is only one element remaining in the heap.
8+
9+
## Complexity
10+
11+
| Best | Average | Worst | Space (Memory) | Stable | Advantage |
12+
| :-------------: | :-----------------: | :-----------------: | :-------: | :-------: | :-------- |
13+
| Ω(n log(n)) | θ(n log(n)) | O(n log(n)) | O(1) | Yes | Sort efficiently large data sets |

Diff for: src/javascript/sorting/quickSort/quickSort.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ QuickSort is a sorting algorithm based on **Divide and Conquer technique** that
1212

1313
| Best | Average | Worst | Space (Memory) | Stable | Advantage |
1414
| :-------------: | :-----------------: | :-----------------: | :-------: | :-------: | :-------- |
15-
| Ω(n log(n)) | θ(n log(n)) | O(n<sup>2</sup>) | O(1) | Yes | Efficient alogorithm except for almost sorted arrays |
15+
| Ω(n log(n)) | θ(n log(n)) | O(n<sup>2</sup>) | O(log n) | Yes | Efficient alogorithm except for almost sorted arrays |

0 commit comments

Comments
 (0)