Hi! I have to do a project for my studies. It is a insertion algorithm.
I'm reading data from file with using vector.
then I'm copying vector to a normal array (the reason is simple: I don't know how to send vector to function, but this is not a main problem).
After copying, I'm sending (with using pointers) array to sorting function.
Everything is good, algorithm is working but! (yep, there is always but... Wink
When I save a lot of data to file, program just after start crashes.
The numbers in file are written in that way:
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10
So there are 40 numbers in one line. I have 4.000 lines, so after simple math we have 160.000 numbers. The main problem: when I add some new numbers for example 40.000 program crashes I must have more numbers because the task is to compare few sorting algorithms with minimum 15 minutes of sorting. (160.000 digits is sorting in ~1 minute)
Here's the code. Maybe it is poor written, but I did my best.
//written with Qt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include <QtCore/QCoreApplication>
#include <iostream>
#include <time.h>
#include <iomanip>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
//sorting function
void insertion_sort(int *wsk, unsigned long size);
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//vector to hold data from file
vector <int> array_vector;
//stream to file
ifstream plik ("numbers.txt",ios::in);
//single number that is being readed from file
unsigned long single_number;
while(plik >> single_number)
{
array_vector.push_back(single_number);
}
int array_nonsorted[array_vector.size()];
//copying from vector to array
for (unsigned long a = 0; a < array_vector.size(); a++)
{
array_nonsorted[a] = array_vector[a];
}
time_t start,end; double long dif;
cout << "START\n";
time(&start);
insertion_sort(array_nonsorted,array_vector.size());
time(&end);
cout << "STOP\n";
dif = difftime(end,start);
cout << "Array sorted in " << setprecision(5) << dif << "sec"; dif = dif/60; cout << " = " << dif << "minutes";
//cout << "End.";
return a.exec();
}
void insertion_sort(int *wsk, unsigned long size)
{
int tab_sorted[size];
//In final version I will do this by using pointers. I'm just copying it because with using pointers it's hard to see data in debugger
for(unsigned long i = 0; i < size; i++)
{
tab_sorted[i] = wsk[i];
}
//main sort engine
for (unsigned long a = 1; a < size; a++)
{
if (a == 1)
{
if (tab_sorted[0] > tab_sorted[1])
{
unsigned long temp = tab_sorted[0];
tab_sorted[0] = tab_sorted[1];
tab_sorted[1] = temp;
}
}
else
{
for (unsigned long b = 0; b <= a-1; b++)
{
if (tab_sorted[a] < tab_sorted[b])
{
unsigned long temp = tab_sorted[a];
for (unsigned long c = a; c > b; c--)
{
tab_sorted[c] = tab_sorted[c-1];
}
tab_sorted[b] = temp;
}
}
}
}
}
| |
So why the program crashes? I don't have a clueFrown Something with memory (but I have 3.0GB and OS works normally when program is starting). Please help!
Luk.