- vừa được xem lúc

Radix Sort

0 0 10

Người đăng: Sujoy Datta

Theo Viblo Asia

Sorting is an important element in modern computer science. It is found in almost every task related to vast areas of computer technology. Today i am gonna write about a least popular searching algorithm named Radix Sort.

Radix Sort

Radix sort is a non-comparative sorting algorithm which works by grouping keys by the individual digits which share the same position and value. In 1887, Herman Hollerith invented this sorting algorithm when he was working on tabulating machine. Some of the well known compare based sorting algorithm is - Merge sort, Quick sort etc. The lowest complexity for these algorithms are O(nlogn). So we cant get better performance than nlogn from them. Counting sort do linear time sorting. So for n elements, complexity of counting sort is O(n). But what if there are n^2 elements? The performance of counting sort will be worst. In that case Radix sort is the better solution. Radix sort starts its operation from the least significant digit and end up when there is no more digit is left to compare.

Working Algorithm

The mechanism of Radix sort algorithm is as following - For each digit of of all the numbers -

  • sort each number using counting or any other sort. if 2 or 3 number's considered digit is same, they will be arranged based of there position in previous array.
  • continue doing it until the algorithm reach most significant bit or left-most bit.
  • is case of a number which has no bit left to compare with, assume the comparable digit is 0

Let there be d digits in input integers. Radix Sort takes O(d*(n+b)) time where b is the base for representing numbers, for example, for decimal system, b is 10. What is the value of d? If k is the maximum possible value, then d would be O(logb(k)). So overall time complexity is O((n+b) * logb(k)). Which looks more than the time complexity of comparison based sorting algorithms for a large k. Let us first limit k. Let k <= n^c where c is a constant. In that case, the complexity becomes O(nLogb(n)). But it still doesn’t beat comparison based sorting algorithms. What if we make value of b larger?. What should be the value of b to make the time complexity linear? If we set b as n, we get the time complexity as O(n). In other words, we can sort an array of integers with range from 1 to nc if the numbers are represented in base n (or every digit takes log2(n) bits).

Example

Lets Demonstrate an example: Assume that our un-sorted array that has to be sorted is A and

A = [120, 90, 7, 155, 75]

So for better understanding we will convert them to number of same digits. The maximum digit of numbers in array A is 3. So our array becomes

A = [120, 090, 007, 155, 075]

So in first iteration, the left-most digits will be in consideration. So considering them our array will be note that we place 120 ahead of 90 in first iteration because 120 comes first in original array.

A = [120, 090, 155, 075, 007]

Now in the second iteration, only the middle digits will be taken into consideration. So our array will be

A = [007, 120, 155, 075, 090]

And in the last but not the least iteration, we will get our disirable sorted array. and the sorted array is

A = [007, 075, 090, 120, 155]

Implementation

Below is the ruby implementation of radix sort

def counting_sort(input_arr, exp = 0) count_arr = Array.new(10, 0) result = Array.new(input_arr.size) m = 10**(exp+1) n = 10**exp input_arr.each do |item| signif_number = (item%m)/n count_arr[signif_number] += 1 end for i in 1...10 count_arr[i] = count_arr[i-1] + count_arr[i] end i = input_arr.size - 1 until i < 0 do item = input_arr[i] signif_number = (item%m)/n count_arr[signif_number] -= 1 result[count_arr[signif_number]] = item i -= 1 end result
end def radix_sort(a) max_num = a.max exp = 0 until max_num == 0 do exp += 1 max_num = max_num/10 end for i in 0...exp do a = counting_sort(a, i) end a
end

thats all from me about radix sort. Hope this article helps you to learn more. Thank you.

Bình luận

Bài viết tương tự

- vừa được xem lúc

Bài toán tìm đường đi ngắn nhất với giải thuật Dijkstra

Với các bạn sinh viên chuyên ngành công nghệ thông tin, chắc không lạ gì với bài toán tìm đường đi ngắn nhất (Shortest Path Problems) trong đồ thị trọng số nữa. Ở bài viết lần này, mình sẽ làm 3 việc:.

0 0 123

- vừa được xem lúc

Tôi cá là bạn không biết những điều này - Ruby on rails ( Phần 2)

Các bạn có thể theo dõi phần 1 ở đây :. https://viblo.asia/p/toi-ca-la-ban-khong-biet-nhung-dieu-nay-ruby-on-rails-phan-1-WAyK8DDeKxX. 5.

0 0 212

- vừa được xem lúc

Những thay đổi trong ruby 3.0

. 2020 là một năm lớn đối với cộng đồng Ruby. Những người sáng lập Ruby có một món quà thực sự tuyệt vời cho chúng ta vào giáng sinh với việc phát hành Ruby 3.

0 0 34

- vừa được xem lúc

Có gì đặc biệt trong phiên bản Ruby 3x3 ?

Hello guys, chắc hẳn thời gian vừa rồi chúng ta cũng đã nghe qua thông tin Ruby sắp cho ra mắt Ruby version 3, hay còn được gọi là ruby 3x3, vậy liệu Ruby version 3 này có gì mới, và có những update nào đáng phải kể đến, và tại sao mọi người lại gọi nó là ruby version 3x3, thì trong bài ngày hôm nay

0 0 30

- vừa được xem lúc

Cách sử dụng class Time & Date trong Ruby (Phần 1)

Time là một class trong Ruby, nó sẽ giúp chỉnh sửa format, trích xuất thông tin một cách hiệu quả theo ý của bạn. . Topic hôm nay chúng ta có gì nào. .

0 0 84

- vừa được xem lúc

Ruby 3.0 có gì mới

Ruby 3.0.0 đã được ra mới được ra mắt vào tháng 12/2020, mục tiêu của bản 3.0.

0 0 28