Open In App

Types of Sorting Algorithm in R Programming

Last Updated : 17 Jun, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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

Sorting of Arrays in R Programming
Prerequisite: R – Array A vector is a uni-dimensional array, which is specified by a single dimension, length. A Vector can be created using the ‘c()‘ function. A list of values is passed to the c() function to create a vector. Sorting can be done either in ascending order or descending. There are few things which should be kept in mind before sort
5 min read
Sorting of a Vector in R Programming - sort() Function
In R Programming Language we can sort a vector in ascending or descending order using the sort() function. The sort() function returns a sorted version of the input vector. sort() function in is used to sort a vector by its values. It takes the Boolean value as an argument to sort in ascending or descending order. Syntax: sort(x, decreasing, na.las
2 min read
Apriori Algorithm in R Programming
Apriori algorithm is used for finding frequent itemsets in a dataset for association rule mining. It is called Apriori because it uses prior knowledge of frequent itemset properties. We apply an iterative approach or level-wise search where k-frequent itemsets are used to find k+1 itemsets. To improve the efficiency of the level-wise generation of
6 min read
Getting class of different data types in R Programming - class() Function
class() function in R Language is used to return the class of data used as the arguments. Syntax: class(x) Parameters: x: specified data Example 1: # R program to illustrate # class function # Specifying &quot;Biochemical oxygen demand&quot; # data set x &lt;- BOD x # Calling class() function class(x) Output: Time demand 1 1 8.3 2 2 10.3 3 3 19.0 4
1 min read
Getting type of different data types in R Programming - typeof() Function
typeof() function in R Language is used to return the types of data used as the arguments. Syntax: typeof(x) Parameters: x: specified data Example 1: # R program to illustrate # typeof function # Specifying &quot;Biochemical oxygen demand&quot; # data set x &lt;- BOD x # Calling typeof() function typeof(x) Output: Time demand 1 1 8.3 2 2 10.3 3 3 1
1 min read
Regression and its Types in R Programming
Regression analysis is a statistical tool to estimate the relationship between two or more variables. There is always one response variable and one or more predictor variables. Regression analysis is widely used to fit the data accordingly and further, predicting the data for forecasting. It helps businesses and organizations to learn about the beh
5 min read
Types of Vectors in R Programming
Vectors in R programming are the same as the arrays in C language which are used to hold multiple data values of the same type. One major key point is that in R the indexing of the vector will start from ‘1’ and not from ‘0’. Vectors are the most basic data types in R. Even a single object created is also stored in the form of a vector. Vectors are
5 min read
Types of Functions in R Programming
A function is a set of statements orchestrated together to perform a specific operation. A function is an object so the interpreter is able to pass control to the function, along with arguments that may be necessary for the function to accomplish the actions. The function in turn performs the task and returns control to the interpreter as well as a
6 min read
Sorting DataFrame in R using Dplyr
In this article, we will discuss about how to sort a dataframe in R programming language using Dplyr package. The package Dplyr in R programming language provides a function called arrange() function which is useful for sorting the dataframe. Syntax : arrange(.data, ...) The methods given below show how this function can be used in various ways to
3 min read
Sorting, Ordering, and Ranking: Unraveling R’s Powerful Functions
R Programming Language is widely used by data scientists and analysts. This language provides various packages and libraries that are user-friendly making analysis easier. The rich set of functions this language provides helps in data manipulation and transformation. In this article, we will understand R's powerful functions and its uses. Differenc
13 min read
Article Tags :
three90RightbarBannerImg