0% found this document useful (0 votes)
0 views

Sorting Algorithms

Uploaded by

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

Sorting Algorithms

Uploaded by

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

Sorting Algorithms

1. What is Bubble Sort algorithm? How does it work? Write an


example code.
Answer:
Bubble Sort is a simple sorting algorithm that repeatedly steps through the list,
compares adjacent elements, and swaps them if they are in the wrong order.
This process is repeated until the list is sorted.
Working:
In each pass, the largest element "bubbles up" to the end of the array.
Example Code in C:

#include <stdio.h>
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}

int main() {
int arr[] = {5, 3, 8, 4, 2};
int n = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
return 0;
}

2. What is Selection Sort algorithm? How does it work? Write


an example code.

Sorting Algorithms 1
Answer:
Selection Sort is a sorting algorithm that divides the array into a sorted and
unsorted part, and repeatedly selects the minimum element from the unsorted
part and moves it to the sorted part.

Working:
It selects the smallest element from the unsorted part and swaps it with the
element at the beginning.
Example Code in C:

#include <stdio.h>
void selectionSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int min_idx = i;
for (int j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}

int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
return 0;
}

3. What is Insertion Sort algorithm? How does it work? Write an


example code.
Answer:

Sorting Algorithms 2
Insertion Sort is a simple sorting algorithm that builds the final sorted array one
element at a time. It works similarly to how people sort playing cards in their
hands.

Working:
It picks each element and inserts it into its correct position among the already
sorted elements on its left.

Example Code in C:

#include <stdio.h>
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key)
arr[j + 1] = arr[j--];
arr[j + 1] = key;
}
}

int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);
return 0;
}

4. What is Merge Sort algorithm? How does it work? Write an


example code.
Answer:
Merge Sort is a divide and conquer sorting algorithm. It divides the array into
two halves, sorts them recursively, and then merges the sorted halves.
Working:

Sorting Algorithms 3
It keeps dividing the array into halves until each sub-array has one element,
then merges them in sorted order.
Example Code in C:

#include <stdio.h>
void merge(int arr[], int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];

for (i = 0; i < n1; i++) L[i] = arr[l + i];


for (j = 0; j < n2; j++) R[j] = arr[m + 1 + j];

i = 0; j = 0; k = l;
while (i < n1 && j < n2)
arr[k++] = (L[i] <= R[j]) ? L[i++] : R[j++];
while (i < n1) arr[k++] = L[i++];
while (j < n2) arr[k++] = R[j++];
}

void mergeSort(int arr[], int l, int r) {


if (l < r) {
int m = (l + r) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}

int main() {
int arr[] = {38, 27, 43, 3, 9, 82, 10};
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
printf("Sorted array: ");
for (int i = 0; i < n; i++) printf("%d ", arr[i]);

Sorting Algorithms 4
return 0;
}

5. What is Quick Sort algorithm? How does it work? Write an


example code.
Answer:
Quick Sort is a divide and conquer algorithm. It picks a pivot element,
partitions the array around the pivot, and recursively sorts the partitions.

Working:
It rearranges the array such that all elements smaller than the pivot go to its left
and all greater go to its right.

Example Code in C:

#include <stdio.h>
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++)
if (arr[j] < pivot) {
int temp = arr[++i]; arr[i] = arr[j]; arr[j] = temp;
}
int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp;
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]);

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

Sorting Algorithms 6

You might also like