#include <iostream>
#include <vector>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
using
namespace
std;
int
bits = 32;
void
dump_array(vector<
int32_t
> array);
void
bin_dump(
int32_t
v);
void
inplace_radix_sort_bit(vector<
int32_t
> &a,
int
start,
int
end,
int
bit,
int
&breakpoint);
void
swap(vector<
int32_t
> &a,
int
l,
int
r) {
int32_t
x = a[l];
a[l] = a[r];
a[r] = x;
}
bool
bit_is_set(
int32_t
v,
int
bit) {
if
((v & (1 << bit)) > 0)
return
true
;
else
return
false
;
}
void
inplace_radix_sort(vector<
int32_t
> &a) {
int
breakpoint;
vector<
int
> breakpoints;
inplace_radix_sort_bit(a,0,a.size(),bits-1,breakpoint);
breakpoints.push_back(0);
breakpoints.push_back(breakpoint);
breakpoints.push_back(a.size());
for
(
int
bit=bits-2;bit>=0;bit--) {
cout <<
"breakpoints: "
;
for
(
int
n=0;n<breakpoints.size();n++) cout << breakpoints[n] <<
" "
;
cout << endl;
vector<
int
> newbreakpoints;
for
(
int
n=0;n<breakpoints.size()-1;n++) {
if
(breakpoints[n] != breakpoints[n+1])
inplace_radix_sort_bit(a,breakpoints[n],breakpoints[n+1]-1,bit,breakpoint);
newbreakpoints.push_back(breakpoint);
}
vector<
int
> mergebreakpoints;
for
(
int
n=0;n<breakpoints.size();n++) {
mergebreakpoints.push_back(breakpoints[n]);
if
(n!=(breakpoints.size()-1)) mergebreakpoints.push_back(newbreakpoints[n]);
}
breakpoints = mergebreakpoints;
vector<
int
> cleanedbreakpoints;
cleanedbreakpoints.push_back(breakpoints[0]);
for
(
int
n=1;n<breakpoints.size();n++) {
if
(breakpoints[n] != breakpoints[n-1]) cleanedbreakpoints.push_back(breakpoints[n]);
}
breakpoints = cleanedbreakpoints;
}
}
void
inplace_radix_sort_bit(vector<
int32_t
> &a,
int
start,
int
end,
int
bit,
int
&breakpoint) {
cout <<
"sorting bit: "
<< bit <<
" pos: "
<< start <<
" "
<< end << endl;
int
l_pos = start;
int
r_pos = end;
for
(;l_pos < r_pos;) {
cout <<
"l_pos: "
<< l_pos <<
" r_pos: "
<< r_pos << endl;
cout <<
"comparing: "
<< a[l_pos] <<
" "
<< a[r_pos] <<
" "
;
bin_dump(a[l_pos]);
cout <<
" "
;
bin_dump(a[r_pos]);
cout << endl;
bool
l_bit = bit_is_set(a[l_pos],bit);
bool
r_bit = bit_is_set(a[r_pos],bit);
if
(l_bit) cout <<
"l_bit: 1"
<< endl;
else
cout <<
"l_bit: 0"
<< endl;
if
(r_bit) cout <<
"r_bit: 1"
<< endl;
else
cout <<
"r_bit: 0"
<< endl;
if
(!l_bit && r_bit) { swap(a,l_pos,r_pos); l_pos++; r_pos--; cout <<
"swp"
<< endl;
if
(l_pos == r_pos)
if
(bit_is_set(a[l_pos],bit)) {l_pos++;
break
;}}
else
if
( l_bit && !r_bit) { l_pos++; r_pos--; cout <<
"10 linc rdec"
<< endl; }
else
if
( l_bit && r_bit) { l_pos++; cout <<
"11 linc"
<< endl;
if
(l_pos == r_pos) {l_pos++;
break
;}}
else
if
(!l_bit && !r_bit) { r_pos--; cout <<
"00 rdec"
<< endl;
if
(l_pos == r_pos) {
break
;}}
}
breakpoint = l_pos;
dump_array(a);
}
void
bin_dump(
int32_t
v) {
for
(
int
n=bits-1;n>=0;n--) {
if
(v & (1 << n)) cout <<
"1"
;
else
cout <<
"0"
;
}
}
void
dump_array(vector<
int32_t
> array) {
for
(
int
n=0;n<array.size();n++) {
printf
(
"%20d "
,array[n]);
bin_dump(array[n]);
cout << endl;
}
}
int
main() {
vector<
int32_t
> array;
for
(
int
n=0;n<20;n++) {
array.push_back(
rand
());
}
cout <<
"random array"
<< endl;
dump_array(array);
inplace_radix_sort(array);
cout <<
"sorted array"
<< endl;
dump_array(array);
}