-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquicksort.R
More file actions
46 lines (34 loc) · 956 Bytes
/
quicksort.R
File metadata and controls
46 lines (34 loc) · 956 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#' ---
#' title: "Quick Sort in R"
#' author: "Rahul Goswami"
#' ---
#' It is a recursive function that implements the quicksort algorithm.
#' It takes as input a vector
#' It returns the sorted vector.
#' This algorithm was invented by Tony Hoare.
#' Algorithm:
#' 1. Pick an element as pivot
#' 2. Partition the vector around the pivot
#' 3. Sort the two partitions
#' 4. Merge the two sorted partitions
#' 5. Repeat the above steps until the vector is sorted
#' @param vec Vector to be sorted
#' @return Sorted vector
qs <- function(vec)
{
# If the vector has more than one element
if(length(vec) > 1)
{
# Pick the first element as pivot (can be any element)
pivot <- vec[1]
low <- qs(vec[vec < pivot])
mid <- vec[vec == pivot]
high <- qs(vec[vec > pivot])
c(low, mid, high)
}
else vec
}
#' ### Example
#' Take A vector
vector = c(7,6,5,1,0,9,5,5)
qs(vector)