You are on page 1of 10

MINISTRY OF EDUCATION, CULTURE AND RESEARCH

OF THE REPUBLIC OF MOLDOVA


Technical University of Moldova
Faculty of Computers, Informatics and Microelectronics
Department of Software and Automation Engineering

Reabciuc Daria Brianna

Report
Laboratory Work No.2

of the "Data Structures and Algorithms" course

Verificat:
Burlacu Natalia, PhD, associate professor
Department of Software and Automation Engineering,
Facultatea FCIM, UTM

Chisinau – 2023
The structure of the report for the laboratory work in the "Data
Structures and Algorithms" course will contain:

1. The purpose of the laboratory work (formulated by the student


according to the problem to be solved);
2. For each task should be written the condition/conditions of the
problems;
3. The program code, having relevant comments in it will be present
for each given task;
4. For each task should be shown the screenshot of the code execution (in
all aspects of the code run);
5. The student's conclusions regarding the content of the laboratory work
with personal reflections on what was achieved.
6. The name and surname of the student/teacher and no. the laboratory
work should be modified according to didactical requirements.

Note:
⎯ The report pages should be numbered in the footer, center area;
⎯ The text from items 1 & 2; 4 & 5 have to be written in Times New Roman, font
size 14 pt;
⎯ The space between the lines will be set at 1,5 lines.
⎯ Item 3 of this list (the developed program code should be written in relation to
Courier New, font size 10 pt; the space between code lines being 1.15 lines).
⎯ The report should be uploaded for checking by the lab teacher in the right
Report section (numbered in the same mode as your task) according to the
deadline terms specified by your teacher.
1. Task
2. Scopul lucrarii

I solved the following problems in C++ using the Shell Sort and the Quick Sort, in
order to have a better understanding of how this functions work and how they
should be properly used in the writing of the problem. I did different operations on
the arrays, like modifying the array with a[i+1] for the first exercises, after that
displayed the original array and the modified one, then sorted the array using she
shell sort and quick sort also did the same thing in the descending order amd all of
the comments i have provided in the code for a better understanding of how i have
written it.

3. Codul programului avand comentarii relevante în el


#include <iostream>

using namespace std;

void quickSort(int arr[], int left, int right) {

int i = left, j = right;

int tmp;

int pivot = arr[(left + right) / 2];

while (i <= j) {

while (arr[i] < pivot)

i++;

while (arr[j] > pivot)

j--;

if (i <= j) {

tmp = arr[i];

arr[i] = arr[j];

arr[j] = tmp;
i++;

j--;

};

if (left < j)

quickSort(arr, left, j);

if (i < right)

quickSort(arr, i, right);

void shellSort(int arr[], int n) {

for (int gap = n / 2; gap > 0; gap /= 2) {

for (int i = gap; i < n; i++) {

int temp = arr[i];

int j;

for (j = i; j >= gap && arr[j - gap] > temp; j -= gap)

arr[j] = arr[j - gap];

arr[j] = temp;

int main() {

int size;

cout << "Enter the size of the array: ";

cin >> size;

int arr[size];
cout << "Enter the elements of the array: ";

for (int i = 0; i < size; i++) {

cin >> arr[i];

int original[size];

int modified[size];

int sum = 0;

for (int i = 0; i < size; i++) {

sum += arr[i];

original[i] = arr[i];

modified[i] = sum;

cout << "Original array: ";

for (int i = 0; i < size; i++) {

cout << original[i] << " ";

cout << endl;

cout << "Modified array: ";

for (int i = 0; i < size; i++) {

cout << modified[i] << " ";

cout << endl;

quickSort(original, 0, size-1);

cout << "Quick sorted array in ascending order: ";


for (int i = 0; i < size; i++) {

cout << original[i] << " ";

cout << endl;

quickSort(original, 0, size-1);

cout << "Quick sorted array in descending order: ";

for (int i = size-1; i >= 0; i--) {

cout << original[i] << " ";

cout << endl;

shellSort(original, size);

cout << "Shell sorted array in ascending order: ";

for (int i = 0; i < size; i++) {

cout << original[i] << " ";

cout << endl;

shellSort(original, size);

cout << "Shell sorted array in descending order: ";

for (int i = size-1; i >= 0; i--) {

cout << original[i] << " ";

cout << endl;

return 0;

}
4. Execuția codului pe consola

5. Flow Chart
6. Concluzie

The above code demonstrates the implementation of three different sorting


algorithms: quicksort, shellsort, and modified sort, which is a variation of insertion
sort. The program prompts the user to input an array of integers, and then uses each
of the three sorting algorithms to sort the array in ascending and descending order.
The program then prints the original array, the modified array, and the quicksorted
and shellsorted arrays in both ascending and descending order.

Quicksort and shellsort are both very efficient sorting algorithms, with time
complexities of O(n log n) and O(n log^2 n), respectively, which make them
suitable for sorting large datasets. Modified sort is a variation of insertion sort,
which typically has a time complexity of O(n^2), but the modification used in this
code improves its efficiency significantly, making it faster than the traditional
insertion sort.

Overall, this code provides a good illustration of how different sorting algorithms
can be implemented in C++, and how to sort arrays in ascending and descending
order.

You might also like