Last active
October 24, 2017 09:21
-
-
Save darhonbek/6a2469ba526ad6ee61370af893ed8835 to your computer and use it in GitHub Desktop.
String Radix sort. For strings containing only lowercase alphabet characters.
This file contains 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 <string> | |
using namespace std; | |
template <class T> | |
void print(T *arr, int n) { | |
for(register int i=0; i<n; i++) { | |
cout<<arr[i]<<" "; | |
} | |
cout<<"\n"; | |
} | |
void stringRadixSort(string *arr, int n) { | |
vector<string> bucks[27]; | |
int pass = 0; | |
for(register int i=0; i<n; i++) { | |
if(arr[i].size() > pass) | |
pass = arr[i].size(); | |
} | |
register int position = 0; | |
while(pass > 0) { | |
for(register int i=0; i<n; i++) { | |
if(arr[i].size() < pass) { | |
bucks[0].push_back(arr[i]); | |
} else { | |
position = arr[i].at(pass-1) - 97 + 1; | |
bucks[position].push_back(arr[i]); | |
} | |
} | |
for(register int i=0, k=0; i<27, k<n; i++) { | |
for(register int j=0; j<bucks[i].size(); j++) { | |
arr[k] = bucks[i].at(j); | |
k++; | |
} | |
bucks[i].clear(); | |
} | |
pass--; | |
} | |
} | |
int main() { | |
string arr[] = {"bak", "aef", "duck", "bak", "jack", "london", "vachach"}; | |
stringRadixSort(arr, 7); | |
print(arr, 7); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment