0% found this document useful (0 votes)
9 views10 pages

Untitled Document

The document presents the implementation and time analysis of various sorting algorithms including Bubble Sort, Insertion Sort, Selection Sort, Quick Sort, and Merge Sort, along with their respective source codes. Additionally, it includes implementations of Linear Search and Binary Search algorithms, also with source code. Each section provides example outputs demonstrating the functionality of the algorithms.

Uploaded by

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

Untitled Document

The document presents the implementation and time analysis of various sorting algorithms including Bubble Sort, Insertion Sort, Selection Sort, Quick Sort, and Merge Sort, along with their respective source codes. Additionally, it includes implementations of Linear Search and Binary Search algorithms, also with source code. Each section provides example outputs demonstrating the functionality of the algorithms.

Uploaded by

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

210020107074

ADA(3150703)

Experiment 1
Aim : Implementation and Time analysis of sorting algorithms. Bubble sort,
Selection sort, Insertion sort, Merge sort and Quicksort.

1. Bubble sort:
Source Code:
#include <stdio.h>

void swap(int* xp, int* yp)


{ int temp = *xp; *xp
= *yp;
*yp = temp;
}

void bubbleSort(int arr[], int n)


{
int i, j;
bool swapped; for (i = 0; i < n - 1; i++)
{ swapped = false; for (j = 0; j < n - i - 1; j++)
{ if (arr[j] > arr[j + 1]) { swap(&arr[j], &arr[j +
1]); swapped = true;
}
}

if (swapped == false)
break;
}
}

void printArray(int arr[], int size)


{
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
}

int main()
{ int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
int n = sizeof(arr) /
sizeof(arr[0]); bubbleSort(arr, n);
210020107074
ADA(3150703)

printf("Sorted array: \n");


printArray(arr, n); return 0;
}

Output:

2.Insertion sort:
Source Code:
#include <stdio.h>

void insert(int a[], int n)


{ int i, j, temp; for (i =
1; i < n; i++) {
temp = a[i];
j = i - 1;

while(j>=0 && temp <= a[j])


{ a[j+1] = a[j];
j = j-1; }
a[j+1] = temp;
}
}

void printArr(int a[], int n)


{
int i;
210020107074
ADA(3150703)

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


printf("%d ", a[i]); }

int main()
{ int a[] = { 12, 31, 25, 8, 32, 17 }; int n =
sizeof(a) / sizeof(a[0]); printf("Before sorting
array elements are - \n"); printArr(a, n);
insert(a, n); printf("\nAfter sorting array
elements are - \n"); printArr(a, n);

return 0;
}

Output:

3.Selection sort:
Source code:
#include <stdio.h>

void selection(int arr[], int n)


{ int i, j, small;

for (i = 0; i < n-1; i++)


{ small = i;
for (j =
i+1; j <
n; j++) if
(arr[j] <
arr[small
]) small
= j;
int temp = arr[small];
arr[small] = arr[i];
arr[i] = temp;
210020107074
ADA(3150703)

}
}

void printArr(int a[], int n)


{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]); }

int main()
{ int a[] = { 12, 31, 25, 8, 32, 17 }; int n =
sizeof(a) / sizeof(a[0]); printf("Before sorting
array elements are - \n"); printArr(a, n);
selection(a, n); printf("\nAfter sorting array
elements are - \n"); printArr(a, n); return 0;
}

Output:

4.Quick Sort:
Source Code:
#include <stdio.h>

void swap(int* a, int* b)


{ int t = *a;
*a = *b;
*b =
t; }

int partition(int arr[], int low, int high)


210020107074
ADA(3150703)

{ int pivot = arr[high]; int i = (low -

1); for (int j = low; j <= high - 1; j+

+) {

if (arr[j] < pivot) {


i++;

swap(&arr[i], &arr[j]);
} } swap(&arr[i + 1],
&arr[high]); return (i + 1); }

void quickSort(int arr[], int low, int high)


{ if (low < high) { int pi =

partition(arr, low, high);

quickSort(arr, low, pi - 1);


quickSort(arr, pi + 1, high);
}
}

int main()
{ int arr[] = { 10, 7, 8, 9, 1, 5 };
int N = sizeof(arr) /
sizeof(arr[0]);

quickSort(arr, 0, N - 1);
printf("Sorted array: \n");
for (int i = 0; i < N; i++)
printf("%d ", arr[i]);
return 0;
}

Output:
210020107074
ADA(3150703)

5.Merge sort:
Source Code :
#include <stdio.h>

void merge(int a[], int beg, int mid, int end)


{ int i, j, k;
int n1 = mid - beg + 1;
int n2 = end - mid;

int LeftArray[n1], RightArray[n2];

for (int i = 0; i < n1; i++)


LeftArray[i] = a[beg + i]; for
(int j = 0; j < n2; j++)
RightArray[j] = a[mid + 1 +
j];

i = 0; j = 0; k = beg;
while (i < n1 && j <
n2) { if(LeftArray[i] <=
RightArray[j])
{ a[k] = LeftArray[i];
i++; }
else
{ a[k] = RightArray[j];
j++; } k++; } while
(i<n1)
{ a[k] = LeftArray[i];
i++; k++; }
210020107074
ADA(3150703)

while (j<n2)
{ a[k] = RightArray[j];
j++; k++;
}
}

void mergeSort(int a[], int beg, int end)


{ if (beg < end)
{ int mid = (beg + end) / 2;
mergeSort(a, beg, mid);
mergeSort(a, mid + 1,
end); merge(a, beg, mid,
end);
}
}

void printArray(int a[], int n)


{
int i;
for (i = 0; i < n; i++)
printf("%d ", a[i]); printf("\
n"); }

int main()
{ int a[] = { 12, 31, 25, 8, 32, 17, 40, 42 }; int n =
sizeof(a) / sizeof(a[0]); printf("Before sorting
array elements are - \n"); printArray(a, n);
mergeSort(a, 0, n - 1); printf("After sorting
array elements are - \n"); printArray(a, n);
return 0;
}

Output:
210020107074
ADA(3150703)

Experiment 2
Aim: Implementation And Time Analysis Of Linear And Binary Search.

1.Implement the Linear search program.


Source Code:
#include <stdio.h> int
linearSearch(int a[], int n, int val) {
for (int i = 0; i < n; i++)
{ if (a[i] == val)
return i+1;
} return -1; } int main() { int a[] = {70, 40, 30, 11, 57,
41, 25, 14, 52}; int val = 25; int n = sizeof(a) / sizeof(a[0]);
int res = linearSearch(a, n, val); printf("The elements of the
array are - "); for (int i = 0; i < n; i++) printf("%d ", a[i]);
printf("\nElement to be searched is - %d", val); if (res == -
1) printf("\nElement is not present in the array"); else
printf("\nElement is present at %d position of array", res);
return 0;
}

Output:

2.Implement binary search algorithm using recursive Method.


Source Code:
#include <stdio.h>

int binary_search(int arr[], int left, int right, int target) {


210020107074
ADA(3150703)

while (left <= right) {


int mid = left + (right - left) / 2;

if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target)
{ left = mid + 1;
} else { right =
mid - 1;
}
}

return -
1; }

int main() {
int arr[] = {1, 3, 5, 7, 9}; int n = sizeof(arr) /
sizeof(arr[0]); int target = 5; int index =
binary_search(arr, 0, n - 1, target);
if (index == -1) {
printf("Target not found\n");
} else { printf("Target found at index %d\n",
index);
}
return 0;
}

Output:
210020107074
ADA(3150703)

You might also like