You are on page 1of 6

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

DANIEL MUNTEAN

Report
Laboratory Work No.2B

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:
The purpose of second laboratory work (number 22) is to move cyclically to
the left by K positions a 1D array given by the user as an input. Also to sort
the original array with quick sort and the modified array with shell sort.
Now in the second part of the exercise (variation of the first exercise) we
will use Counting and Merge sort. And also the elements of the array will be
added number N given by the user.

Code:
#include <stdio.h>
#include <stdlib.h>
//we do cyclic shift
void cyclic_shift(int arr[], int n, int k) {
int temp[k];
for (int i = 0; i < k; i++) {
temp[i] = arr[i];
}
for (int i = k; i < n; i++) {
arr[i-k] = arr[i];
}
for (int i = 0; i < k; i++) {
arr[n-k+i] = temp[i];
}
}
//algorithm of counting sort
void counting_sort(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
int count[max+1];
for (int i = 0; i <= max; i++) {
count[i] = 0;
}
for (int i = 0; i < n; i++) {
count[arr[i]]++;
}
int index = 0;
for (int i = 0; i <= max; i++) {
for (int j = 0; j < count[i]; j++) {
arr[index++] = i;
}
}
}
//algorithm for merge sort
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (int i = 0; i < n1; i++) {
L[i] = arr[l + i];
}
for (int j = 0; j < n2; j++) {
R[j] = arr[m + 1 + j];
}
int 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++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}

void merge_sort(int arr[], int l, int r) {


if (l < r) {
int m = l + (r - l) / 2;
merge_sort(arr, l, m);
merge_sort(arr, m+1, r);
merge(arr, l, m, r);
}
}

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


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int n, k;
printf("Enter the size of the array: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements of the array: ");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the value of k: ");
scanf("%d", &k);
printf("Original array: ");
display_array(arr, n);
cyclic_shift(arr, n, k);
printf("Modified array: ");
display_array(arr, n);
printf("counting sort original array ascending: ");
counting_sort(arr, n);
display_array(arr, n);
printf("counting sort original array descending: ");
counting_sort(arr, n);
for (int i = n-1; i >= 0; i--) {
printf("%d ", arr[i]);
}
printf("\n");
printf("merge sort modified array ascending: ");
merge_sort(arr, 0, n-1);
display_array(arr, n);
printf("merge sort modified array descending: ");
merge_sort(arr, 0, n-1);
for (int i = n-1; i >= 0; i--) {
printf("%d ", arr[i]);
}
printf("\n");
int sum_n;
printf("Enter the value of N: ");
scanf("%d", &sum_n);
counting_sort(arr, n);
printf("Sorted array descending order added to N: ");
for (int i = n-1; i >= 0; i--) {
printf("%d ", arr[i]+sum_n);
}
printf("\n");
return 0;
}
Results in console:
In conclusion in this laboratory we learned how to apply the counting sort and
merge sort in a better. As well an important part of programing arrays which is
cyclic shift. This also helps to understand better how to work with modified and
original vector, which are different things. In addition this problem has a variation
that is an integer N added to each element of the sorted array.

You might also like