TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
|
Algorithm of Radix sort More...
#include <algorithm>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <vector>
Go to the source code of this file.
Namespaces | |
namespace | sorting |
for working with vectors | |
namespace | radix_sort |
Functions for Radix sort algorithm. | |
Functions | |
std::vector< uint64_t > | sorting::radix_sort::step_ith (uint16_t cur_digit, const std::vector< uint64_t > &ar) |
Function to sort vector according to current digit using stable sorting. | |
std::vector< uint64_t > | sorting::radix_sort::radix (const std::vector< uint64_t > &ar) |
Function to sort vector digit by digit. | |
static void | tests () |
Function to test the above algorithm. | |
int | main () |
Main function. | |
Algorithm of Radix sort
Sort the vector of unsigned integers using radix sort i.e. sorting digit by digit using Counting Sort as subroutine. Running time of radix sort is O(d*(n+b)) where b is the base for representing numbers and d in the max digits in input integers and n is number of unsigned integers. consider example for n = 5, aray elements = 432,234,143,332,123 sorting digit by digit sorting according to 1) 1st digit place => 432, 332, 143, 123, 234
2) 2nd digit place => 123, 432, 332, 234, 143
3) 3rd digit place => 123, 143, 234, 332, 432
using count sort at each step, which is stable. stable => already sorted according to previous digits.
Definition in file radix_sort2.cpp.
int main | ( | void | ) |
Main function.
Definition at line 119 of file radix_sort2.cpp.
std::vector< uint64_t > sorting::radix_sort::radix | ( | const std::vector< uint64_t > & | ar | ) |
Function to sort vector digit by digit.
ar | - vector to be sorted |
Definition at line 82 of file radix_sort2.cpp.
std::vector< uint64_t > sorting::radix_sort::step_ith | ( | uint16_t | cur_digit, |
const std::vector< uint64_t > & | ar ) |
Function to sort vector according to current digit using stable sorting.
cur_digit | - sort according to the cur_digit |
ar | - vector to be sorted |
Definition at line 51 of file radix_sort2.cpp.
|
static |
Function to test the above algorithm.
Test 1
Test 2
Definition at line 104 of file radix_sort2.cpp.