Skip to content

Instantly share code, notes, and snippets.

@tjarrow
Created June 24, 2017 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjarrow/bd94a916f844dffcc49cd8ff2ce5fc3b to your computer and use it in GitHub Desktop.
Save tjarrow/bd94a916f844dffcc49cd8ff2ce5fc3b to your computer and use it in GitHub Desktop.
#include "iostream"
#include "locale.h"
#include "windows.h"
#include <stdio.h>
using namespace std;
int n, digit_amt = 0; //digit amount - количество разрядов
int max_digit(int num)
{
int max = 0;
while (num > 1)
{
num /= 16;
max++;
}
return max;
}
int digit_value(int num, int digit)
{
while (digit>1)
{
num /= 16;
digit--;
}
return num % 16;
}
void radix_sort(int **dop_mas, int *mas, int digit)
{
int *mas_col, i, j, temp = 0; //mas_col - в скольки числах есть определенная цифра (напр., mas_col[5] = 2 - в двух числах есть пятерка
mas_col = new int[16]; //Здесь падает
for (i = 0; i<16; i++)
mas_col[i] = 0;
for (i = 0; i<n; i++)
{
int a = digit_value(mas[i], digit);
dop_mas[mas_col[a]][a] = mas[i]; //dop_mas заполняется неправильно
mas_col[a]++;
}
for (i = 0; i<n; i++)
{
for (j = 0; j<mas_col[i]; j++)
{
mas[temp] = dop_mas[j][i];
temp++;
}
}
}
int main()
{
int digit, i, *mas, **dop_mas;
cout « "Razmer massiva: " « endl;
cin » n;
mas = new int[n];
cout « "Elementy massiva: " « endl;
for (i = 0; i<n; i++)
{
cout « "[" « i + 1 « "]= ";
scanf("%x", &mas[i]);
}
dop_mas = new int*[n];
for (i = 0; i<n; i++)
dop_mas[i] = new int[n]; //Здесь скорее всего не n
for (i = 0; i<n; i++)
if (digit_amt < max_digit(mas[i]))
digit_amt = max_digit(mas[i]); //3
for (digit = 1; digit <= digit_amt; digit++) //
radix_sort(dop_mas, mas, digit);
for (i = 0; i<n; i++)
cout « hex « mas[i] « endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment