Dsa 3
Dsa 3
Set a
a) Sort a random array of n integers (accept the value of n from user) in ascending order by using
recursive Counting sort algorithm.
#include <stdio.h>
#include <stdlib.h>
max = arr[i];
return max;
if (depth == 0) {
return;
count[arr[i]]++;
output[count[arr[i]] - 1] = arr[i];
count[arr[i]]--;
arr[i] = output[i];
}
free(count);
free(output);
printf("\n");
int main() {
int n;
scanf("%d", &n);
int arr[n];
printf("\n");
printArray(arr, n);
return 0;
b) Sort a random array of n integers (accept the value of n from user) in ascending order
by using a recursive Merge sort algorithm.
#include <stdio.h>
#include <stdlib.h>
int i, j, k;
int n1 = mid - left + 1;
i = 0;
j = 0;
k = left;
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
arr[k] = R[j];
j++;
k++;
printf("\n");
int main() {
int n;
scanf("%d", &n);
int arr[n];
printf("\n");
mergeSort(arr, 0, n - 1);
printArray(arr, n);
return 0;
}
a)Sort a random array of n integers (create a random array of n integers) in ascending
order by using recursive Quick sort algorithm.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
*a = *b;
*b = temp;
i++;
swap(&arr[i], &arr[j]);
return (i + 1);
quickSort(arr, pi + 1, high);
printf("\n");
}
int main() {
int n;
scanf("%d", &n);
int arr[n];
srand(time(0));
generateRandomArray(arr, n);
printArray(arr, n);
quickSort(arr, 0, n - 1);
printArray(arr, n);
return 0;
}
Set b
1) Read the data from the ‘employee.txt’ file and sort on age using Counting sort, Merge
sort, Quick sort and write the sorted data to another file 'sortedemponage.txt'.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 50
typedef struct {
char name[MAX_NAME_LENGTH];
int age;
} Employee;
if (file == NULL) {
return -1;
int i = 0;
while (fscanf(file, "%s %d", employees[i].name, &employees[i].age) != EOF && i < MAX_EMPLOYEES) {
i++;
fclose(file);
return i;
if (file == NULL) {
return;
}
for (int i = 0; i < size; i++) {
fclose(file);
Employee output[n];
count[employees[i].age]++;
output[count[employees[i].age] - 1] = employees[i];
count[employees[i].age]--;
employees[i] = output[i];
int i = 0, j = 0, k = left;
employees[k] = L[i];
i++;
} else {
employees[k] = R[j];
j++;
k++;
employees[k] = L[i];
i++;
k++;
employees[k] = R[j];
j++;
k++;
i++;
employees[i] = employees[j];
employees[j] = temp;
}
employees[i + 1] = employees[high];
employees[high] = temp;
return (i + 1);
quickSort(employees, pi + 1, high);
destination[i] = source[i];
int main() {
Employee employees[MAX_EMPLOYEES];
Employee tempEmployees[MAX_EMPLOYEES];
if (numEmployees == -1) {
return 1;
countingSort(tempEmployees, numEmployees);
return 0;
}
2) Read the data from the file and sort on names in alphabetical order (use strcmp)
using Counting sort, Merge sort, Quick sort and write the sorted data to another file
'sortedemponname.txt'.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME_LENGTH 50
typedef struct {
char name[MAX_NAME_LENGTH];
int age;
} Employee;
if (file == NULL) {
return -1;
int i = 0;
while (fscanf(file, "%s %d", employees[i].name, &employees[i].age) != EOF && i < MAX_EMPLOYEES) {
i++;
fclose(file);
return i;
if (file == NULL) {
return;
fclose(file);
}
int i = 0, j = 0, k = left;
employees[k] = L[i];
i++;
} else {
employees[k] = R[j];
j++;
k++;
employees[k] = L[i];
i++;
k++;
employees[k] = R[j];
j++;
k++;
i++;
employees[i] = employees[j];
employees[j] = temp;
employees[i + 1] = employees[high];
employees[high] = temp;
return (i + 1);
quickSort(employees, pi + 1, high);
Employee output[n];
count[employees[i].name[0] - 'A']++;
count[employees[i].name[0] - 'A']--;
employees[i] = output[i];
int main() {
Employee employees[MAX_EMPLOYEES];
if (numEmployees == -1) {
return 1;
countingSort(employees, numEmployees);
return 0;
Set c
A) What modification is required to Merge sort to sort the integers in descending order?
#include <stdio.h>
int i = 0;
int j = 0;
int k = left;
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
arr[k] = R[j];
j++;
k++;
printf("\n");
int main() {
printArray(arr, arrSize);
printArray(arr, arrSize);
return 0;
}
b) Compare the system time taken by Merge sort and bubble sort by using time
command on a random array of integers of size 10000 or more.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
arr[j + 1] = temp;
int i = 0, j = 0, k = left;
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
arr[k] = R[j];
j++;
k++;
dest[i] = src[i];
int main() {
int arr[SIZE];
srand(time(NULL));
generateRandomArray(arr, SIZE);
bubbleSort(arrCopy, SIZE);
start = clock();
end = clock();
return 0;