Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav authored Oct 4, 2020
2 parents 276eae7 + cac55ee commit 608bbdc
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,3 @@ Here are some common algorithms that can be applied to almost all data problems:
- [Merge Sort](https://github.com/TheAlgorithms/R/blob/master/sorting/Merge%20sort.R)
- [Radix Sort](https://github.com/TheAlgorithms/R/blob/master/sorting/Radix%20sort.R)
- [Selection Sort](https://github.com/TheAlgorithms/R/blob/master/sorting/SelectionSort.R)
- [Quick Sort](https://github.com/TheAlgorithms/R/blob/master/sorting/Quick%20sort.R)

63 changes: 63 additions & 0 deletions sorting/Heap sort.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Heap sort in R:

build.heap <- function(elements.vec) {
l = length(elements.vec)
heap = elements.vec
for (i in l:1) {
heap = modify.heap(heap, i)
}
return(heap)
}

is.heap <- function(heap, rootIndex) {
i = rootIndex
res = T
while(2 * i <= length(heap) & res) {
child = c(heap[2 * i], heap[2 * i + 1])
child = child[!is.na(child)]
result.bool = all(heap[i] <= child)
i = i + 1
}
return(result.bool)
}

modify.heap <- function(heap, rootIndex) {
l = length(heap)
flag = 1
while (rootIndex * 2 <= l && flag == 1) {
leftIndex = rootIndex * 2
rightIndex = rootIndex * 2 + 1
flag = 0
child = c(heap[leftIndex], heap[rightIndex])
child = child[!is.na(child)]
minIndex = which.min(child)
if (heap[rootIndex] > child[minIndex]) {
flag = 1
heapIndex = c(leftIndex, rightIndex)[minIndex]
temp = heap[heapIndex]
heap[heapIndex] = heap[rootIndex]
heap[rootIndex] = temp
rootIndex = heapIndex
}
}
return(heap)
}

heap.sort <- function(heap) {
sorted.elements = NULL
l = length(heap)
while(l > 0)
{
sorted.elements = c(sorted.elements, heap[1])
l = length(heap)
heap[1] = heap[l]
heap = heap[1:(l - 1)]
heap = modify.heap(heap, rootIndex = 1)
l = l - 1
}
return(sorted.elements)
}

# Example:
# heap.sort(build.heap(c(5, 2, 3, 1, 4)))
# [1] 1 2 3 4 5

0 comments on commit 608bbdc

Please sign in to comment.