DSA Lab Assignment 2 Answer
DSA Lab Assignment 2 Answer
Ques: Following numbers are given for sorting. Write a C program to apply Insertion Sort on them.
64, 34, 25, 12, 22, 11, 90, 5
Ans:
#include <stdio.h>
int i, j, temp;
temp = arr[i];
j = i - 1;
arr[j + 1] = arr[j];
j--;
arr[j + 1] = temp;
int i;
printf("\n");
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90, 5};
printArray(arr, n);
insertionSort(arr, n);
printArray(arr, n);
return 0;
Output
Ques:
You are working with a large dataset and need to efficiently find the Kth smallest element. How
would you modify the Quick Sort algorithm to find this element without sorting the entire array?
Ans:
#include <stdio.h>
int i = low - 1;
i++;
arr[i] = arr[j];
arr[j] = temp;
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
if (low == high) {
return arr[low];
}
int pivotIndex = partition(arr, low, high);
if (k == pivotIndex) {
return arr[k];
} else {
int main() {
return 0;
Output: