-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathradix-sort - Copy - Copy.cpp
68 lines (62 loc) · 1.3 KB
/
radix-sort - Copy - Copy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//============================================================================
// Name : radix-sort.cpp
// Author : Derek Wene
// Date :
// Copyright : Sure, lets say its copyrighted.
// Description : Implementation of radix sort in C++
//============================================================================
#include "sort.h"
#include <iostream>
#include <cmath>
using namespace std;
void
RadixSort::sort(int A[], int size)
{
int digits=0;
int x=0;
for(int i =0; i<size; ++i)//Find largest number
{
if(A[i]>digits)
digits=A[i];
}
digits = floor(log(digits));//find highest digit number
int B[10];
int C[size];
int D[size];
int power=1;
while(power <= digits)//Get all the digits in the array
{
for(int n = 0; n < 10; ++n)
{
B[n] = 0;
}
for(int j=0; j<size; ++j)//Set digits
{
D[j] = (A[j]/((int)pow(10.0,power)))%10;
}
//Counting sort starts here
for(int k=0; k<size; ++k)//Set number of each digit.
{
B[D[k]]++;
}
int total = 0;
for(int l=0; l<10; ++l)//Do the less than equal part.
{
int temp = B[l];
B[l] = total;
total += temp;
}
for(int m=0; m<size; ++m)//Actual Sorting Here
{
C[B[D[m]]] = A[m];
++B[D[m]];
}
for(int n=0; n<size; ++n)//reset A with new sorted part
{
A[n] = C[n];
C[n] = 0;
D[n] = 0;
}
++power;
}
}