-
-
Save LifeMoroz/7389440 to your computer and use it in GitHub Desktop.
All is lost
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <math.h> | |
int digit(long long A, long long shift){ | |
return (int)(A / shift)%(sizeof(long long)*8); | |
} | |
template<class T> | |
void count_sort(T *a, T* vtemp, int n, long long shift, int (*dig)(T,T)){ | |
const int digits = sizeof(T) * 8; | |
std::vector<T> positions(digits); | |
std::vector<T> counts(digits); | |
for (int i = 0; i < n; ++i) { | |
++counts[dig(a[i],shift)]; | |
} | |
int position = 0; | |
for (int bucket = 0; bucket < digits; ++bucket) { | |
positions[bucket] += position; | |
position += counts[bucket]; | |
} | |
for (int i = 0; i < n; ++i) { | |
vtemp[positions[dig(a[i],shift)]] =a[i]; | |
++positions[dig(a[i],shift)]; | |
} | |
for(int i =0; i<n;i++) | |
a[i] = vtemp[i]; | |
} | |
template<class T> | |
void LSD_sort(T *a, int n, int (*dig)(T,T)){ | |
const int digits = sizeof(T) * 8; //can't be > int | |
long long shift = 1; | |
const int RANGE = (int)ceil(log(pow(2, sizeof(T) * 8) - 1) / log(digits)); | |
T *vtemp = new T[n]; | |
for (int pos = 0; pos < RANGE; ++pos) { | |
count_sort(a,vtemp,n,shift,dig); | |
shift *= digits; | |
} | |
delete [] vtemp; | |
} | |
template<class T> | |
void LSD_sort(T *first, T*last, int (*dig)(T,T)){ | |
LSD_sort(first,last-first,dig); | |
} | |
int main() | |
{ | |
int SIZE; | |
std::cin >> SIZE;; | |
long long* data = new long long[SIZE]; | |
for (int i = 0; i < SIZE; i++) | |
std::cin >> data[i]; | |
LSD_sort(data, SIZE,digit); | |
for (int i = 0; i < SIZE; i++) | |
std::cout << data[i] << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment