Skip to content

Commit 9cdbe9b

Browse files
committed
add heap sort
1 parent 4218611 commit 9cdbe9b

File tree

8 files changed

+115
-6
lines changed

8 files changed

+115
-6
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ WIP, the description of the below `unsolved yet` problems can be found in the or
5959
- [x] [Bubble Sort](https://github.com/danrusei/algorithms_with_Go/tree/main/sorting/bubble_sort)
6060
- [x] [Insertion Sort](https://github.com/danrusei/algorithms_with_Go/tree/main/sorting/insertion_sort)
6161
- [x] [Merge Sort](https://github.com/danrusei/algorithms_with_Go/tree/main/sorting/merge_sort)
62-
- [] Heap Sort (Binary Heap)
62+
- [x] [Heap Sort (Binary Heap)](https://github.com/danrusei/algorithms_with_Go/tree/main/sorting/heap_sort)
6363
- [x] [Quick Sort](https://github.com/danrusei/algorithms_with_Go/tree/main/sorting/quick_sort)
6464
- [] Interpolation Search
6565
- [] Find Kth Smallest/Largest Element In Unsorted Array

sorting/bubble_sort/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ Bubble sort, sometimes referred to as sinking sort, is a simple sorting algorith
66

77
## Complexity
88

9-
Worst Case Time Complexity [ Big-O ]: **O(n2)**
9+
Worst Case Time Complexity [ Big-O ]: **O(n * n)**
1010
Best Case Time Complexity [Big-omega]: **O(n)**- when the list is already sorted
11-
Average Time Complexity [Big-theta]: **O(n2)**
11+
Average Time Complexity [Big-theta]: **O(n * n)**
1212
Space Complexity: **O(1)**- because only a single additional memory space is required for temp variable
1313

1414
## Example

sorting/heap_sort/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Heap Sort
2+
3+
Heap sort is a comparison based sorting technique based on Binary Heap data structure. It is similar to selection sort where we first find the maximum element and place the maximum element at the end. We repeat the same process for the remaining elements.
4+
5+
Source: [Geeksforgeeks](https://www.geeksforgeeks.org/heap-sort/)
6+
7+
## Complexity
8+
9+
Worst Case Time Complexity [ Big-O ]: **O(n log n)**
10+
Best Case Time Complexity [Big-omega]: **O(n log n)**
11+
Average Time Complexity [Big-theta]: **O(n log n)**
12+
Space Complexity: **O(n)**
13+
14+
## Algorithm
15+
16+
* Build a max heap from the input data.
17+
* At this point, the largest item is stored at the root of the heap. Replace it with the last item of the heap followed by reducing the size of heap by 1. Finally, heapify the root of the tree.
18+
* Repeat step 2 while size of heap is greater than 1.

sorting/heap_sort/heap.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package heapsort
2+
3+
func heapsort(target []int) {
4+
n := len(target)
5+
sortIt(target, n)
6+
}
7+
8+
func sortIt(target []int, n int) {
9+
10+
// Build heap (rearrange slice)
11+
for i := n/2 - 1; i >= 0; i-- {
12+
heapify(target, n, i)
13+
}
14+
15+
// One by one extract an element from heap
16+
for i := n - 1; i > 0; i-- {
17+
// move current root to end
18+
target[0], target[i] = target[i], target[0]
19+
// call max heapify on the reduced heap
20+
heapify(target, i, 0)
21+
}
22+
}
23+
24+
func heapify(target []int, n int, i int) {
25+
26+
// If the parent node is stored at index i,
27+
// the left child can be calculated by 2 * i + 1 and
28+
// right child by 2 * i + 2
29+
largest := i
30+
left := 2*i + 1
31+
right := 2*i + 2
32+
33+
if left < n && target[left] > target[largest] {
34+
largest = left
35+
}
36+
37+
if right < n && target[right] > target[largest] {
38+
largest = right
39+
}
40+
41+
if largest != i {
42+
target[i], target[largest] = target[largest], target[i]
43+
// recursively heapify the affected sub-tree
44+
heapify(target, n, largest)
45+
}
46+
}

sorting/heap_sort/heap_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package heapsort
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestMergeSort(t *testing.T) {
9+
for _, tc := range testcases {
10+
t.Run(tc.name, func(t *testing.T) {
11+
heapsort(tc.original)
12+
if !reflect.DeepEqual(tc.original, tc.sorted) {
13+
t.Errorf("got: %v, want: %v", tc.original, tc.sorted)
14+
}
15+
})
16+
}
17+
}

sorting/heap_sort/test_cases.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package heapsort
2+
3+
var testcases = []struct {
4+
name string
5+
original []int
6+
sorted []int
7+
}{
8+
{
9+
name: "4 elements in list",
10+
original: []int{4, 2, 3, 1},
11+
sorted: []int{1, 2, 3, 4},
12+
},
13+
{
14+
name: "empty list",
15+
original: []int{},
16+
sorted: []int{},
17+
},
18+
{
19+
name: "already sorted list",
20+
original: []int{10, 20, 30, 40},
21+
sorted: []int{10, 20, 30, 40},
22+
},
23+
{
24+
name: "9 elements in the list",
25+
original: []int{11, 21, 12, 4, 3, 100, 1000, 1, 123},
26+
sorted: []int{1, 3, 4, 11, 12, 21, 100, 123, 1000},
27+
},
28+
}

sorting/insertion_sort/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ Insertion sort is a simple sorting algorithm that builds the final sorted array
66

77
## Complexity
88

9-
Worst Case Time Complexity [ Big-O ]: **O(n2)**
9+
Worst Case Time Complexity [ Big-O ]: **O(n * n)**
1010
Best Case Time Complexity [Big-omega]: **O(n)**
11-
Average Time Complexity [Big-theta]: **O(n2)**
11+
Average Time Complexity [Big-theta]: **O(n * n)**
1212
Space Complexity: **O(n)**
1313

1414
## Algorithm

sorting/quick_sort/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Quicksort is a divide-and-conquer algorithm. It works by selecting a 'pivot' ele
66

77
## Complexity
88

9-
Worst Case Time Complexity [ Big-O ]: **O(n2)**
9+
Worst Case Time Complexity [ Big-O ]: **O(n * n)**
1010
Best Case Time Complexity [Big-omega]: **O(n log n)**
1111
Average Time Complexity [Big-theta]: **O(n log n)**
1212
Space Complexity: **O(n)**

0 commit comments

Comments
 (0)