Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Patience Sort #137

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions sorting_algorithms/patience_sort.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Patience Sort Function
# Sorts an input vector using the Patience Sort algorithm.
# Parameters:
# - arr: Input vector to be sorted.
# Returns:
# - Sorted vector.

patience_sort <- function(arr) {
if (length(arr) == 0) {
return(arr)
}

piles <- list()

# Build piles
for (x in arr) {
placed <- FALSE
for (i in seq_along(piles)) {
if (x < tail(piles[[i]], n=1)) {
piles[[i]] <- c(piles[[i]], x)
placed <- TRUE
break
}
}
if (!placed) {
piles[[length(piles) + 1]] <- c(x)
}
}

# Collect sorted elements
sorted_arr <- c()
while (length(piles) > 0) {
# Find the pile with the smallest top element
min_top <- Inf
min_index <- -1
for (i in seq_along(piles)) {
if (tail(piles[[i]], n=1) < min_top) {
min_top <- tail(piles[[i]], n=1)
min_index <- i
}
}
# Remove the smallest top element and add it to the sorted array
sorted_arr <- c(sorted_arr, min_top)
piles[[min_index]] <- head(piles[[min_index]], -1)
if (length(piles[[min_index]]) == 0) {
piles[[min_index]] <- NULL
}
}

return(sorted_arr)
}

# Example usage:
elements_vec <- c(4, 3, 2, 1)
patience_sorted_vec <- patience_sort(elements_vec)
print(patience_sorted_vec)
Loading