4 курс / Програмная инженерия / Сортир. масив. пузырек. треуг
..doc/* Назначение: Провести сортировку заданного массива различными методами (пузырьком и метод треугольника) и сравнить время выполнения сортировок.
Автор: Русаков Д. Группа П-23.
17.11.15 */
#include<ctime>
#include<iostream>
#include<time.h>
#include<fstream>
using namespace std;
const int n=10000; //Размерность массива
int mas[n]; // Объявление массива
void Input() //Инициализация массива
{
int i = 0;
for (int i = 0; i<n; i+=10)
{
mas[i] = rand() % 10000 + 1;
mas[i+1] = rand() % 10000 + 1;
mas[i+2] = rand() % 10000 + 1;
mas[i+3] = rand() % 10000 + 1;
mas[i+4] = rand() % 10000 + 1;
mas[i+5] = rand() % 10000 + 1;
mas[i+6] = rand() % 10000 + 1;
mas[i+7] = rand() % 10000 + 1;
mas[i+8] = rand() % 10000 + 1;
mas[i+9] = rand() % 10000 + 1;
}
}
void Show() //Вывод массива
{
for (int i = 0; i < n; i++)
{
cout << mas[i] << endl;
cout << mas[i + 1] << endl;
cout << mas[i + 2] << endl;
cout << mas[i + 3] << endl;
cout << mas[i + 4] << endl;
cout << mas[i + 5] << endl;
cout << mas[i + 6] << endl;
cout << mas[i + 7] << endl;
cout << mas[i + 8] << endl;
cout << mas[i + 9] << endl;
}
}
void Sort() //Сортировка пузырьком
{
unsigned int start_time = clock();
for (int j = 0; j < n - 1; j++)
{
for (int i = 0; i < n - j - 1; i++)
{
if (mas[i] > mas[i + 1])
{
int b = mas[i];
mas[i] = mas[i + 1];
mas[i + 1] = b;
}
}
}
unsigned int end_time = clock();
cout << (end_time - start_time) << " time sort buble \n";
}
void SortVibor() // Сортировка выборкой
{
int tmp;
unsigned int start_time = clock();
for (int i = 0; i < n; i++)
{
int pos = i;
tmp = mas[i];
for (int j = i+1; j < n; j++)
{
if (mas[j] < tmp)
{
pos = j;
tmp = mas[j];
}
}
mas[pos] = mas[i];
mas[i] = tmp;
}
unsigned int end_time = clock();
cout << (end_time - start_time) << " time sort viborka\n";
}
void main()
{
srand(time(NULL));
Input();
Sort();
Input();
SortVibor();
system("PAUSE");
}