Untitled Document
Untitled Document
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>
if (swapped == false)
break;
}
}
int main()
{ int arr[] = { 64, 34, 25, 12, 22, 11, 90 };
int n = sizeof(arr) /
sizeof(arr[0]); bubbleSort(arr, n);
210020107074
ADA(3150703)
Output:
2.Insertion sort:
Source Code:
#include <stdio.h>
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>
}
}
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>
+) {
swap(&arr[i], &arr[j]);
} } swap(&arr[i + 1],
&arr[high]); return (i + 1); }
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>
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++;
}
}
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.
Output:
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)