R Program to Implement Quick Sort

R Program to Implement Quick Sort

Quick Sort is one of the most popular and powerful sorting algorithms used in programming. It works by picking one element as a pivot, then arranging all smaller values on one side and larger values on the other side. After that, it repeats the same process on each side until the entire list becomes sorted. Even though the idea sounds simple, Quick Sort is very fast and efficient in practice.

Learn C Programming For Free on Windows

A beginner-friendly course that teaches real C programming using a Windows compiler. Learn arrays, pointers, functions, and file handling step by step with practical lessons.

Start Learning C Programming

Quick Sort matters because it is widely used in real systems, libraries, and software tools. Many built-in sorting functions are inspired by it. In R programming, learning Quick Sort helps beginners understand recursion, logical thinking, and how large problems can be broken into smaller ones. These ideas are very useful when working with real data, such as exam scores from Nigeria or sales data from Zambia.

Program 1: Quick Sort Using Basic Recursion

This program shows the classic recursive way to implement Quick Sort in R. It chooses a pivot, splits the data, and sorts each part separately.

# Quick Sort using basic recursion

quick_sort <- function(numbers) {

  if (length(numbers) <= 1) {
    return(numbers)
  }

  pivot <- numbers[1]
  left <- numbers[numbers < pivot]
  middle <- numbers[numbers == pivot]
  right <- numbers[numbers > pivot]

  c(quick_sort(left), middle, quick_sort(right))

}

data <- c(10, 7, 8, 9, 1, 5)
sorted_data <- quick_sort(data)

print(sorted_data)

This program works by selecting the first value as the pivot and dividing the remaining values into smaller and larger groups. Each group is sorted again using the same function. Beginners like this version because it is short, clear, and closely matches the idea behind Quick Sort.

Program 2: Quick Sort Using the Last Element as Pivot

This version uses the last element as the pivot instead of the first one. The overall behavior stays the same, but the pivot choice changes.

# Quick Sort using last element as pivot

quick_sort <- function(numbers) {

  if (length(numbers) <= 1) {
    return(numbers)
  }

  pivot <- numbers[length(numbers)]
  left <- numbers[numbers < pivot]
  middle <- numbers[numbers == pivot]
  right <- numbers[numbers > pivot]

  c(quick_sort(left), middle, quick_sort(right))

}

values <- c(4, 2, 6, 9, 2, 1)
print(quick_sort(values))

Here, the pivot is taken from the end of the vector. This program helps beginners see that Quick Sort can be implemented in different ways while still following the same idea. It also encourages experimentation with pivot selection.

Program 3: Quick Sort Using a Separate Partition Function

This program separates the partition logic into its own function. This makes the code easier to understand step by step.

# Quick Sort with a partition function

partition <- function(numbers, pivot) {

  left <- numbers[numbers < pivot]
  middle <- numbers[numbers == pivot]
  right <- numbers[numbers > pivot]
  list(left = left, middle = middle, right = right)

}

quick_sort <- function(numbers) {

  if (length(numbers) <= 1) {
    return(numbers)
  }

  pivot <- numbers[1]
  parts <- partition(numbers, pivot)

  c(quick_sort(parts$left), parts$middle, quick_sort(parts$right))

}

data <- c(15, 3, 9, 8, 5, 2)
print(quick_sort(data))

This program clearly shows how the data is split around the pivot before sorting continues. Beginners find this helpful because it breaks the problem into smaller, easier pieces. It also teaches how functions can work together in R.

Program 4: Quick Sort Using an Index-Based Recursive Approach

This version looks closer to traditional textbook Quick Sort, using indexes instead of vector filtering.

# Index-based Quick Sort

quick_sort <- function(numbers, low = 1, high = length(numbers)) {

  if (low < high) {

    pivot_index <- low
    pivot <- numbers[high]

    for (j in low:(high - 1)) {

      if (numbers[j] <= pivot) {

        temp <- numbers[pivot_index]
        numbers[pivot_index] <- numbers[j]
        numbers[j] <- temp
        pivot_index <- pivot_index + 1

      }

    }

    temp <- numbers[pivot_index]
    numbers[pivot_index] <- numbers[high]
    numbers[high] <- temp

    numbers <- quick_sort(numbers, low, pivot_index - 1)
    numbers <- quick_sort(numbers, pivot_index + 1, high)

  }

  numbers

}

values <- c(12, 7, 14, 9, 10, 11)
print(quick_sort(values))

This approach sorts the vector in place using index positions. It is slightly more complex, but it helps beginners understand how Quick Sort works at a deeper level. This style is closer to how Quick Sort is implemented in low-level languages.

Program 5: Quick Sort with Negative and Floating Point Numbers

Quick Sort in R naturally supports negative and decimal values. This program demonstrates that clearly using mixed data.

# Quick Sort with negative and floating point numbers

quick_sort <- function(numbers) {

  if (length(numbers) <= 1) {
    return(numbers)
  }

  pivot <- numbers[1]
  left <- numbers[numbers < pivot]
  middle <- numbers[numbers == pivot]
  right <- numbers[numbers > pivot]

  c(quick_sort(left), middle, quick_sort(right))

}

numbers <- c(3.5, -2.4, 7.1, 0.0, -1.9)
print(quick_sort(numbers))

This program behaves the same way as earlier versions but works with negative and floating point values. It shows beginners that Quick Sort is suitable for real-world data such as temperatures, measurements, or financial values collected in cities like Lusaka or Nairobi.

Frequently Asked Questions (FAQ)

This section answers common beginner questions about Quick Sort in R in a simple and friendly way.

Q1: Why is Quick Sort so fast?
Quick Sort reduces the problem size quickly by dividing the data around a pivot. This makes it much faster than simple algorithms for large datasets.

Q2: Is Quick Sort better than Merge Sort?
Quick Sort is usually faster in practice, but Merge Sort is more stable. Both are important and useful in different situations.

Q3: Does Quick Sort always use recursion?
Quick Sort is usually written using recursion, but iterative versions also exist. Recursion makes the idea easier to understand.

Q4: Can Quick Sort sort characters or strings in R?
Yes, Quick Sort can sort strings as long as R can compare them. Strings are sorted alphabetically by default.

Q5: Where is Quick Sort used in real life?
Quick Sort is used in system libraries, data processing tools, and many applications that need fast sorting.

Conclusion

Quick Sort is a powerful and widely used sorting algorithm that every R beginner should learn. By exploring different implementations, from simple recursive versions to index-based approaches, you can understand how flexible and efficient this algorithm is. It works well with large datasets and handles negative and floating point numbers with ease.

If you are learning R, take time to practice these Quick Sort programs and change the input values. Like learning a trusted method passed down over time, mastering Quick Sort will build your confidence and prepare you for more advanced algorithms. Keep practicing, stay curious, and enjoy your journey in R programming.

Scroll to Top