Types of Sorting Algorithm in R Programming
Last Updated :
17 Jun, 2021
There are multiple ways by which data can be sorted in the R language. It’s up to the data Analyst to consider the most suitable method based upon the structure of the data. There are multiple algorithms for performing sorting on the data in the R programming language. Below different types of sorting function have been discussed. A sample of 10 random numbers between 1 to 100 from an array is used. We are going to discuss the following sorting algorithm:
- Bubble Sort
- Insertion Sort
- Selection Sort
- Merge Sort
- Quick Sort
Bubble Sort
In this algorithm, two adjacent elements are compared and swapped if the criteria are met. In bubble sort, in each iteration, the largest element is brought to the end of the array(in case of increasing) by swapping elements, hence the name of the algorithm is bubble sort. To understand the bubble sort algorithm in detail please refer to Bubble Sort.
R
# function to sort the array using bubble sort
bubble_sort <- function(x)
{
# calculate the length of array
n <- length(x)
# run loop n-1 times
for (i in 1 : (n - 1)) {
# run loop (n-i) times
for (j in 1 : (n - i)) {
# compare elements
if (x[j] > x[j + 1]) {
temp <- x[j]
x[j] <- x[j + 1]
x[j + 1] <- temp
}
}
}
x
}
# take 10 random numbers between 1 - 100
arr <- sample(1 : 100, 10)
# sort the array and store the result
# in sorted_array
sorted_array <- bubble_sort(arr)
# print sorted_array
sorted_array
Output:
[1] 2 19 26 68 74 76 80 81 82 91
Insertion Sort
In this sorting algorithm, sorted and unsorted elements are compared, and the unsorted element is placed in its correct position after each iteration. In this algorithm, the first element is assumed to be sorted and the second element is stored separately as a key element that needs to be sorted. The key is then compared with the sorted element. If the sorted element is greater than the key element, their places are swapped, and the key element becomes the first element. To understand the Insertion sort algorithm in detail please refer to Insertion Sort.
R
# insertion sort function to sort array
insertion_sort <- function(x)
{
# calculate the length of array
n <- length(x)
# outer loop
for (i in 2 : (n))
{
# store first element as key
key = x[i]
j = i - 1
# compare key with elements for
# its correct position
while (j > 0 && x[j] > key)
{
x[j + 1] = x[j]
j = j - 1
}
# Place key at its correct position
x[j + 1] = key
}
# return sorted array
x
}
# take sample array
arr <- sample(1 : 100, 10)
# call insertion sort function
sorted_arr <- insertion_sort(arr)
# print sorted array
sorted_arr
Output:
[1] 10 27 30 41 58 77 80 89 90 85
Selection Sort
This sorting algorithm is widely used in the R language. Here, the smallest element from the unsorted list is pushed to the start of the list at every iteration. To understand the Selection sort algorithm in detail please refer to Selection Sort.
R
# function to sort array using selection sort
selection_sort <- function(x)
{
# length of array
n <- length(x)
for (i in 1 : (n - 1))
{
# assume element at i is minimum
min_index <- i
for (j in (i + 1) : (n))
{
# check if element at j is smaller
# than element at min_index
if (x[j] < x[min_index]) {
# if yes, update min_index
min_index = j
}
}
# swap element at i with element at min_index
temp <- x[i]
x[i] <- x[min_index]
x[min_index] <- temp
}
x
}
# take sample input
arr <- sample(1 : 100, 10)
# sort array
sorted_arr <- selection_sort(arr)
# print array
sorted_arr
Output
[1] 6 16 21 28 31 48 57 73 85 99
Merge Sort
This is a divide and conquers algorithm. We divide the array into two parts from mid, sort those two array,s and merge them. The entire process is done recursively. To understand the Merge sort algorithm in detail please refer to Merge Sort.
R
# function to merge two sorted arrays
merge <- function(a, b) {
# create temporary array
temp <- numeric(length(a) + length(b))
# take two variables which initially points to
# starting of the sorted sub arrays
# and j which points to starting of starting
# of temporary array
astart <- 1
bstart <- 1
j <- 1
for(j in 1 : length(temp)) {
# if a[astart] < b[bstart]
if((astart <= length(a) &&
a[astart] < b[bstart]) ||
bstart > length(b)) {
# insert a[astart] in temp and increment
# astart pointer to next
temp[j] <- a[astart]
astart <- astart + 1
}
else {
temp[j] <- b[bstart]
bstart <- bstart + 1
}
}
temp
}
# function to sort the array
mergeSort <- function(arr) {
# if length of array is greater than 1,
# then perform sorting
if(length(arr) > 1) {
# find mid point through which
# array need to be divided
mid <- ceiling(length(arr)/2)
# first part of array will be from 1 to mid
a <- mergeSort(arr[1:mid])
# second part of array will be
# from (mid+1) to length(arr)
b <- mergeSort(arr[(mid+1):length(arr)])
# merge above sorted arrays
merge(a, b)
}
# else just return arr with single element
else {
arr
}
}
# take sample input
arr <- sample(1:100, 10)
# call mergeSort function
result <- mergeSort(arr)
# print result
result
Output
[1] 6 8 16 19 21 24 35 38 74 90
Quick Sort
This is a divide and conquers algorithm. It picks an element as a pivot and partitions the given array around the picked pivot. Pivot can be random. To understand the Merge sort algorithm in detail please refer to Quick Sort.
R
# function to sort the values
quickSort <- function(arr) {
# Pick a number at random
random_index <- sample(seq_along(arr), 1);
pivot <- arr[random_index]
arr <- arr[-random_index]
# Create array for left and right values.
left <- c()
right <- c()
# Move all smaller and equal values to the
# left and bigger values to the right.
# compare element with pivot
left<-arr[which(arr <= pivot)]
right<-arr[which(arr > pivot)]
if (length(left) > 1)
{
left <- quickSort(left)
}
if (length(right) > 1)
{
right <- quickSort(right)
}
# Return the sorted values.
return(c(left, pivot, right))
}
# take sample array
arr <- sample(1:100, 10)
# call quickSort function
result <- quickSort(arr)
# print result
result
Output:
[1] 13 18 21 38 70 74 80 83 95 99
Similar Reads
Computer Science Subjects