0% found this document useful (0 votes)
34 views28 pages

Report SDA Lab 2

The document describes a task to solve problems in C using functions with parameters passed by value and by address. It includes function declarations, main method, and functions to scan and print arrays, rearrange elements, find length, quicksort and shellsort arrays.

Uploaded by

max.kostov04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views28 pages

Report SDA Lab 2

The document describes a task to solve problems in C using functions with parameters passed by value and by address. It includes function declarations, main method, and functions to scan and print arrays, rearrange elements, find length, quicksort and shellsort arrays.

Uploaded by

max.kostov04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

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

MAXIM COSTOV STUDENT

Report
Laboratory Work No.2

of the "Data Structures and Algorithms" course

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.

B. with the use of the method of passing parameters of functions by


address/pointers (the formal parameter will be a pointer to the value of the
corresponding object).

C. To draw the block diagram corresponding to the solved problem.

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

1. The program code, having relevant comments in it and the Block


diagram;

--------The version with the transmission of function parameters by value-------


#include <stdio.h>

// 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

length = get_l(arr, n); // Getting the length of the rearranged array


int arr2[length]; // Declaring an array of size length

printf("\n");
reb_arr(arr, arr2, n, length); // Rearranging the array elements
parr(arr2, length); // Printing the rearranged array elements

quicksort(arr, 0, n-1, 1); // Sorting the original array in ascending


order using quicksort
parr(arr, n); // Printing the sorted array

quicksort(arr, 0, n-1, 0); // Sorting the original array in descending


order using quicksort
parr(arr, n); // Printing the sorted array

printf("\n");
shellsort(arr2, length, 1); // Sorting the rearranged array in ascending
order using shellsort
parr(arr2, length); // Printing the sorted array

shellsort(arr2, length, 0); // Sorting the rearranged array in descending


order using shellsort
parr(arr2, length); // Printing the sorted array
}

// Function to scan array elements from user input


void sc_arr(int arr[], int n)
{
int c; // Variable to store current element
for (int i = 0; i < n; i++)
{
printf("Enter your number: ");
scanf("%i", &c); // Getting the number from the keyboard
arr[i] = c; // Adding this number to array
}
}

// Function to print array elements


void parr(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
printf("%i ", arr[i]); // Printing each element of the array
}
printf("\n"); // Printing newline after printing all elements
}

// Function to get the length of the rearranged array


int get_l(int arr[], int n)
{
int c = 0; // Counter for increasing length

4
int l = n-1; // Length of the array

for (int i = 0; i < l; i++)


{
if (arr[i] < arr[i+1])
{
c++; // Incrementing counter if the next element is greater than
the current one
}
}
return c+n; // Returning the length of the rearranged array
}

// Function to rearrange array elements


void reb_arr(int arr[], int arr2[], int n, int length)
{
int k = 0; // Index for the rearranged array
int l = n-1; // Length of the original array
for (int j = 0; j < l; j++)
{
arr2[k] = arr[j]; // Copying elements from the original array to the
rearranged array
if (arr[j] < arr[j+1])
{
arr2[k+1] = 0; // Adding a flag value if the next element is
greater than the current one
k++; // Moving to the next index in the rearranged array
}
k++; // Moving to the next index in the rearranged array
}
arr2[k] = arr[l]; // Copying the last element of the original array to
the rearranged array
}

// Function to swap two elements


void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}

// Function to partition the array for quicksort


int partition(int arr[], int low, int high, int order)
{
int pivotal = arr[high]; // Choosing the last element as the pivot
int i = low - 1; // Index of smaller element

// Partitioning the array based on the chosen pivot


if (order == 0) // For descending order
{
for (int j = low; j < high; j++)
{
if (arr[j] < pivotal)
{

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
}

// Function to perform quicksort


void quicksort(int arr[], int low, int high, int order)
{
if (low<high)
{
int pi = partition(arr, low, high, order); // Partitioning index

// Recursively sort elements before and after partition


quicksort(arr, low, pi-1, order);
quicksort(arr, pi+1, high, order);
}
}

// Function to perform shellsort


void shellsort(int arr[], int n, int order)
{
if (order == 1) // For ascending 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];

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
}
}

}
}

--------------Version with passing function parameters via pointers---------------


#include <stdio.h>
#include <stdlib.h>

// 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

int *arr = (int *)malloc(n * sizeof(int)); // Dynamically allocating


memory for arr
sc_arr(arr, n); // Calling function to scan array elements

7
parr(arr, n); // Calling function to print array elements

