0% found this document useful (0 votes)
14 views5 pages

Sorting Algorithms in Computer Science With C Code Snippets For Each

C keys as words
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)
14 views5 pages

Sorting Algorithms in Computer Science With C Code Snippets For Each

C keys as words
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/ 5

SORTING ALGORITHMS IN COMPUTER SCIENCE WITH C CODE SNIPPETS

FOR EACH.

1. Bubble Sort

Description: Bubble Sort repeatedly steps through the list, compares adjacent elements, and
swaps them if they are in the wrong order. The pass through the list is repeated until the list is
sorted.

• Time Complexity: O(n²)


• Best For: Small datasets or when you know the list is nearly sorted.

c
Copy code
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]) {
// Swap arr[j] and arr[j+1]
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}

2. Selection Sort

Description: Selection Sort divides the array into a sorted and an unsorted part. It repeatedly
selects the smallest (or largest) element from the unsorted part and moves it to the end of the
sorted part.

• Time Complexity: O(n²)


• Best For: Simple implementation, small datasets.

c
Copy code
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;
}
}
// Swap the found minimum element with the first element
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
}
}

3. Insertion Sort
Description: Insertion Sort builds the final sorted array one item at a time. It’s much like
sorting playing cards in your hands. Elements from the unsorted part are picked and placed at
the correct position in the sorted part.

• Time Complexity: O(n²)


• Best For: Small datasets or nearly sorted data.

c
Copy code
void insertionSort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
// Move elements of arr[0..i-1] that are greater than key to one
position ahead of their current position
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}

4. Merge Sort

Description: Merge Sort is a divide-and-conquer algorithm. It divides the input array into
two halves, calls itself for the two halves, and then merges the two sorted halves.

• Time Complexity: O(n log n)


• Best For: Large datasets, stable sort.

c
Copy code
void merge(int arr[], int l, int m, int r) {
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];

for (int i = 0; i < n1; i++)


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

int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1) {


arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}

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


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

5. Quick Sort

Description: Quick Sort is another divide-and-conquer algorithm. It picks an element as a


pivot and partitions the array around the pivot. There are many ways to pick the pivot.

• Time Complexity: O(n log n) on average, but O(n²) in the worst case.
• Best For: Large datasets.

c
Copy code
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);

for (int j = low; j <= high - 1; j++) {


if (arr[j] < pivot) {
i++;
// Swap arr[i] and arr[j]
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// Swap arr[i + 1] and arr[high] (or pivot)
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);
}
}

6. Heap Sort
Description: Heap Sort is based on a binary heap data structure. It first builds a max heap
and then repeatedly extracts the maximum element from the heap and rebuilds it.

• Time Complexity: O(n log n)


• Best For: Large datasets, in-place sorting.

c
Copy code
void heapify(int arr[], int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;

if (left < n && arr[left] > arr[largest])


largest = left;
if (right < n && arr[right] > arr[largest])
largest = right;
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}

void heapSort(int arr[], int n) {


for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
for (int i = n - 1; i > 0; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}

7. Radix Sort

Description: Radix Sort processes integers by sorting them digit by digit, from the least
significant digit to the most significant. It requires a stable subroutine, such as Counting Sort,
for sorting digits.

• Time Complexity: O(nk), where k is the number of digits in the largest number.
• Best For: Large datasets with integer keys.

c
Copy code
int getMax(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > max)
max = arr[i];
return max;
}

void countSort(int arr[], int n, int exp) {


int output[n];
int count[10] = {0};

for (int i = 0; i < n; i++)


count[(arr[i] / exp) % 10]++;

for (int i = 1; i < 10; i++)


count[i] += count[i - 1];

for (int i = n - 1; i >= 0; i--) {


output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}

for (int i = 0; i < n; i++)


arr[i] = output[i];
}

void radixSort(int arr[], int n) {


int m = getMax(arr, n);

for (int exp = 1; m / exp > 0; exp *= 10)


countSort(arr, n, exp);
}

Summary of Sorting Algorithms:

Algorithm Time Time Complexity Time Space


Complexity (Average) Complexity Complexity
(Best) (Worst)
Bubble O(n) O(n²) O(n²) O(1)
Sort
Selection O(n²) O(n²) O(n²)
Sort
4o

You might also like