Sorting Algorithms in R Language

Posted by dazzathedrummer on Tue, 14 Jul 2020 18:11:18 +0200

Prior to writing this blog, I wrote a post called Eight Sorts Programmers Have No Reason Not to Know In that blog, there are detailed descriptions of the eight sorting algorithms. The code implementation is mainly implemented by Java code. Recently, there are many sorting algorithms in R language. So I want to reorganize the sorting algorithms in R language again this time. If you are not clear about the principle of sorting algorithms, you can click the link above to view the content of my previous article. This article is the main body of this article.To focus on code implementation, the principles are not covered here.If there are any errors in this article, we welcome criticism and correction.

1. Test data

# Test Array
vector = c(5,34,65,36,67,3,6,43,69,59,25,785,10,11,14)
vector

##  [1]   5  34  65  36  67   3   6  43  69  59  25 785  10  11  14

2.Sort functions included in the R language

There are three main sorting-related functions in R: sort(), rank(), order().Where sort(x) is a sort of vector x, rank () is a function of rank, its return value is the "rank" of the corresponding element in this vector, and the return value of order () is the position of the corresponding "rank" element in the vector.
sort(vector)
##  [1]   3   5   6  10  11  14  25  34  36  43  59  65  67  69 785
order(vector)
##  [1]  6  1  7 13 14 15 11  2  4  8 10  3  5  9 12
rank(vector)
##  [1]  2  8 12  9 13  1  3 10 14 11  7 15  4  5  6

3. Bubble sorting

# bubble sort
bubbleSort = function(vector) {
  n = length(vector)
  for (i in 1:(n-1)) {
    for (j in (i+1):n) {
      if(vector[i]>=vector[j]){
        temp = vector[i]
        vector[i] = vector[j]
        vector[j] = temp
        }
      }
    }
  return(vector)
}
bubbleSort(vector)

##  [1]   3   5   6  10  11  14  25  34  36  43  59  65  67  69 785

4. Quick Sort

# quick sort
quickSort = function(vector, small, big) {
  left = small
  right = big
  if (left >= right) {
    return(vector)
  }else{
    markValue = vector[left]
    while (left < right) {
      while (left < right && vector[right] >= markValue) {
        right = right - 1
      }
      vector[left] = vector[right]
      while (left < right && vector[left] <= markValue) {
        left = left + 1
      }
      vector[right] = vector[left]
    }
  vector[left] = markValue
  vector = quickSort(vector, small, left - 1)
  vector = quickSort(vector, right + 1, big)
  return(vector)
  }
}
quickSort(vector,1,15)

##  [1]   3   5   6  10  11  14  25  34  36  43  59  65  67  69 785

5. Insert Sort

# insert sort
insertSort = function(vector){
  n = length(vector)
  for(i in 2:n){
    markValue = vector[i]
    j=i-1
    while(j>0){
      if(vector[j]>markValue){
        vector[j+1] = vector[j]
        vector[j] = markValue
      }
      j=j-1
    }
  }
  return(vector)
}
insertSort(vector)

##  [1]   3   5   6  10  11  14  25  34  36  43  59  65  67  69 785

6. Hill Sorting

# shell sort
shellSort = function(vector){
   n = length(vector)
   separate = floor(n/2)
   while(separate>0){
     for(i in 1:separate){
       j = i+separate
       while(j<=n){
         m= j- separate
         markVlaue = vector[j]
         while(m>0){
           if(vector[m]>markVlaue){
             vector[m+separate] = vector[m]
             vector[m] = markVlaue
           }
           m = m-separate
         }
         j = j+separate
       }
     }
     separate = floor(separate/2)
   }
   return(vector)
}
shellSort(vector)

##  [1]   3   5   6  10  11  14  25  34  36  43  59  65  67  69 785

7. Select Sort

# select sort
selectSort = function(vector){
  n = length(vector)
  for(i in 1:(n-1)){
    minIndex = i
    for(j in (i+1):n){
      if(vector[minIndex]>vector[j]){
        minIndex = j
      }
    }
    temp = vector[i]
    vector[i] = vector[minIndex]
    vector[minIndex] = temp
  }
  return(vector)
}
selectSort(vector)

##  [1]   3   5   6  10  11  14  25  34  36  43  59  65  67  69 785

8. Heap Sorting

# heap sort
adjustHeap = function(vector,k,n){
  left = 2*k
  right = 2*k+1
  max = k
  if(k<=n/2){
    if(left<=n&&vector[left]>=vector[max]){
      max = left
    }
    if(right<=n&&vector[right]>=vector[max]){
      max = right
    }
    if(max!=k){
      temp = vector[k]
      vector[k] = vector[max]
      vector[max] = temp
      vector = adjustHeap(vector,max,n)
    }
  }
  return(vector)
}
createHeap = function(vector,n){
  for(i in (n/2):1){
    vector = adjustHeap(vector,i,n)
  }
  return(vector)
}
heapSort = function(vector){
  n = length(vector)
  vector = createHeap(vector,n)
  for(i in 1:n){
    temp = vector[n-i+1]
    vector[n-i+1] = vector[1]
    vector[1] = temp
    vector = adjustHeap(vector,1,n-i)
  }
  return(vector)
}

9. Merge Sort

# merge sort
combine = function(leftSet,rightSet){
  m = 1
  n = 1
  vectorTemp = c()
  while (m<=length(leftSet)&&n<=length(rightSet)) {
    if(leftSet[m]<=rightSet[n]){
      vectorTemp = append(vectorTemp,leftSet[m])
      m = m+1
    }else{
      vectorTemp = append(vectorTemp,rightSet[n])
      n = n+1
    }
  }
  if(m>length(leftSet)&&n==length(rightSet)){
    vectorTemp = append(vectorTemp,rightSet[n:length(rightSet)])
  }else if(m==length(leftSet)&&n>length(rightSet)){
    vectorTemp = append(vectorTemp,leftSet[m:length(leftSet)])
  }
  return(vectorTemp)
}
mergeSort = function(vector){
  size = length(vector)
  if(size==1){
    return(vector)
  }
    cut = ceiling(size/2)
    leftSet = mergeSort(vector[1:cut])
    rightSet = mergeSort(vector[(cut+1):size])
    vector = combine(leftSet,rightSet)
    return(vector)
}

Topics: R Language Java shell