Регистрация: 02.10.2018
Сообщений: 6
|
Цифровая сортировка
Здраствуйте, дано задание рандомом набрать числа в текст, и при помощи цифровой сортировке отсортировать весь набор чисел во 2 текст. У меня вопрос, почему в коде, привиденном ниже это работает только до 720 чисел?Нужно до 1 миллиона
код
Код:
#include "iostream"
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <fstream>
#include <ctime>
#include <stdio.h>
using namespace std;
// Function to find largest element
int max(int a[], int n)
{
int max = a[0], i;
for(i = 1; i < n; i++)
{
if(max < a[i])
max = a[i];
}
return max;
}
// Function to perform sorting
void RadixSort(int a[], int n)
{
int bucket[n][n], bucket_cnt[n];
int i, j, k, r, NOP=0, divisor=1, lar, pass;
lar=max(a, n);
while(lar > 0)
{
NOP++;
lar/=10;
}
/*Initialize the buckets*/
for(pass = 0; pass < NOP; pass++)
{
for(i = 0; i < 10; i++)
{
bucket_cnt[i] = 0;
}
//sort the numbers according to the digit & place it into specified bucket
for(i = 0; i < n; i++)
{
r = (a[i] / divisor) % 10;
bucket[r][bucket_cnt[r]] = a[i];
bucket_cnt[r] += 1;
}
//Collect the numbers after completed each pass
i = 0;
for(k = 0; k < 10; k++)
{
for(j = 0; j < bucket_cnt[k]; j++)
{
a[i] = bucket[k][j];
i++;
}
}
divisor *= 10;
}
}
//Entry point of program
int main()
{
setlocale(0,"");
int i, n;
cout << "Введите диапазон чисел (от 0 до n) n= " <<endl;
cin >> n;
int *a=new int [n]; // Массив куда будем записывать числа (от INT_MIN до INT_MAX)*/
//РАНДОМ НАЧИНАЕТСЯ ЗДЕСЬ--------------------------------------------------------------------------------------------------------
int array1[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // Единицы
int array2[10] = {0, 10, 20, 30, 40, 50, 60, 70, 80, 90}; // Десятки
int array3[10] = {0, 100, 200, 300, 400, 500, 600, 700, 800, 900}; // Сотни
int array4[10] = {0, 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000}; // Тысячи
int array5[10] = {0, 10000, 20000, 30000, 40000, 50000, 60000, 70000, 80000, 90000}; // Десятки тысяч
int array6[10] = {0, 100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000, 900000}; // Сотни тысяч
int array7[10] = {0, 1000000, 2000000, 3000000, 4000000, 5000000, 6000000, 7000000, 8000000, 9000000}; // Миллион
int index1,
index2,
index3,
index4,
index5,
index6,
index7,
index8,
index9,
index10,
SumNumber;
for(int i = 0; i < n; i++){
index1 = rand() % 10; // Значения от '0' до '10' Всего 10 значений. Для индекса массива.
index2 = rand() % 10;
index3 = rand() % 10;
index4 = rand() % 10;
index5 = rand() % 10;
index6 = rand() % 10;
index7 = rand() % 10;
if(n<=10){
SumNumber=array1[index1];
a[i] = SumNumber % n;
}
else{
if(n<=100){
SumNumber=array2[index2] + array1[index1];
a[i] = SumNumber % n;
}
else{if(n<=1000){
SumNumber=array3[index3] + array2[index2] + array1[index1];
a[i] = SumNumber % n;
}
else{
if(n<=10000){
SumNumber=array4[index4] + array3[index3] + array2[index2] + array1[index1];
a[i] = SumNumber % n;
}
else{
if(n<=100000){
SumNumber=array5[index5] +array4[index4] + array3[index3] + array2[index2] + array1[index1];
a[i] = SumNumber % n;
}
else{
if(n<=1000000){
SumNumber=array6[index6] + array5[index5] +array4[index4] + array3[index3] + array2[index2] + array1[index1];
a[i] = SumNumber % n;
}
else{
if(n>=1000000){
SumNumber=array7[index7] + array6[index6] +array5[index5] +array4[index4] + array3[index3] + array2[index2] + array1[index1];
a[i] = SumNumber % n;
}
}}}}}}}
//РАНДОМ ЗАКАНЧИВАЕТСЯ ЗДЕСЬ--------------------------------------------------------------------------------------------------------
ofstream out("D:\\цифровая\\1.txt");
// Вывод элементов массива до сортировки
for (int i = 0; i<n; i++)
out << a[i] << endl;
out.close();
RadixSort(a,n);
ofstream out2("D:\\цифровая\\2.txt");
for (int i = 0; i<n; i++)
out2 << a[i] << endl;
out2.close();
getchar();
return 0;
}
|