Raport 2
Raport 2
Raport 2
GEORGE RĂBUȘ
Report
Laboratory Work No.2
Verificat:
Burlacu Natalia, PhD, associate professor
Department of Software and Automation Engineering,
Facultatea FCIM, UTM
Chisinau – 2023
21
The structure of the report for the laboratory work in the "Data
Structures and Algorithms" course will contain:
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.
21
1)
The purpose of this laboratory was to familiarize myself and to be able to work
with new sorting alogirthms like shell sort, quicksort and counting merge.
2)
21
3)
Program Task1:
#include <stdio.h>
#include <math.h>
21
for (int i = index_min + 1; i < trunc(index_max-index_min)/2 + index_min;
i++) {
int j = index_max - (i - index_min);
swap(&(*(a+i)), &(*(a+j)));
}
}
21
return (i + 1);
}
21
// recursive call on the left of pivot
task3(array, low, pi - 1);
21
// Rearrange elements at each n/2, n/4, n/8, ... intervals
for (int interval = n / 2; interval > 0; interval /= 2) {
for (int i = interval; i < n; i += 1) {
int temp = *(array + i);
int j;
for (j = i; j >= interval && *(array+j - interval) < temp; j -=
interval) {
*(array + j) = *(array+j - interval);
}
*(array + j) = temp;
}
}
}
int main() {
int n;
scanf("%d", &n);
int a_original[50];
int a_modified[50];
for (int i = 0; i < n; i++)
scanf("%d", &a_original[i]);
for (int i = 0; i < n; i++)
{a_modified[i] = a_original[i];}
printf("\nTasks:\n\n");
task1(a_modified,n);
printf("Display the original and modified arrays:\n");
task2(a_original,a_modified,n);
printf("Display the original array after quick-sorting:\n");
task3(a_original, 0, n - 1);
print_array(a_original, n);
task3_2(a_original, 0, n - 1);
print_array(a_original, n);
printf("Display the modified array after shell-sorting:\n");
task4(a_modified,n);
print_array(a_modified, n);
task4_2(a_modified,n);
print_array(a_modified, n);
return 0;
//-18 10 -3 7 28 55 -4
}
21
Program Task2:
#include <stdio.h>
#include <stdlib.h>
21
for (int i = index_min + 1; i < (index_max - index_min) / 2
+ index_min; i++) {
int j = index_max - (i - index_min);
swap(&(*(a + i)), &(*(a + j)));
}
}
21
// Merge the temp arrays back into arr[l..r]
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (*(L + i) <= *(R + j)) {
*(arr + k) = *(L + i);
i++;
} else {
*(arr + k) = *(R + j);
j++;
}
k++;
}
21
void mergeSort(int *arr, int l, int r) {
if (l < r) {
int m = l + (r - l) / 2;
merge(arr, l, m, r);
}
}
21
// Merge the temp arrays back into arr[l..r]
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2) {
if (*(L + i) >= *(R + j)) {
*(arr + k) = *(L + i);
i++;
} else {
*(arr + k) = *(R + j);
j++;
}
k++;
}
21
}
merge2(arr, l, m, r);
}
}
21
}
21
max = *(array + i);
}
21
*(array + i) = *(output + size - i - 1);
}
}
int main() {
int n;
scanf("%d", &n);
int a_original[50];
int a_modified[50];
21
int a[50];
for (int i = 0; i < n; i++)
scanf("%d", &a_original[i]);
for (int i = 0; i < n; i++)
a_modified[i] = a_original[i];
for (int i = 0; i < n; i++)
a[i] = a_original[i];
printf("\nTasks:\n\n");
task1(a_modified, n);
printf("Display the original and modified arrays:\n");
task2(a_original, a_modified, n);
printf("Display the original array after merge-sorting:\n");
mergeSort(a_original, 0, n - 1);
print_array(a_original, n);
mergeSort2(a_original, 0, n - 1);
print_array(a_original, n);
printf("Display the modified array after count-sorting:\n");
countingSort(a_modified, n);
print_array(a_modified, n);
countingSort2(a_modified, n);
print_array(a_modified, n);
printf("\n");
printf("Additional task: Find in an array how many triangles
can be formed.\n\n");
int triangleCount = countTriangles(a, n);
printf("Number of triangles that can be formed: %d\n",
triangleCount);
return 0;
}
21
FLOWCHARTS:
21
4)
Screenshots:
5)
Conclusion:
21
In conclusion, the laboratory work focused on implementing and understanding
various sorting algorithms, including shell sort, merge sort, quick sort counting sort
as well as developing our creativity on problem solving.
21