0% found this document useful (0 votes)
63 views17 pages

LAB3

The document is a lab report for a data structures and algorithms course. It includes: 1) Solving problems using functions that pass parameters by value and address. 2) Rewriting the solutions recursively using sorting methods like combo sort and radix sort. 3) Code for functions that implement sorting algorithms like counting sort, heap sort, combo sort and radix sort to solve the problems. 4) A conclusion that discusses the learning experience of working with matrices and sorting algorithms.

Uploaded by

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

LAB3

The document is a lab report for a data structures and algorithms course. It includes: 1) Solving problems using functions that pass parameters by value and address. 2) Rewriting the solutions recursively using sorting methods like combo sort and radix sort. 3) Code for functions that implement sorting algorithms like counting sort, heap sort, combo sort and radix sort to solve the problems. 4) A conclusion that discusses the learning experience of working with matrices and sorting algorithms.

Uploaded by

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

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

GRIGORAȘ DUMITRU

Report
Laboratory Work No.3

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 thegiven sta
tements. Write the solution of the problem by procedural approach in twoversions:
a.  with the use of the method of transmitting the parametric functions byvalue;
b.  with the use of the method of passing parameters of functions byaddress/pointer
s (the formal parameter will be a pointer to the value of thecorresponding object)
.
c. To draw the block diagram corresponding to the solved problem.

2. For the second version of the presented solution, you should re-write subtask 1of your pr
oblem using the recursive approach.
The second solution should be integrated with the other functions required underyour pr
oblem statement (with pointers).

— Due to the fact that in each problem in version 1, you should use two specifiedsorting
 methods, in version 2, of the (modified) problem, you should use the sortingmethods as 
Combo Sort & Radix Sort.

Problem 15:

2
#include<iostream>
using namespace std;
int n;
int m;
int length;
int indexI;
int indexJstart;
int indexJend;
//Function for condition 1
void cond1(int *arr, int target_sum, int *shortest, int c) {
int arr2[n];
for(int i =0; i < m; i ++){
int l = 0;
for(int j = i; j < m; j++){
arr2[l] = arr[j];
l++;
}
int index = m;
while(index >1){
int sum = 0;
for(int k = 0; k < index; k++){
sum += arr2[k];
}
if(sum == target_sum){
cout<<"Subarray: ";
for(int k = 0; k <index; k++){
cout<<arr2[k]<<" ";
if(index <= length){
shortest[k] = arr2[k];
length = index;
}
}
if(index <= length){
indexJstart = i;
indexJend = length;
indexI = c;
}
cout<<endl;
}
index--;
}

3
}
}
//Counting sort
void countingSort(int *arr, int n) {
int maxElement = arr[0];
int minElement = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > maxElement) {
maxElement = arr[i];
}
if (arr[i] < minElement) {
minElement = arr[i];
}
}
int count[maxElement - minElement + 1] = {0};
for (int i = 0; i < n; i++) {
count[arr[i] - minElement]++;
}
for (int i = 1; i <= maxElement - minElement; i++) {
count[i] += count[i - 1];
}
int output[n];
for (int i = n - 1; i >= 0; i--) {
output[count[arr[i] - minElement] - 1] = arr[i];
count[arr[i] - minElement]--;
}
for (int i = 0; i < n; i++) {
arr[i] = output[i];
}
}
//Swap elements
void swap(int *arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
void minHeapify(int *arr, int n, int i) {
int smallest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && arr[left] < arr[smallest]) {


smallest = left;
}
4
if (right < n && arr[right] < arr[smallest]) {
smallest = right;
}
if (smallest != i) {
swap(arr, i, smallest);
minHeapify(arr, n, smallest);
}
}
//heap Sort descending
void heapSortDescending(int *arr, int n) {
for (int i = n / 2 - 1; i >= 0; i--) {
minHeapify(arr, n, i);
}
for (int i = n - 1; i >= 0; i--) {
swap(arr, 0, i);
minHeapify(arr, i, 0);
}
}
//combo sort
void comboSort(int *arr, int n) {
int gap = n;
bool swapped = true;

while (gap > 1 || swapped) {


gap = std::max(1, static_cast<int>(gap / 1.3)); // reduce gap
size by 1.3
swapped = false;

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


if (arr[i] < arr[i + gap]) { // descending order
std::swap(arr[i], arr[i + gap]);
swapped = true;
}
}
}
}
//radix sort
void radixSortDescending(int *arr, int n) {
int maxNum = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > maxNum) {
maxNum = arr[i];
}
}
5
for (int exp = 1; maxNum / exp > 0; exp *= 10) {
int output[n];
int count[10] = {0};

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


int digit = (maxNum / exp) % 10;
count[9 - digit]++;
}

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


count[i] += count[i - 1];
}

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


int digit = (maxNum / exp) % 10;
output[count[9 - digit] - 1] = arr[i];
count[9 - digit]--;
}

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


arr[i] = output[i];
}
}
}

int main(){
cout<<"Enter nr of rows: ";
cin>>n;
cout<<"Enter nr of columns: ";
cin>>m;
length = m+1;
int **matrix = new int*[n];
int *shortest = new int[n];
for(int i = 0; i < n; i++){
matrix[i] = new int[m];
}
cout << "Enter the elements of the matrix:" << endl;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> matrix[i][j];
}
}
int S;
6
cout<<"Enter sum: ";
cin>>S;
int var;
//Menu
while(var!= 3){
cout<<endl<<endl;

cout<<"-------------------------------------------Menu---------------
-------------------------- \n";
cout<<"1. If nr of elements forming the subarrays found is an
even: \n";
cout<<" TRUE) Counting Sort ASC\n";
cout<<" FALSE) Heap Sort DES\n";
cout<<"\n2. If nr of elements forming the subarrays found is
an even: \n ";
cout<<" TRUE) Combo Sort ASC\n";
cout<<" FALSE) Radix Sort DES\n";
cout<<"3. Exit";
cout<<endl<<endl;

cout<<"--------------------------------------------------------------
--------------------------- \n";
cout<<"Your choice: ";
cin>>var;
switch(var){
case 1:
if(length%2==0){
countingSort(shortest, length);
}
else{
heapSortDescending(shortest, length);
}
break;
case 2:
if(length%2==0){
comboSort(shortest, length);
}
else{
radixSortDescending(shortest, length);
}
break;
case 3: break;
}
int i = 0;
7
for(int j = indexJstart; j <= indexJend; j++){
matrix[indexI][j] = shortest[i];
i++;
}
cout<<"Modified matrix: \n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cout<<matrix[i][j]<< " ";
}
cout<<endl;
}
}
}

8
FlowChart:

9
10
11
12
13
14
15
Outputs:

16
Conclusion

During this lab work, I had the opportunity to work with matrices using
pointers, which gave me valuable experience in memory allocation and data
manipulation. I also explored different sorting algorithms such as radix sort, combo
sort, counting sort, and heap sort, which helped me gain a deeper understanding of
how to efficiently sort large datasets.

Through working with matrices and sorting algorithms, I learned how to


manipulate complex data structures and apply different techniques to efficiently
process and sort large amounts of data. This experience will be useful in future
projects that involve working with large datasets and complex data structures.

Overall, this lab work was a valuable learning experience and helped me
develop my skills in data manipulation and sorting algorithms.

17

You might also like