Types of Sorting Algorithm in R Programming
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
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.
 2 19 26 68 74 76 80 81 82 91
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.
 10 27 30 41 58 77 80 89 90 85
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.
 6 16 21 28 31 48 57 73 85 99
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.
 6 8 16 19 21 24 35 38 74 90
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.
 13 18 21 38 70 74 80 83 95 99
Please Login to comment...