Report SDA Lab 2
Report SDA Lab 2
Report
Laboratory Work No.2
Checked:
Burlacu Natalia, PhD, associate professor
Department of Software and Automation Engineering,
FCIM Faculty, UTM
Chisinau – 2023
Task:
1. Solve the following problems in C, writing your own functions according to the
given statements. Write the solution of the problem by procedural approach in two
versions:
A. with the use of the method of transmitting the parametric functions by value.
2. Modify the content of your problems emerging from the possibilities that are
missing, but which can be brought as added value in the condition of the existing
problem. Formulate and present in writing the modified condition; to solve in C
your problem in the modified version, using functions developed by you.
-sBecause of the fact that in every problem in version 1, you should use two
specified sorting methods, in version 2, of the problem proposed (modified) by
you, you should use the sorting methods as Counting Merge & Merge Sort.
Problem statement:
2
Figure 1. 1 - Problem statement
// Function declarations
void sc_arr(int arr[], int n); // Function to scan array elements from user
input
void parr(int arr[], int n); // Function to print array elements
void reb_arr(int arr[], int arr2[], int n, int length); // Function to
rearrange array elements
int get_l(int arr[], int n); // Function to get the length of the rearranged
array
void quicksort(int arr[], int low, int high, int order); // Function to
perform quicksort
int partition(int arr[], int low, int high, int order); // Function to
partition the array for quicksort
void swap(int *a, int *b); // Function to swap two elements
void shellsort(int arr[], int n, int order); // Function to perform shellsort
int main()
{
int n; // Size of the array
int length; // Length of the rearranged array
printf("Enter the size of the array: ");
scanf("%i", &n); // Reading the size of the array from the user
3
int arr[n]; // Declaring an array of size n
sc_arr(arr, n); // Calling function to scan array elements
parr(arr, n); // Calling function to print array elements
printf("\n");
reb_arr(arr, arr2, n, length); // Rearranging the array elements
parr(arr2, length); // Printing the rearranged array elements
printf("\n");
shellsort(arr2, length, 1); // Sorting the rearranged array in ascending
order using shellsort
parr(arr2, length); // Printing the sorted array
4
int l = n-1; // Length of the array
5
i++;
swap(&arr[i], &arr[j]); // Swapping if the current element is
less than the pivot
}
}
swap(&arr[i+1], &arr[high]); // Placing the pivot element at its
correct position
}
else // For ascending order
{
for (int j = low; j < high; j++)
{
if (arr[j] > pivotal)
{
i++;
swap(&arr[i], &arr[j]); // Swapping if the current element is
greater than the pivot
}
}
swap(&arr[i+1], &arr[high]); // Placing the pivot element at its
correct position
}
return (i+1); // Returning the partitioning index
}
int j;
// Shifting elements to make space for the current element
for (j = i; j >= gap && arr[j-gap] > temp; j -= gap)
{
arr[j] = arr[j-gap];
6
}
arr[j] = temp; // Placing the current element at its correct
position
}
}
}
else // For descending order
{
for (int gap = n/2; gap > 0; gap /= 2)
{
for (int i = gap; i < n; i++)
{
int temp = arr[i]; // Current element to be inserted at its
correct position
int j;
// Shifting elements to make space for the current element
for (j = i; j >= gap && arr[j-gap] < temp; j -= gap)
{
arr[j] = arr[j-gap];
}
arr[j] = temp; // Placing the current element at its correct
position
}
}
}
}
// Function declarations
void sc_arr(int *arr, int n);
void parr(int *arr, int n);
void reb_arr(int *arr, int *arr2, int n, int length);
int get_l(int *arr, int n);
void quicksort(int *arr, int low, int high, int order);
int partition(int *arr, int low, int high, int order);
void swap(int *a, int *b);
void shellsort(int *arr, int n, int order);
int main()
{
int n; // Size of the array
int length; // Length of the rearranged array
printf("Enter the size of the array: ");
scanf("%i", &n); // Reading the size of the array from the user
7
parr(arr, n); // Calling function to print array elements
printf("\n");
reb_arr(arr, arr2, n, length); // Rearranging the array elements
parr(arr2, length); // Printing the rearranged array elements
printf("\n");
shellsort(arr2, length, 1); // Sorting the rearranged array in
ascending order using shellsort
parr(arr2, length); // Printing the sorted array
8
int c = 0; // Counter for increasing length
int *end = arr + n - 1; // Pointer to the last element of the array
9
{
if (*(arr+j) < pivotal)
{
i++;
swap((arr+i), *(arr+j)); // Swapping if the current
element is less than the pivot
}
}
swap((arr+(i + 1)), (arr+high)); // Placing the pivot element at
its correct position
}
else // For ascending order
{
for (int j = low; j < high; j++)
{
if (*(arr+j) > pivotal)
{
i++;
swap(*(arr+i), *(arr+j)); // Swapping if the current
element is greater than the pivot
}
}
swap((arr+(i + 1)), (arr+high)); // Placing the pivot element at
its correct position
}
return (i + 1); // Returning the partitioning index
}
int j;
10
// Shifting elements to make space for the current
element
for (j = i; j >= gap && *(arr+(j - gap)) > temp; j -=
gap)
{
*(arr+j) = *(arr+(j - gap));
}
*(arr+j) = temp; // Placing the current element at its
correct position
}
}
}
else // For descending order
{
for (int gap = n / 2; gap > 0; gap /= 2)
{
for (int i = gap; i < n; i++)
{
int temp = *(arr+i); // Current element to be inserted at
its correct position
int j;
// Shifting elements to make space for the current
element
for (j = i; j >= gap && *(arr+(j - gap)) < temp; j -=
gap)
{
*(arr+j) = *(arr+(j - gap));
}
*(arr+j) = temp; // Placing the current element at its
correct position
}
}
}
}
-----------------------------------Modified version---------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
// Function declarations
11
void swap(int *a, int *b);
int main() {
scanf("%i", &n); // Reading the size of the array from the user
if (n <= 0) {
sc_arr(arr, n);
*(arr2+i) = *(arr+i);
int sum = 0;
sum += *(arr+i);
12
if (sum % 2 == 0) {
merge_sort(arr, 0, n - 1);
} else {
countingSort(arr, n);
randprint(arr, n, arr2);
13
{
int t = *a;
*a = *b;
*b = t;
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
*(L+i) = *(arr+l+i);
*(R+j) = *(arr+j+m+1);
14
if (*(L+i) >= *(R+j))
*(arr+k) = *(L+i);
i++;
else
*(arr+k) = *(R+j);
j++;
k++;
*(arr+k) = *(L+i);
i++;
k++;
*(arr+k) = *(R+j);
j++;
k++;
if (l < r)
15
int m = (r + l)/2; // Finding the middle element
max = *(arr+i);
if (count_arr == NULL) {
return;
*(count_arr+i) = 0;
(*(count_arr+(*(arr+i))))++;
16
// Modify count array to store actual position of each element
if (arr1 == NULL) {
return;
(*(count_arr+(*(arr+i))))--;
*(arr+i) = *(arr1+i);
free(count_arr);
free(arr1);
srand(time(NULL));
17
for (int i = 0; i < 15 * n; i++)
system("cls");
printf("\n");
int x;
do
x = rand() % 10;
} while (x == *(arr2+j));
*(arr2+j) = x;
else
*(arr2+j) = *(arr+j);
printf("\n");
18
usleep(100000);
free(arr2);
19
20
Figure 2.1 – Function “main()”
21
Figure 2.3 – Function “reb_arr”
22
Figure 2.4 – Function “selsort”
23
Figure 2.6 – Function “partition
24
Figure 2.7 – Function “parr”
25
Figure 2.9 – Function “quicksort”
26
Figure 2.10 – Function “shellsort”
27
Conclusion:
During the lab work, I utilized the knowledge gleaned from courses and
seminars. This involved employing various sorting techniques for array
organization, commencing with elementary methods like Shell sort and Quick sort,
and advancing to more intricate approaches such as Merge Sort and Counting sort.
Furthermore, I formulated an algorithm for printing the array in a beautiful format.
28