NATIONAL INSTITUTE OF TECHNOLOGY DELHI
Department of Computer Science and Engineering
Assignment of
Design and Analysis of Algorithm (CSBB 202)
Submitted by:
Aniket Raj (241210015)
Submitted to:
Dr. Gunjan (Subject Coordinator, CSE Department)
Assignment 1
Aim:
a) Comparison of linear search and binary search.
Source Code:
#include<stdio.h>
#include<time.h>
void insertion_sort(int arr[],int n){
int key,j;
for(int i = 1;i<n;i++){
key = arr[i];
j = i-1;
while(j>=0 && arr[j]>key){
arr[j+1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
void Binary_Search(int arr[],int si,int ei, int x){
int mid = si + (ei-si)/2;
if(arr[mid] == x) {
printf("Yes the number is present in the array at the index of %d",mid);
printf("\n");
}
else if(arr[mid]>x) Binary_Search(arr,si,mid-1,x);
else Binary_Search(arr,mid+1,ei,x);
}
void Linear_Search(int arr[],int n,int x){
for(int i = 0;i<n;i++){
if(x == arr[i]){
printf("Yes the number is present in the array at the index of %d",i);
printf("\n");
break;
}
else continue;
}
}
int main(){
printf("Name = ANIKET RAJ \n");
printf("Roll no. = 241210015\n");
int n;
printf("Enter the size of the array: ");
scanf("%d",&n);
int arr[n];
printf("Enter the element of the array: ");
for(int i = 0;i<n;i++){
scanf("%d",&arr[i]);
}
int x;
printf("Enter the number that you want to search in the array: ");
scanf("%d",&x);
insertion_sort(arr,n);
printf("Linear_Search\n");
clock_t start = clock();
Linear_Search(arr,n,x);
clock_t end = clock();
double time_taken = (double)(end-start)/CLOCKS_PER_SEC;
printf("Time Taken is : %f seconds",time_taken);
printf("\n");
printf("Binary_Search\n");
clock_t start1 = clock();
Binary_Search(arr,0,n-1,x);
clock_t end1 = clock();
double time_taken1 = (double)(end1-start1)/CLOCKS_PER_SEC;
printf("Time Taken is : %f seconds",time_taken1);
printf("\n");
}
Output:
b) Implement insertion sort.
Source code:
#include<stdio.h>
void insertion_sort(int arr[],int n){
int key,j;
for(int i = 1;i<n;i++){
key = arr[i];
j = i-1;
while(j>=0 && arr[j]>key){
arr[j+1] = arr[j];
j--;
}
arr[j + 1] = key;
}
printf("Sorted array is : ");
for(int i = 0;i<n;i++){
printf("%d ",arr[i]);
}
printf("\n");
}
int main(){
int n;
printf("Name = ANIKET RAJ \n");
printf("Roll no. = 241210015\n");
printf("Enter the size of an array : ");
scanf("%d",&n);
int arr[n];
printf("Enter the elements of an array here : ");
for(int i = 0;i<n;i++){
scanf("%d",&arr[i]);
}
insertion_sort(arr,n);
return 0;
}
Output:
c) Sort a given set of elements using the quicksort method and determine the time
required to sort the elements. Repeat the experiment for different values of n, the
number of elements in the list to be sorted.
Source Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
int partition(int arr[], int si, int ei) {
int pivot = arr[ei];
int i = si - 1;
for (int j = si; j < ei; j++) {
if (arr[j] <= pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[ei]);
return (i + 1);
}
void quick_sort(int arr[], int si, int ei) {
if (si < ei) {
int pi = partition(arr, si, ei);
quick_sort(arr, si, pi - 1);
quick_sort(arr, pi + 1, ei);
}
}
void print_array(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
printf("Name = ANIKET RAJ\n");
printf("Roll no. = 241210015\n");
srand(time(NULL)); // Seed for random numbers
int choice1 = 1;
while (choice1 == 1) {
int n;
printf("\nEnter the size of the array: ");
scanf("%d", &n);
int arr[n];
// Fill the array with random numbers between 0 and 999
for (int i = 0; i < n; i++) {
arr[i] = rand() % 1000;
}
printf("Unsorted array: ");
print_array(arr, n);
clock_t start = clock();
quick_sort(arr, 0, n - 1);
clock_t end = clock();
double time_taken = (double)(end - start) / CLOCKS_PER_SEC;
printf("Sorted array: ");
print_array(arr, n);
printf("Time Taken: %f seconds\n", time_taken);
printf("Enter Choice (1 to continue, 0 to exit): ");
scanf("%d", &choice1);
}
return 0;
}
Output: