DAA Practical 5
DAA Practical 5
Practical – 5
Q . Write user defined functions for the following sorting methods and compare their
performance by time measurement with random data and Sorted data.
1. Selection Sort
code :-
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
double total_time;
start = clock();
min_index = i;
min_index = j;
}
23012571013 U.V Patel College of Engineering Gayatri Solanki
2CSBS4102-Design And Analysis Of Algorithm
if (min_index != i) {
temp = arr[min_index];
arr[min_index] = arr[i];
arr[i] = temp;
end = clock();
int main() {
int arr[ARRAY_SIZE];
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
break;
case 2:
srand(time(NULL));
break;
default:
printf("Invalid choice!\n");
return 1;
selection_sort(arr, ARRAY_SIZE);
return 0;
2. Bubble Sort
code:-#include <stdio.h>
#include <stdlib.h>
#include <time.h>
double total_time;
start = clock();
flag = 0;
temp = arr[j];
arr[j + 1] = temp;
flag = 1; }
if (flag == 0) {
break;
23012571013 U.V Patel College of Engineering Gayatri Solanki
2CSBS4102-Design And Analysis Of Algorithm
end = clock();
int main() {
int arr[SIZE];
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
arr[i] = i + 1;
break;
case 2:
srand(time(NULL));
for (int i = 0; i < SIZE; i++) {
break;
default:
printf("Invalid choice!\n");
return 1; }
bubble_sort(arr, SIZE);
return 0; }
output:-
3. Insertion Sort
code:-#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int key, i, j;
double total_time;
start = clock();
key = arr[j];
i = j - 1;
arr[i + 1] = arr[i];
i = i - 1;
arr[i + 1] = key; }
end = clock();
int main() {
int arr[SIZE];
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
arr[i] = i + 1;
break;
case 2:
srand(time(NULL));
break;
default:
printf("Invalid choice!\n");
return 1;
insertion_sort(arr, SIZE);
return 0;
output:-
4. Merge Sort
code:-#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Function prototypes
void merge(int arr[], int start, int mid, int finish, int temp[]);
int main() {
int arr[SIZE];
int temp[SIZE];
int choice;
printf("1. Sorted Data\n2. Random Data\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
arr[i] = i + 1;
break;
case 2:
srand(time(NULL));
break;
default:
printf("Invalid choice!\n");
return 1; }
double total_time;
start = clock();
end = clock();
total_time = ((double)(end - start)) / CLOCKS_PER_SEC;
return 0; }
void merge(int arr[], int start, int mid, int finish, int temp[]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
temp[k++] = arr[i++];
temp[k++] = arr[j++];
arr[i] = temp[i];
output:-
6. Quick Sort
code:-#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int a[size];
int choice, i;
double total_time;
scanf("%d", &choice);
switch (choice) {
case 1:
a[i] = i + 1;
break;
case 2:
srand(time(NULL));
break;
default:
printf("Invalid choice!\n");
return 0;
start = clock();
end = clock();
return 0;
start++;
end--;
a[start] = a[end];
a[end] = temp;
a[lb] = a[end];
a[end] = pivot;
return end;
output:-
23012571013 U.V Patel College of Engineering Gayatri Solanki