Sorting Solution - Machine Problem 6
Sorting Solution - Machine Problem 6
int main() {
int n;
int arr[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
quickSort(arr, 0, n - 1);
return 0;
}
#include <stdio.h>
Bubble Sort repeatedly steps through the list,
compares adjacent elements, and swaps them if // Function to perform Bubble Sort
they are in the wrong order. The process continues void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
until the array is sorted.
// Last i elements are already sorted
for (int j = 0; j < n - i - 1; j++) {
Steps for 94925
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
1. Initial Array: [9, 4, 9, 2, 5]
int temp = arr[j];
2. Pass 1:
arr[j] = arr[j + 1];
arr[j + 1] = temp;
• Compare 9 and 4 → Swap → [4, 9, 9, 2, 5] }
• Compare 9 and 9 → No swap → [4, 9, 9, 2, 5] }
• Compare 9 and 2 → Swap → [4, 9, 2, 9, 5] }
• Compare 9 and 5 → Swap → [4, 9, 2, 5, 9] }
int arr[n];
5. Pass 4:
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
• No swaps needed; the array is sorted.
scanf("%d", &arr[i]);
}
Program output:
printf("Original array: ");
printArray(arr, n);
bubbleSort(arr, n);
return 0;
}
#include <stdio.h>
Selection Sort divides the array into two parts: the
sorted part and the unsorted part. It repeatedly // Function to perform Selection Sort
selects the smallest (or largest) element from the void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
unsorted part and moves it to the sorted part.
// Find the minimum element in the unsorted part
int minIndex = i;
Steps for 94925 for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIndex]) {
1. Initial Array: [9, 4, 9, 2, 5] minIndex = j;
2. Pass 1: }
o Find the smallest element (2). }
o Swap 2 with the first element → [2, 4, 9, // Swap the found minimum element with the first
9, 5]. element of the unsorted part
3. Pass 2: int temp = arr[minIndex];
o Find the smallest element in the arr[minIndex] = arr[i];
remaining array (4). arr[i] = temp;
o No swap needed → [2, 4, 9, 9, 5]. }
4. Pass 3: }
o Find the smallest element in the
remaining array (5). // Function to print an array
o Swap 5 with the first unsorted element void printArray(int arr[], int size) {
(9) → [2, 4, 5, 9, 9]. for (int i = 0; i < size; i++)
5. Pass 4: printf("%d ", arr[i]);
o Remaining elements (9 and 9) are printf("\n");
already sorted. }
int arr[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
selectionSort(arr, n);
return 0;
}
#include <stdio.h>
Merge Sort is a divide-and-conquer algorithm. It
systematically splits the array into smaller // Function to merge two subarrays
subarrays, sorts those subarrays, and merges void merge(int arr[], int left, int mid, int right) {
int n1 = mid - left + 1;
them back together to form a sorted array.
int n2 = right - mid;
How It Works // Create temporary arrays
int L[n1], R[n2];
1. Divide the Array:
o Split the array into two halves // Copy data to temporary arrays
repeatedly until each subarray has for (int i = 0; i < n1; i++)
only one element (a single element L[i] = arr[left + i];
is always sorted). for (int j = 0; j < n2; j++)
2. Conquer (Sort and Merge): R[j] = arr[mid + 1 + j];
o Combine (merge) two sorted
subarrays into a single sorted array. // Merge the temporary arrays back into arr[left...right]
int i = 0, j = 0, k = left;
During the merging process:
while (i < n1 && j < n2) {
▪ Compare the elements of
if (L[i] <= R[j]) {
both subarrays. arr[k] = L[i];
▪ Place the smaller element i++;
into the resulting array. } else {
▪ Continue until all elements arr[k] = R[j];
from both subarrays are j++;
added to the result. }
3. Combine Results: k++;
o The merging process continues }
recursively, resulting in the fully
// Copy the remaining elements of L[], if any
sorted array.
while (i < n1) {
arr[k] = L[i];
Steps for Example Array: [9, 4, 9, 2, 5] i++;
k++;
1. Initial Array: [9, 4, 9, 2, 5] }
• Split into two halves: [9, 4, 9] and [2, 5]. // Copy the remaining elements of R[], if any
while (j < n2) {
arr[k] = R[j];
2. Divide Further: j++;
k++;
• Split [9, 4, 9] into [9] and [4, 9]. }
}
• Split [4, 9] into [4] and [9].
// Function to perform Merge Sort
• Split [2, 5] into [2] and [5].
void mergeSort(int arr[], int left, int right) {
if (left < right) {
3. Start Merging: int mid = left + (right - left) / 2;
• Merge [2] and [5] → [2, 5]. // Merge the sorted halves
merge(arr, left, mid, right);
4. Final Merge: }
}
int arr[n];
printf("Enter the elements of the array:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
mergeSort(arr, 0, n - 1);
return 0;
}