From fe44c76017d9c3a0d2078a54c2f31aedcbb4c081 Mon Sep 17 00:00:00 2001 From: iMeet07 <97329296+iMeet07@users.noreply.github.com> Date: Thu, 5 Oct 2023 00:18:05 +0530 Subject: [PATCH 1/3] Create binary-insertionSort.r Added binary-insertionSort Algorithm --- sorting_algorithms/binary-insertionSort.r | 43 +++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 sorting_algorithms/binary-insertionSort.r diff --git a/sorting_algorithms/binary-insertionSort.r b/sorting_algorithms/binary-insertionSort.r new file mode 100644 index 0000000..4497c77 --- /dev/null +++ b/sorting_algorithms/binary-insertionSort.r @@ -0,0 +1,43 @@ +# Binary Insertion Sort Function +# Sorts an input vector using the Binary Insertion Sort algorithm. +# Parameters: +# - arr: Input vector to be sorted. +# Returns: +# - Sorted vector. +binary_insertion_sort <- function(arr) { + # Loop through the input vector starting from the second element. + for (i in 2:length(arr)) { + # Store the current element in a variable. + key <- arr[i] + # Initialize left and right pointers for binary search. + left <- 1 + right <- i - 1 + + # Binary search to find the correct position to insert the key. + while (left <= right) { + mid <- left + (right - left) %/% 2 + + if (key < arr[mid]) { + right <- mid - 1 + } else { + left <- mid + 1 + } + } + + # Shift elements to the right to make space for the key. + for (j in i: (left + 1)) { + arr[j] <- arr[j - 1] + } + + # Insert the key into its correct position. + arr[left] <- key + } + + # Return the sorted vector. + return(arr) +} + +# Example usage: +elements_vec <- c(64, 34, 25, 12, 22, 11, 90) +sorted_vec <- binary_insertion_sort(elements_vec) +print(sorted_vec) From f50904f66a2923fe0fe2219c67cd1b5c870c182e Mon Sep 17 00:00:00 2001 From: iMeet07 <97329296+iMeet07@users.noreply.github.com> Date: Thu, 5 Oct 2023 01:25:33 +0530 Subject: [PATCH 2/3] Rename binary-insertionSort.r to binary_insertion_Sort.r --- .../{binary-insertionSort.r => binary_insertion_Sort.r} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename sorting_algorithms/{binary-insertionSort.r => binary_insertion_Sort.r} (100%) diff --git a/sorting_algorithms/binary-insertionSort.r b/sorting_algorithms/binary_insertion_Sort.r similarity index 100% rename from sorting_algorithms/binary-insertionSort.r rename to sorting_algorithms/binary_insertion_Sort.r From 62d4bd0581ff1c20826c6497cf58b1323cce2c3d Mon Sep 17 00:00:00 2001 From: iMeet07 <97329296+iMeet07@users.noreply.github.com> Date: Thu, 5 Oct 2023 13:10:33 +0530 Subject: [PATCH 3/3] Rename binary_insertion_Sort.r to binary_insertion_sort.r --- .../{binary_insertion_Sort.r => binary_insertion_sort.r} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename sorting_algorithms/{binary_insertion_Sort.r => binary_insertion_sort.r} (100%) diff --git a/sorting_algorithms/binary_insertion_Sort.r b/sorting_algorithms/binary_insertion_sort.r similarity index 100% rename from sorting_algorithms/binary_insertion_Sort.r rename to sorting_algorithms/binary_insertion_sort.r