
Отчет ПР4
.docxФЕДЕРАЛЬНОЕ ГОСУДАРСТВЕННОЕ БЮДЖЕТНОЕ ОБРАЗОВАТЕЛЬНОЕ УЧРЕЖДЕНИЕ ВЫСШЕГО ОБРАЗОВАНИЯ
«САНКТ-ПЕТЕРБУРГСКИЙ ГОСУДАРСТВЕННЫЙ УНИВЕРСИТЕТ ТЕЛЕКОММУНИКАЦИЙ ИМ. ПРОФ. М.А. БОНЧ-БРУЕВИЧА»
(СПбГУТ)
Кафедра безопасности информационных систем
ОТЧЁТ
по практической работе № 4 на тему: «Сортировка числовых массивов. Некоторые методы сортировки.»
по дисциплине «Алгоритмы и структуры данных»
Выполнил: студент группы ИСТ-114, Медведева С.Г.,
«26» октября 2022 г. ___________/Медведева С.Г.
Принял: к.ф.-м.н., доцент, И.А. Моисеев
«26» октября 2022 г. ___________/ И.А. Моисеев /
1 Основная часть
Цель работы
Изучить сортировку числовых массивов и некоторые методы сортировки.
1.2 Результаты выполнения работы
#include <iostream> #include <fstream> #include <chrono> using namespace std; void readArr(int *arr, ifstream &file){ if (file.is_open()){ int i{}; while (!file.eof()){ file >> arr[i]; ++i; } } else cout << "file is not open"; } void printArr (int *arr, int size){ for (int i {}; i != size; ++i){ cout << arr[i] << "\t"; } } void bubSort(int * arr, int size){ bool flag; int itcom{},itper{}; do{ flag = false; for (int i{}; i !=size-1; ++i){ itcom++; if (arr[i] < arr[i+1]){ itper++; swap(arr[i],arr[i+1]); flag = true; break; } } } while (flag); cout << "Permutations: " << itper << "\t Comparisons: " << itcom << endl; } void selSort (int *arr, int size){ int itCom{},itPer{}, maxIndex; for (int i{}; i < size-1; i++){ maxIndex = i; for (int j{i}; j < size; j++){ itCom++; if (arr[j] > arr[maxIndex]) maxIndex = j; } swap(arr[i],arr[maxIndex]); itPer++; } cout << "Permutations: " << itPer << "\t Comparisons: " << itCom << endl; } void inSort (int *arr, int size){ int itCom{},itPer{}; for(int i{1};i<size;i++){ for(int j=i; j>0 && arr[j-1]>arr[j];j--){ itPer++; itCom++; swap(arr[j],arr[j-1]); } } cout << "Permutations: " << itPer << "\t Comparisons: " << itCom << endl; } int main() { auto begin = chrono::steady_clock::now(); auto end = chrono::steady_clock::now(); ifstream a ("a.txt"); int *arr = new int [30]; readArr(arr,a); printArr(arr, 30); cout << endl << "Bubble sorting:" << endl; begin = chrono::steady_clock::now(); bubSort(arr, 30); end = chrono::steady_clock::now(); cout << "Time: " << (chrono::duration_cast<chrono::milliseconds>(end-begin)).count() << endl; printArr(arr, 30); readArr(arr,a); cout << endl << "Selection sorting: "<< endl; begin = chrono::steady_clock::now(); selSort(arr, 30); end = chrono::steady_clock::now(); cout << "Time: " << (chrono::duration_cast<chrono::milliseconds>(end-begin)).count() << endl; printArr(arr, 30); readArr(arr,a); cout << endl << "Insertion sorting: "<< endl; begin = chrono::steady_clock::now(); inSort(arr, 30); end = chrono::steady_clock::now(); cout << "Time: " << (chrono::duration_cast<chrono::milliseconds>(end-begin)).count() << endl; printArr(arr, 30); return 0; }
Результат выполнения:
Вывод
Изучены сортировка числовых массивов и некоторые методы сортировки.
САНКТ-ПЕТЕРБУРГ 2022