LINUX.ORG.RU

Segmentation fault (Core dumped)

 


0

3
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <cassert>
#include <cctype>
#include <cmath>


using namespace std;

class massgen
{
	public:
		int size;
		int *arr;
		int rand_size;

		void massprint()
		{
			cout << "Введите размер массива: ";
			cin >> size;

			arr = new int [size];
			//int *arr = (int *) malloc(size);

			if (size >= 20) {
            cout << "Введите размер рандомизации массива: ";
            cin >> rand_size;

				if (rand_size == 0) {
					cout << "Размер рандомизации массива не может быть = 0";
					exit(false);
				}

				cout << "Сгенерированный неотсортированный массив: " << endl;
				for (int i = 0; i < size; i++) {
					arr[i] = rand() % rand_size;
					cout << arr[i] << " ";
				}
			}

			if (size >= 2 && size < 20)
			{
				rand_size = 1;
				for (int i = 0; i < size; i++) {
					cout << "arr[" << i << "] = ";
					cin >> arr[i];
				}
			}

			else if (size < 2)
			{
				cout << "Wrong massive size";
				rand_size = 3;
			}
		}

		void test()
		{
			if (size >= 2)
			{
				cout << endl << "=== Test Cases ===" << endl;
				assert(size > 0);
				assert(rand_size > 0);
				cout << "Тест кейсы сработали";
			}
		}

		void configprint()
		{
			if (size >= 2)
			{
				ofstream cfgwrite;
				cfgwrite.open("configure.txt");
				cfgwrite << "[massive_size] = " << size << endl;
				cfgwrite << "[rand_size] = " << rand_size;
				cfgwrite.close();
				cout << endl << "Данные записаны в конфигурационный файл";
			}
		}

		void massfile()
		{
			ofstream fout;
			fout.open("massive.txt");
			fout << "Массив: " << endl;
			for (int i = 0; i < size; ++i) {
				fout << arr[i] << " ";
			}
			fout.close();
			cout << endl
             << "Массив успешно записан в файл" << endl;
		}
};

class sorts:massgen
{
	public:
		double start;
		double end;
		double time1;
		double start2;
		double end2;
		double time2;

		void bubblesort()
		{
			cout << "==== Массив, сортированный методом пузырька ====" << endl;
			start = clock();

			int buff = 0;

			for (int i = 0; i < size - 1; i++)
			{
				for (int j = size - 1; j>i; j--)
				{
					if (arr[j] < arr[j-1])
					{
						buff = arr[j-1];
						arr[j-1] = arr[j];
						arr[j] = buff;
					}
				}
			}

			end = clock();
			time1 = (end - start) / CLOCKS_PER_SEC;

			// вывод массива

			ofstream bubblefile;
			bubblefile.open("bubble.txt");
			bubblefile << "Массив, отсортированный сортировкой Шелла:" << endl;
			for (int i = 0; i < size; i++) {
				cout << arr[i] << " ";
				bubblefile << arr[i] << " ";
			}
			bubblefile << endl
                  << "Время работы сортировки = " << time1 << " сек";

			cout << endl
             << "Сортированный массив выведен в файл bubble.txt";
		}

		void shellsort()
		{
			cout << endl << "==== Массив, сортированный методом Шелла ====" << endl;
			int i, j, step;
			int tmp;

			start2 = clock();

			for (step = size / 2; step > 0; step /= 2) {
				for (i = step; i < size; i++) {
					tmp = arr[i];
					for (j = i; j >= step; j -= step) {
						if (tmp < arr[j - step])
							arr[j] = arr[j - step];
						else
							break;
					}
					arr[j] = tmp;
				}
			}

			end2 = clock();
			time2 = (end2 - start2) / CLOCKS_PER_SEC;

			// вывод массива

			ofstream shellfile;
			shellfile.open("shell.txt");
			shellfile << "Массив, отсортированный сортировкой Шелла:" << endl;
			for (int i = 0; i < size; i++) {
				cout << arr[i] << " ";
				shellfile << arr[i] << " ";
			}
			shellfile << endl
                  << "Время работы сортировки = " << time2 << " сек";

			cout << endl
             << "Сортированный массив выведен в файл shell.txt";
		}

		void timeprint()
		{
			cout << endl << "Время работы пузырьковой сортировки = " << time1 << "сек." << endl;
			cout << endl << "Время работы сортировки Шелла = " << time2 << "сек." << endl;
		}
};

class remove1:massgen
{
	public:
		void delarr()
		{
			delete[] arr;
			cout << "Массив очищен";
		}
};

int main(int argc, char *argv[])
{
	massgen array;
	array.massprint();
	array.test();
	array.configprint();
	array.massfile();
	sorts print;
	print.bubblesort();
	print.shellsort();
	print.timeprint();
	remove1 call;
	call.delarr();
	return 0;
}

выдаёт ошибку «Segmentation fault (Core dumped)».

где допустил ошибку?



Последнее исправление: featurea (всего исправлений: 2)

Ответ на: комментарий от zamazan4ik

а давайте что бы продолжать этот не понятный дискус, вы скажете в каких случаях cache line для quicksort даст прирост по сравнению с другими сортировками ?
потому что если вы не понимаете, то продолжать дискус нет смысла
по вашей ссылке http://warp.povusers.org/SortComparison/integers.html нет самого главного уточнения

anonymous
()
Ответ на: комментарий от anonymous

Не понимаю, почему Вы меряете производительность только дружелюбностью к кешу, но бенчмарки в защиту своей позиции так и не приводите.

zamazan4ik ★★
()
Ответ на: комментарий от zamazan4ik

фишка в том что я вас пытаюсь заставить понять и разобраться, а вы всего лишь пытаетесь мне что то доказать
причем то что я и без вас знаю
тыкать в вас графиками это пытаться вам что то доказать, после чего вы сразу все поймете
но так уже будет совсем не интересно

anonymous
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.