Блог пользователя MohammadParsaElahimanesh

Автор MohammadParsaElahimanesh, 13 месяцев назад, По-английски

Introduction

Radix sort is a sorting algorithm that sorts elements by grouping them based on their digits. It is a non-comparative sorting algorithm that is faster than many other sorting algorithms, including the default std::sort function in C++. In this blog post, we will explore how radix sort works and how to implement it in C++.

How Radix Sort Works

In radix sort, we sort elements based on their digits, starting from the least significant digit to the most significant digit. We group the elements based on each digit and sort them accordingly. This process is repeated for each digit until all digits have been considered. The result is a sorted list of elements.

Implementing Radix Sort in C++

Here's an implementation of radix sort in C++:

In this implementation, we first check if the input values are negative. If they are, we convert them to positive values, sort them, and then convert them back to negative values. We then loop through blocks of digits, starting from the least block, and sort the elements based on that block. We use a counting sort algorithm to sort the elements based on each block.

Performance Benchmarks

To compare the performance of radix sort with other sorting algorithms, we ran a benchmark test on several arrays of integers. Here are the results:

Array Size Data Type Radix Sort Time (ms) std::sort Time (ms)
1000 64-bit integers 0.503 0.098
1,000,000 64-bit integers 84.32 177.304
100,000,000 64-bit integers 8119.51 21297.8
1000 32-bit integers 0.296 0.088
1,000,000 32-bit integers 38.482 170.115
100,000,000 32-bit integers 4418.55 20673.5
1000 integers in range [0, 1e6) 0.159 0.098
1,000,000 integers in range [0, 1e6) 17.277 168.051
100,000,000 integers in range [0, 1e6) 2098 17265.4

As you can see, radix sort is faster than the default std::sort function in C++ for most of the test cases.

Conclusion

Radix sort is a powerful sorting algorithm that can be used to sort elements faster than many other sorting algorithms. In this blog post, we explored how radix sort works and how to implement it in C++. We also compared the performance of radix sort with other sorting algorithms and found that it is faster than the default std::sort function in C++ for most test cases. I hope this blog post has been helpful to you!

You can find the source code and every things on https://github.com/Mohammad-Parsa-Elahimanesh/Radix-sort-Cpp.

If you have any opinion or criticism or if the code can be improved in terms of beauty or functionality, I would be very grateful to say it in the comments.

at end thanks Bing Ai to help me write this blog.

I hope to be useful for you!

  • Проголосовать: нравится
  • +81
  • Проголосовать: не нравится

»
13 месяцев назад, # |
Rev. 3   Проголосовать: нравится +6 Проголосовать: не нравится

Thanks for the blog. I like to use a faster version of radix sort called Ska sort, designed by Malte Skarupke, the details of which can be found here. The link to the implementation on github is in the Summary section of that blog post. The relevant functions are ska_sort and ska_sort_copy — both of these do slightly different things and usually the latter is better, but it makes sense to try out both for each usecase.

The cool thing about this implementation is that it can sort even floating point types, and arbitrary types if you can provide a function that computes the key corresponding to a given element.