length = get_l(arr, n); // Getting the length of the rearranged array


int *arr2 = (int *)malloc(length * sizeof(int)); // Dynamically
allocating memory for arr2

printf("\n");
reb_arr(arr, arr2, n, length); // Rearranging the array elements
parr(arr2, length); // Printing the rearranged array elements

quicksort(arr, 0, n - 1, 1); // Sorting the original array in


ascending order using quicksort
parr(arr, n); // Printing the sorted array

quicksort(arr, 0, n - 1, 0); // Sorting the original array in


descending order using quicksort
parr(arr, n); // Printing the sorted array

printf("\n");
shellsort(arr2, length, 1); // Sorting the rearranged array in
ascending order using shellsort
parr(arr2, length); // Printing the sorted array

shellsort(arr2, length, 0); // Sorting the rearranged array in


descending order using shellsort
parr(arr2, length); // Printing the sorted array

// Free dynamically allocated memory


free(arr);
free(arr2);
}

// Function to scan array elements from user input


void sc_arr(int *arr, int n)
{
for (int *p = arr; p < arr + n; p++)
{
printf("Enter your number: ");
scanf("%i", p); // Getting the number from the keyboard
}
}

// Function to print array elements


void parr(int *arr, int n)
{
for (int *p = arr; p < arr + n; p++)
{
printf("%i ", *p); // Printing each element of the array
}
printf("\n"); // Printing newline after printing all elements
}

// Function to get the length of the rearranged array


int get_l(int *arr, int n)
{

8
int c = 0; // Counter for increasing length
int *end = arr + n - 1; // Pointer to the last element of the array

for (int *p = arr; p < end; p++)


{
if (*p < *(p + 1))
{
c++; // Incrementing counter if the next element is greater
than the current one
}
}
return c + n; // Returning the length of the rearranged array
}

// Function to rearrange array elements


void reb_arr(int *arr, int *arr2, int n, int length)
{
int k = 0; // Index for the rearranged array
int *end = arr + n - 1; // Pointer to the last element of the
original array

for (int *p = arr; p < end; p++)


{
*arr2++ = *p; // Copying elements from the original array to the
rearranged array
if (*p < *(p + 1))
{
*arr2++ = 0; // Adding a flag value if the next element is
greater than the current one
k++; // Moving to the next index in the rearranged array
}
k++; // Moving to the next index in the rearranged array
}
*arr2 = *end; // Copying the last element of the original array to
the rearranged array
}

// Function to swap two elements


void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}

// Function to partition the array for quicksort


