0% found this document useful (0 votes)
3 views

Lab 3

Uploaded by

22052659
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab 3

Uploaded by

22052659
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab – 3

Name-Rahul Churiwal

Roll No. -22052659

Sec-CSE_40
3.1 Aim of the program: Write a menu driven program to sort list of array elements
using Merge
Sort technique and calculate the execution time only to sort the elements. Count the
number of
comparisons.
Note#
● To calculate execution time, assume that single program is under execution in the CPU.
● Number of elements in each input file should vary from 300 to 500 entries.
● For ascending order: Read data from a file “inAsce.dat” having content 10 20 30 40.....,
Store the result in “outMergeAsce.dat”.
● For descending order: Read data from a file “inDesc.dat” having content 90 80 70 60....,
Store the result in “outMergeDesc.dat”.
● For random data: Read data from a file “inRand.dat” having content 55 66 33 11 44 ...,
Store the result in “outMergeRand.dat”

Sample Input from file:


MAIN MENU (MERGE SORT)
1. Ascending Data
2. Descending Data
3. Random Data
4. ERROR (EXIT)
Output:
Enter option: 1
Before Sorting: Content of the input file
After Sorting: Content of the output file
Number of Comparisons: Actual
Execution Time: lapse time in nanosecond

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

void reverse(int arr[], int size)


{
for (int i = 0; i < size / 2; i++)
{
int temp = arr[i];
arr[i] = arr[size - 1 - i];
arr[size - i - 1] = temp;
}
}
void merge(int arr[], int start, int end, int mid, int comparisions)
{
int size1 = mid - start + 1;
int size2 = end - mid;
int arr1[size1];
int arr2[size2];
for (int i = 0; i < size1; i++)
{
arr1[i] = arr[start + i];
}
for (int j = 0; j < size2; j++)
{
arr2[j] = arr[mid + 1 + j];
}
int i = 0, j = 0, k = start;
while (i < size1 && j < size2)
{
comparisions++;
if (arr1[i] <= arr2[j])
{
arr[k] = arr1[i];
i++;
}
else
{
arr[k] = arr2[j];
j++;
}
k++;
}
while (i < size1)
{
comparisions++;
arr[k] = arr1[i];
i++;
k++;
}
while (j < size2)
{
comparisions++;
arr[k] = arr2[j];
j++;
k++;
}
}
int mergesort(int arr[], int start, int end)
{
if (start >= end)
return 0;
int mid = start + (end - start) / 2;
int comparision = 0;
mergesort(arr, start, mid);
mergesort(arr, mid + 1, end);
merge(arr, start, end, mid, comparision);
return comparision;
}
int main()
{
int arr[100];
int i = 0;
FILE *s, *d;
int number;
s = fopen("input.txt", "r");
while (fscanf(s, "%d", &number) != EOF)
{
arr[i++] = number;
}
int arr_size = i;
fclose(s);
printf("Printing Array\n");
for (int i = 0; i < arr_size; i++)
{
printf("%d | ", arr[i]);
}
d = fopen("ascending.txt", "w");
int comp = mergesort(arr, 0, arr_size - 1);
printf("Printing Array\n");
for (int i = 0; i < arr_size; i++)
{
fprintf(d, "%d | ", arr[i]);
}
fclose(d);
reverse(arr, arr_size);
d = fopen("descending.txt", "w");
for (int i = 0; i < arr_size; i++)
{
fprintf(d, "%d | ", arr[i]);
}
}

Output:
3.2 Aim of the program: Write a menu driven program to sort a list of elements in
ascending
order using Quick Sort technique. Each choice for the input data has its own disc file.
A separate
output file can be used for sorted elements. After sorting display the content of the
output file
along with number of comparisons. Based on the partitioning position for each
recursive call,
conclude the input scenario is either best-case partitioning or worst-case
partitioning.
Note#
● The worst-case behavior for quicksort occurs when the partitioning routine produces one
subproblem with n-1 elements and one with 0 elements. The best-case behaviour
occurred in most even possible split, PARTITION produces two subproblems, each of
size no more than n/2.
● Number of elements in each input file should vary from 300 to 500 entries.
● For ascending order: Read data from a file “inAsce.dat” having content 10 20 30 40.....,
Store the result in “outQuickAsce.dat”.
● For descending order: Read data from a file “inDesc.dat” having content 90 80 70 60....,
Store the result in “outQuickDesc.dat”.
● For random data: Read data from a file “inRand.dat” having content 55 66 33 11 44 ...,
Store the result in “outQuickRand.dat”
Sample Input from file:
MAIN MENU (QUICK SORT)
1. Ascending Data
2. Descending Data
3. Random Data
4. ERROR (EXIT)
Output:
Enter option: 1
Before Sorting: Content of the input file
After Sorting: Content of the output file
Number of Comparisons: Actual
Scenario: Best or Worst-case

Output:

You might also like