Report SDA Lab 3
Report SDA Lab 3
Report SDA Lab 3
Report
Laboratory Work No.3
Checked:
Burlacu Natalia, PhD, associate professor
Department of Software and Automation Engineering,
FCIM Faculty, UTM
Chisinau – 2023
Figure 1. 1 - Task
Problem statement:
2
Figure 1. 2 - Problem statement
int main()
{
int n, m, l, h;
3
printf("Enter number of rows: ");
scanf("%d", &n);
printf("Enter number of columns: ");
scanf("%d", &m);
int arr[n][m];
array_filling(n, m, arr);
array_printing(n, m, arr);
printf("\n");
int c = subarrays_searching(n, m, arr, &l, &h);
printf("Total number of subattays = %i \n", c);
printf("Matrix with resorted column is: \n");
printf("\n");
if (c % 2 == 0)
{
heapSort(n, m, l, arr);
}
else
{
countingSort(n, h, m, arr);
}
array_printing(n, m, arr);
}
4
{
int d = 0;
int z = 0;
int init = 0;
for (int i = 0; i < n; i++)
{
if (arr[i][j] == 0 || i == (n-1))
{
if (d != 0)
{
int amm = i - init;
int subarr[amm];
for (int k = 0; k <= amm; k++)
{
subarr[k] = arr[init + k][j];
}
printf("C%i subarrs:\n", j);
printf("a[%i, %i] to a [%i, %i] --> {", j, init, j, i);
for (int k = 0; k < amm; k++)
{
printf("%i, ", subarr[k]);
}
printf("%i}\n", subarr[amm]);
subarrs++;
if (amm < tamm)
{
*l = j;
}
if (amm > hamm)
{
*h = j;
}
}
init = i+1;
d = 0;
}
if (arr[i][j] % 2 == 0)
{
d++;
}
}
}
return subarrs;
}
5
// which is an index in arr[].
// n is size of heap
void heapify(int N, int j, int m, int i, int arr[N][j])
{
// Find largest among root,
// left child and right child
// left = 2*i + 1
int left = 2 * i + 1;
// right = 2*i + 2
int right = 2 * i + 2;
largest = left;
largest = right;
swap(&arr[i][m], &arr[largest][m]);
heapify(N, j, m, i, arr);
// Heap sort
for (int i = N - 1; i >= 0; i--) {
swap(&arr[0][m], &arr[i][m]);
6
heapify(i, j, m, 0, arr);
}
}
// Find the index of each element of the original array in count array, and
// place the elements in output array
for (int i = n - 1; i >= 0; i--) {
output[count[array[i][m]] - 1] = array[i][m];
count[array[i][m]]--;
}
7
void array_printing(int n, int m, int **arr);
int subarrays_searching(int n, int m, int **arr, int *l, int *h);
void swap(int* a, int* b);
void heapSort(int N, int j, int m, int **arr);
void heapify(int N, int j, int m, int i, int **arr);
void countingSort(int n, int m, int j, int **array);
int main()
{
int n, m, l = 0, h = 0;
printf("Enter number of rows: ");
scanf("%d", &n);
printf("Enter number of columns: ");
scanf("%d", &m);
array_filling(n, m, arr);
array_printing(n, m, arr);
printf("\n");
int c = subarrays_searching(n, m, arr, &l, &h);
printf("Total number of subarrays = %i \n", c);
printf("Matrix with resorted column is: \n");
printf("\n");
if (c % 2 == 0)
{
heapSort(n, l, m, arr);
}
else
{
countingSort(n, m, h, arr);
}
array_printing(n, m, arr);
8
int *inner_ptr = *ptr;
for (int j = 0; j < m; j++, inner_ptr++)
{
printf("Enter the element: ");
scanf("%d", inner_ptr);
}
}
}
9
{
*l = j;
}
if (amm > hamm)
{
*h = j;
}
free(subarr);
}
init = i + 1;
d = 0;
}
if (*(*(arr + i) + j) % 2 == 0)
{
d++;
}
}
}
return subarrs;
}
if (largest != i) {
swap(&*(*(arr + i) + m), &*(*(arr + largest) + m));
heapify(N, j, m, largest, arr);
}
}
void heapSort(int N, int j, int m, int **arr)
{
for (int i = N / 2 - 1; i >= 0; i--)
heapify(N, j, m, i, arr);
10
void countingSort(int n, int m, int j, int **array) {
int max = *(*(array + 0) + j);
int **ptr = array;
for (int i = 1; i < n; i++, ptr++)
{
if (*(*ptr + j) > max)
max = *(*ptr + j);
}
ptr = array;
for (int i = 0; i < n; i++, ptr++)
{
count[*(*ptr + j)]++;
}
ptr = array;
for (int i = n - 1; i >= 0; i--, ptr++)
-----------------------------------Modified version---------------------------------------
#include <stdio.h>
void findSubarrays(int m, int n, int arr[m][n], int j, int i, int init, int
d, int tamm, int hamm, int *l, int *h, int *subarrs);
11
int main()
int n, m, l, h;
scanf("%d", &n);
scanf("%d", &m);
int arr[n][m];
array_filling(n, m, arr);
array_printing(n, m, arr);
printf("\n");
printf("\n");
if (c % 2 == 0)
heapSort(n, m, l, arr);
else
countingSort(n, h, m, arr);
array_printing(n, m, arr);
12
void array_filling(int n, int m, int arr[n][m])
scanf("%d", &arr[i][j]);
printf("\n");
void findSubarrays(int m, int n, int arr[m][n], int j, int i, int init, int
d, int tamm, int hamm, int *l, int *h, int *subarrs) {
if (i == n) {
if (d != 0) {
13
int amm = i - init;
int subarr[amm];
(*subarrs)++;
*l = j;
*h = j;
return;
if (arr[i][j] == 0 || i == (n - 1)) {
if (d != 0) {
int subarr[amm];
14
printf("C%i subarrs:\n", j);
(*subarrs)++;
*l = j;
*h = j;
init = i + 1;
d = 0;
if (arr[i][j] % 2 == 0) {
d++;
int subarrs = 0;
int tamm = n;
int hamm = 0;
15
for (int j = 0; j < m; j++) {
return subarrs;
*a = *b;
*b = temp;
// n is size of heap
int largest = i;
// left = 2*i + 1
int left = 2 * i + 1;
16
// right = 2*i + 2
int right = 2 * i + 2;
largest = left;
// so far
largest = right;
if (largest != i) {
swap(&arr[i][m], &arr[largest][m]);
// sub-tree
17
{
heapify(N, j, m, i, arr);
// Heap sort
swap(&arr[0][m], &arr[i][m]);
// root again
heapify(i, j, m, 0, arr);
int output[100];
max = array[i][j];
18
// we cannot declare it as int count(max+1) in C as
int count[100];
count[i] = 0;
count[array[i][m]]++;
// Find the index of each element of the original array in count array, and
output[count[array[i][m]] - 1] = array[i][m];
count[array[i][m]]--;
19
array[i][m] = output[i];
Conclusion:
During the lab work, I applied the knowledge acquired from various courses
and seminars. This involved using different sorting techniques for organizing
subarrays within a 2-D matrix. The process started with basic methods like Heap
Sort, used when the total number of found subarrays was an even number. It then
20
progressed to more complex methods like Counting Sort, used when the total
number of found subarrays was an odd number.
21