int partition(int *arr, int low, int high, int order)
{
int pivotal = *(arr+high); // Choosing the last element as the pivot
int i = low - 1; // Index of smaller element

// Partitioning the array based on the chosen pivot


if (order == 0) // For descending order
{
for (int j = low; j < high; j++)

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
}

// Function to perform quicksort


void quicksort(int *arr, int low, int high, int order)
{
if (low < high)
{
int pi = partition(arr, low, high, order); // Partitioning index

// Recursively sort elements before and after partition


quicksort(arr, low, pi - 1, order);
quicksort(arr, pi + 1, high, order);
}
}

// Function to perform shellsort


void shellsort(int *arr, int n, int order)
{
if (order == 1) // For ascending 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;

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

void sc_arr(int *arr, int n);

void parr(int *arr, int n);

11
void swap(int *a, int *b);

void merge(int *arr, int l, int m, int r);

void merge_sort(int *arr, int l, int r);

void countingSort(int arr[], int n);

void randprint(int *arr, int n, int *arr3);

int main() {

int n; // Size of the array

printf("Enter the size of the array: ");

scanf("%i", &n); // Reading the size of the array from the user

if (n <= 0) {

printf("Invalid array size.\n");

return 1; // Exiting with an error code

int *arr = malloc(n * sizeof(int));

int *arr2 = malloc(n * sizeof(int));

if (arr == NULL || arr2 == NULL) {

printf("Memory allocation failed.\n");

return 1; // Exiting with an error code

sc_arr(arr, n);

for (int i = 0; i < n; i++)

*(arr2+i) = *(arr+i);

int sum = 0;

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

sum += *(arr+i);

12
if (sum % 2 == 0) {

merge_sort(arr, 0, n - 1);

} else {

countingSort(arr, n);

randprint(arr, n, arr2);

free(arr); // Freeing allocated memory

return 0; // Exiting with success

// Function to scan array elements from user input

void sc_arr(int *arr, int n)

for (int *p = arr; p < arr + n; p++)

printf("Enter your number: ");

scanf("%i", p); // Getting the number from the keyboard

// Function to print array elements

void parr(int *arr, int n)

for (int *p = arr; p < arr + n; p++)

printf("%i ", *p); // Printing each element of the array

printf("\n"); // Printing newline after printing all elements

// Function to swap two elements

void swap(int *a, int *b)

13
{

int t = *a;

*a = *b;

*b = t;

void merge(int *arr, int l, int m, int r)

int i, j, k;

int n1 = m - l + 1;

int n2 = r - m;

// Creating of 2 temporary arrays (Left and Right)

int *L = malloc(n1 * sizeof(int));

int *R = malloc(n2 * sizeof(int));

// Copying the data in the temporary arrays

for (i = 0; i < n1; i++)

*(L+i) = *(arr+l+i);

for (j = 0; j < n2; j++)

*(R+j) = *(arr+j+m+1);

// Merging arrays back in arr[l .. r]

i = 0; // Variable to store the indexes for L arr

j = 0; // Variable to store the indexes for R arr

k = l; //Variable to store the indexes for original array arr[l .. r]

while (i < n1 && j < n2)

14
if (*(L+i) >= *(R+j))

*(arr+k) = *(L+i);

i++;

else

*(arr+k) = *(R+j);

j++;

k++;

// Copying the elements that left from the left side

while (i < n1)

*(arr+k) = *(L+i);

i++;

k++;

// Copying the elements that left from the right side

while (j < n2)

*(arr+k) = *(R+j);

j++;

k++;

void merge_sort(int *arr, int l, int r)

// If first ID is less then the last

if (l < r)

15
int m = (r + l)/2; // Finding the middle element

merge_sort(arr, l, m); // Sorting recursively the left side

merge_sort(arr, m+1, r); // Sorting recursively the right side

merge(arr, l, m, r); // Merging the left and the right arrays

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

// Find the maximum element in the input array

int max = *(arr+0);

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

if (*(arr+i) > max) {

max = *(arr+i);

// Create a count array to store the frequency of each element

int *count_arr = malloc((max + 1) * sizeof(int));

if (count_arr == NULL) {

printf("Memory allocation failed.\n");

return;

// Initialize count array elements to 0

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

*(count_arr+i) = 0;

// Count the occurrences of each element

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

(*(count_arr+(*(arr+i))))++;

16
// Modify count array to store actual position of each element

for (int i = 1; i <= max; i++) {

*(count_arr+i) += *(count_arr+(i - 1));

// Create a temporary array to store sorted elements

int *arr1 = malloc(n * sizeof(int));

if (arr1 == NULL) {

printf("Memory allocation failed.\n");

free(count_arr); // Freeing previously allocated memory

return;

// Place elements in sorted order

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

*(arr1+((*(count_arr+(*(arr+i)))) - 1)) = *(arr+i);

(*(count_arr+(*(arr+i))))--;

// Copy sorted elements back to the original array

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

*(arr+i) = *(arr1+i);

// Free allocated memory

free(count_arr);

free(arr1);

// Function to print array elements with a delay for visualization

void randprint(int *arr, int n, int *arr3)

srand(time(NULL));

int *arr2 = malloc(n * sizeof(int));

17
for (int i = 0; i < 15 * n; i++)

system("cls");

printf("The original array: \n");

for (int k = 0; k < n; k++)

printf("%d ", *(arr3+k));

printf("\n");

printf("The sorted one \n");

for (int j = 0; j < n; j++)

if (j > (float)i / 15)

int x;

do

x = rand() % 10;

} while (x == *(arr2+j));

*(arr2+j) = x;

else

*(arr2+j) = *(arr+j);

for (int k = 0; k < n; k++)

printf("%d ", *(arr2+k));

printf("\n");

18
usleep(100000);

free(arr2);

Block diagram for the first version:

19
20
Figure 2.1 – Function “main()”

Figure 2.2 – Function “get_l”

21
Figure 2.3 – Function “reb_arr”

22
Figure 2.4 – Function “selsort”

Figure 2.5 – Function “swap”

23
Figure 2.6 – Function “partition

24
Figure 2.7 – Function “parr”

Figure 2.8 – Function “sc_arr”

25
Figure 2.9 – Function “quicksort”

26
Figure 2.10 – Function “shellsort”

Output first version:

Figure 3.1 – Output for the first version

Output modified version:

Figure 3.2 – Output for the modified version

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

You might also like