202411005_ CS162_4
202411005_ CS162_4
Format/Frame Work
Question Provided by the professor.
Program Must be typed content (not screenshots).
Can include program code, syntax, or relevant theory.
Include your roll number and name in comments.
Output: Typed or copy-pasted content of your program's output is preferred.
If difficult, output screenshots are acceptable.
Note: Ensure your name is included in the comments of all submitted programs.
Your
Observation
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Sorting process:\n\n");
int comp = 0;
int shift = 0;
if (shift == 0) {
printf("Best Case: O(n) - Already Sorted Input.\n");
} else if (shift == (n * (n - 1)) / 2) {
printf("Worst Case: O(n^2) - Reverse Sorted Input.\n");
} else {
printf("Average Case: O(n^2) - Random Order Input.\n");
}
printf("\n");
return 0;
}
Output:
Your This code teaches Insertion Sort, showing how it compares and shifts elements to sort an
Observation array, with explanations for best, worst, and average cases
int main() {
int n, swap = 0, com = 0;
scanf("%d", &n);
int arr[n];
printf("Sorting process:\n\n");
for(int i = 0; i < n - 1; i++){
int swapped = 0;
printf("Pass %d:\n", i + 1);
for(int j = 0; j < n - i - 1; j++) {
printf("Comparing %d and %d -> ", arr[j], arr[j + 1]);
if(arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
printf("Swapped to get: ");
for(int k = 0; k < n; k++) printf("%d ", arr[k]);
printf("\n");
swap++;
swapped = 1;
} else {
printf("No Swap, so Same previous View: ");
for(int k = 0; k < n; k++) printf("%d ", arr[k]);
printf("\n");
}
com++;
}
if(!swapped) break;
printf("\n");
}
int sum = n * (n - 1) / 2;
if(swap == 0) {
printf("Best Case: O(n) - Already Sorted Input.\n");
} else if(swap == sum) {
printf("Worst Case: O(n^2) - Reverse Sorted Input.\n");
} else {
printf("Average Case: O(n^2) - Random Order Input.\n");
}
return 0;
}
Output:
Your This code demonstrates Bubble Sort, showing how it compares and swaps adjacent
Observation elements to sort an array, with explanations for best, worst, and average cases.
int main()
{
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
int c = 0;
int s = 0;
printf("Sorting process:\n");
for(int i=0;i<n-1;i++){
printf("Pass %d (i=%d):\n", i+1, i);
printf("Unsorted: [");
for(int k=i; k<n; k++) {
printf("%d", arr[k]);
if(k < n-1) printf(" ");
}
printf("]\n");
int min=i;
for(int j=i+1;j<n;j++){
c++;
int m = arr[min];
if(arr[j]<arr[min]) {
printf("Comparing %d and %d ==> Now Min %d\n", m, arr[j], arr[j]);
min=j;
}
else {
printf("Comparing %d and %d ==> Now Min %d\n", m, arr[j], m);
}
}
printf("Final Minimum: %d (index %d).\n", arr[min], min);
if(min != i) {
int x = arr[i];
int y = arr[min];
arr[i]=arr[i]^arr[min];
arr[min]=arr[i]^arr[min];
arr[i]=arr[i]^arr[min];
s++;
printf("Swapped %d (index %d) and %d (index %d) -> [", x, i, y, min);
for(int k=0; k<n; k++) {
printf("%d", arr[k]);
if(k < n-1) printf(" ");
}
printf("]\n");
}
else {
printf("No Swap needed, %d remains at position %d-> [", arr[i], i);
for(int k=0; k<n; k++) {
printf("%d", arr[k]);
if(k < n-1) printf(" ");
}
printf("]\n");
}
}
printf("Pass %d (i=%d):\n", n, n-1);
printf("Only one element left. Sorting complete.\n");
return 0;
}
Output:
Your This code explains selection sort, where it finds the smallest element in each pass and
Observation swaps it to the right position. It shows the sorting process step-by-step, including
comparisons and swaps, and counts the total comparisons and swaps made.
int temp[100];
int total_splits = 0;
int total_comparisons = 0;
int total_swaps = 0;
int left = l;
int right = mid + 1;
int i = 0;
ms(arr, l, mid);
ms(arr, mid + 1, h);
mergeing_array(arr, l, mid, h);
}
int main() {
printf("Starting Merge Sort...\n");
int n;
scanf("%d",&n);
int arr[n];
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
int is_sorted = 1;
int is_reverse_sorted = 1;
for (int i = 0; i < n - 1; i++) {
if (arr[i] > arr[i+1]) is_sorted = 0;
if (arr[i] < arr[i+1]) is_reverse_sorted = 0;
}
ms(arr, 0, n - 1);
printf("\nPerformance Analysis:\n");
printf("Total Splits: %d\n", total_splits);
printf("Total Comparisons: %d\n", total_comparisons);
printf("Total Swaps: %d\n", total_swaps);
if (is_sorted) {
printf("Input Type: Best Case\n");
} else if (is_reverse_sorted) {
printf("Input Type: Worst Case\n");
} else {
printf("Input Type: Average Case\n");
}
return 0;
}
Output:
Your This code demonstrates merge sort, showing how it splits the array into smaller parts,
Observation merges them in sorted order, and counts splits, comparisons, and swaps. It also analyzes
the time complexity for best, worst, and average cases.
int size;
int splits = 0;
int comparisons = 0;
int swaps = 0;
int main() {
scanf("%d", &size);
int arr[size];
for (int i = 0; i < size; i++) scanf("%d", &arr[i]);
printf("Initial Array: ");
for (int i = 0; i < size; i++) printf("%d ", arr[i]);
printf("\n\nSorting Steps:\n");
qs(arr, 0, size - 1);
printf("\nSorted Array: ");
for (int i = 0; i < size; i++) printf("%d ", arr[i]);
printf("\n\nPerformance Analysis:\n");
printf("Total Splits: %d\n", splits);
printf("Total Comparisons: %d\n", comparisons);
printf("Total Swaps: %d\n", swaps);
if (splits == size - 1) printf("Case: Worst Case (O(n^2))\n");
else printf("Case: Best Case (O(n log n))\n");
return 0;
}
Output:
Your This code demonstrates quick sort, showing how it selects a pivot, partitions the array, and
Observation recursively sorts the subarrays. It counts splits, comparisons, and swaps, and analyzes the
time complexity for best and worst cases.
int comps = 0;
int swaps = 0;
if (left < n) {
printf(" Comparing %d (index %d) with left child %d (index %d)\n", arr[large], large,
arr[left], left);
comps++;
if (arr[left] > arr[large]) {
large = left;
}
}
if (right < n) {
printf(" Comparing %d (index %d) with right child %d (index %d)\n", arr[large],
large, arr[right], right);
comps++;
if (arr[right] > arr[large]) {
large = right;
}
}
if (large != i) {
printf(" !! SWAPPING %d (index %d) with %d (index %d)\n", arr[i], i, arr[large],
large);
int temp = arr[large];
arr[large] = arr[i];
arr[i] = temp;
swaps++;
printf(" Array state: ");
print_array(arr, n, i, large);
heapify(arr, n, large);
}
}
printf("\nSorting Phase:\n\n");
for (int i = n - 1; i > 0; i--) {
printf("=== Moving root %d (index 0) to final position %d ===\n", arr[0], i);
printf(" Before swap: ");
print_array(arr, i + 1, 0, i);
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
swaps++;
printf(" After swap: ");
print_array(arr, n, 0, i);
printf("Heapifying new root:");
heapify(arr, i, 0);
if (i > 1) printf("\n");
}
}
int main() {
int n;
scanf("%d", &n);
int arr[n];
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Initial array: ");
print_array(arr, n, -1, -1);
printf("\n");
heapsort(arr, n);
printf("\nFinal sorted array: ");
print_array(arr, n, -1, -1);
printf("Total comparisons: %d\n", comps);
printf("Total swaps: %d\n", swaps);
return 0;
}
Output:
Your This code demonstrates heap sort, showing how it builds a max heap, swaps the root with
Observation the last element, and heapifies the remaining array. It counts comparisons and swaps, and
visualizes the sorting process step-by-step.