Sorting and other Topics
Contents
Define and classify the Sorting 1
Differentiate between Internal Sorting and External Sorting 3
Differentiate between Online Sorting and Offline Sorting in â€˜Câ€™ 6
List properties of Sorting Techniques in C 6
Write a C function/program to implement Bubble Sort 6
Bubble Sort with Optimization 6
Recall the Complexity of Bubble Sort 6
Write a C Function/Program to implement Selection Sort 6
Recall the Complexity of Selection Sort 6
Write a C Function/Program to implement Insertion Sort 6
Recall the Complexity of Insertion Sort 6
Write a C Function/Program to implement Quick Sort 6
Recall the Complexity of Quick Sort 6
Write a C function/Program to implement Merge Sort 6
Recall the Complexity of Merge Sort 6
Recall the procedure to install Eclipse C C++ Development Tool (CDT) 8.1.2 Eclipse 4.2.2 on windows 6
Write and execute your First C/C++ Program in Eclipse 6
Write an Algorithm for Radix Sort 6
Recall the Complexity of Radix Sort 6
Define and classify the Sorting
Sorting:
Sorting is the process of arranging a collection of elements in a specific order. The order can be based on various criteria, such as numerical value, alphabetical order, or customdefined criteria. Sorting is a fundamental operation in computer science and is used in various applications to organize and retrieve data efficiently.
Classification of Sorting Algorithms:
Sorting algorithms can be classified based on different characteristics, including their time complexity, space complexity, stability, and method of comparison. Here are some common classifications of sorting algorithms:
 Comparisonbased Sorting Algorithms:
Comparisonbased sorting algorithms compare elements using comparison operators (e.g., greater than, less than) to determine their relative order. Examples of comparisonbased sorting algorithms include:

 Bubble Sort
 Selection Sort
 Insertion Sort
 Merge Sort
 Quick Sort
 Heap Sort
 Noncomparisonbased Sorting Algorithms:
Noncomparisonbased sorting algorithms do not rely solely on element comparisons to determine the order. Instead, they exploit specific properties of the elements being sorted. Examples of noncomparisonbased sorting algorithms include:

 Counting Sort
 Radix Sort
 Bucket Sort
 Stable Sorting Algorithms:
Stable sorting algorithms maintain the relative order of elements with equal keys during the sorting process. If two elements have the same key, the one that appears first in the input will also appear first in the sorted output. Stable sorting algorithms are useful when preserving the original order of equal elements is necessary. Examples of stable sorting algorithms include:

 Insertion Sort
 Merge Sort
 Unstable Sorting Algorithms:
Unstable sorting algorithms do not guarantee the preservation of the original order of equal elements. If two elements have the same key, their relative order in the sorted output may differ from their order in the input. Examples of unstable sorting algorithms include:

 Selection Sort
 Quick Sort
 Heap Sort
 Inplace Sorting Algorithms:
Inplace sorting algorithms rearrange the elements within the given data structure itself, without requiring additional memory proportional to the size of the input. Inplace sorting algorithms are memoryefficient but may require more computational time compared to algorithms that use additional memory. Examples of inplace sorting algorithms include:

 Bubble Sort (with optimized implementation)
 Selection Sort
 Insertion Sort
 Quick Sort (with inplace partitioning)
 Outofplace Sorting Algorithms:
Outofplace sorting algorithms create and use additional memory space to store the sorted elements, resulting in a new data structure separate from the original input. Outofplace sorting algorithms require additional memory proportional to the size of the input but can be more efficient in terms of computational time compared to inplace algorithms. Examples of outofplace sorting algorithms include:

 Merge Sort
 Heap Sort
These classifications provide a framework to understand the characteristics and tradeoffs of different sorting algorithms, aiding in the selection of an appropriate algorithm based on the requirements of a specific problem or scenario.
Differentiate between Internal Sorting and External Sorting
Here’s a tabular comparison between Internal Sorting and External Sorting:
Internal Sorting  External Sorting  
Data Size  Suitable for sorting small to mediumsized datasets.  Designed for sorting large datasets that don’t fit in main memory. 
Memory Usage  Entire data set can fit in the main memory (RAM).  Requires additional memory for external storage (e.g., hard drives). 
Disk Access  No or minimal disk input/output operations.  Relies heavily on disk input/output operations. 
Efficiency  Typically faster due to direct access to memory.  Slower due to disk access and I/O operations. 
Data Movement  Data movement within memory is faster.  Data movement between memory and external storage is slower. 
Sorting Algorithms  Can use various sorting algorithms efficiently.  Specialized algorithms are designed for efficient disk access. 
Stability  Stability of sorting algorithms can be maintained.  Maintaining stability can be more challenging. 
Applications  Suitable for inmemory sorting and realtime applications.  Used for largescale data sorting, such as database operations. 
Examples of Algorithms  Quick Sort, Merge Sort, Insertion Sort, Selection Sort, etc.  External Merge Sort, External Quick Sort, Polyphase Merge Sort, etc. 
Note that the comparison above provides a general overview and may vary depending on specific implementations and hardware configurations.
Differentiate between Online Sorting and Offline Sorting in â€˜Câ€™
Here’s a tabular comparison between Online Sorting and Offline Sorting in the context of the C programming language:
Online Sorting  Offline Sorting  
Availability of Data  Data elements are continuously received or accessed one by one.  Entire data set is available before sorting begins. 
Sorting Process  Elements are sorted onthefly as they are received or accessed.  Sorting is performed on the complete data set. 
Data Structures  Often implemented using dynamic data structures (e.g., linked lists).  Can use various data structures, such as arrays or linked lists. 
Complexity  Can have varying time complexity depending on the algorithm used.  Time complexity can be analyzed and optimized based on the algorithm. 
Memory Usage  May require additional memory for dynamically managing data elements.  Memory usage can be controlled based on the chosen data structure. 
Efficiency  Sorting efficiency may be affected by the continuous data arrival.  Efficiency can be optimized since the entire data set is available. 
Applications  Useful for realtime data processing or continuous data streams.  Applicable for batch processing or scenarios with complete data sets. 
Examples  Insertion Sort (for small data streams), Online Bubble Sort, etc.  Merge Sort, Quick Sort, Heap Sort, etc. 
It’s important to note that the terms “online sorting” and “offline sorting” can have different interpretations depending on the context, and the definitions and characteristics can vary accordingly. The provided comparison gives a general understanding of the differences between online and offline sorting in the C programming language.
List properties of Sorting Techniques in C
Here are some common properties of sorting techniques in the C programming language:
 Time Complexity: Sorting techniques have different time complexities, which indicate the amount of time required to sort the data. Some techniques have better time complexity than others, resulting in faster sorting for larger datasets.
 Space Complexity: Sorting techniques have varying space complexities, which represent the amount of additional memory required to perform the sorting operation. Some techniques may require additional memory for auxiliary data structures, while others may operate inplace, using minimal or no additional memory.
 Stability: Stability refers to whether the sorting technique preserves the relative order of elements with equal keys. Stable sorting techniques maintain the original order of equal elements, while unstable sorting techniques do not guarantee this.
 Adaptability: Some sorting techniques are adaptive, meaning they take advantage of partially sorted or nearly sorted data, resulting in improved efficiency. Adaptive techniques may have different time complexity depending on the initial order of the data.
 Inplace Sorting: Inplace sorting techniques rearrange the elements within the given data structure itself, without requiring additional memory proportional to the size of the input. Inplace sorting techniques are memoryefficient but may require more computational time compared to algorithms that use additional memory.
 Recursive or Iterative: Sorting techniques can be implemented using either recursive or iterative approaches. Recursive implementations use function calls and may have higher overhead due to function stack operations. Iterative implementations use loops and can be more memoryefficient.
 Comparisonbased or Noncomparisonbased: Sorting techniques can be classified as comparisonbased or noncomparisonbased. Comparisonbased techniques compare elements using comparison operators to determine their order. Noncomparisonbased techniques exploit specific properties of the elements being sorted without relying solely on comparisons.
 Ease of Implementation: Different sorting techniques have varying levels of complexity in terms of implementation. Some techniques may be simpler to understand and code, while others may require more advanced algorithms or data structures.
 Scalability: Sorting techniques may have different levels of scalability, indicating how well they perform as the size of the dataset increases. Techniques that exhibit good scalability can efficiently handle larger datasets without a significant increase in time or space complexity.
 Stability in Resource Usage: Sorting techniques may have different resource usage patterns, such as CPU utilization, memory usage, and disk access. Some techniques may require high CPU usage but minimal memory, while others may involve significant disk input/output operations.
These properties help assess and compare different sorting techniques in terms of their performance, resource requirements, and suitability for specific use cases.
Describe Bubble Sort
Bubble Sort is a simple comparisonbased sorting algorithm that repeatedly compares adjacent elements and swaps them if they are in the wrong order. It gets its name from the way smaller elements “bubble” to the top (or larger elements sink to the bottom) of the list during each iteration.
Here’s the stepbystep procedure for the Bubble Sort algorithm:
 Start with an unsorted array of elements.
 Repeat the following steps until the array is fully sorted:
a. Compare adjacent elements in pairs, starting from the first pair.
b. If the elements are in the wrong order (e.g., the current element is greater than the next element in ascending order), swap them.
c. Move to the next pair of elements and repeat the comparison and swapping process.
d. Continue this process until you reach the end of the array. At this point, the largest element in the unsorted portion will “bubble” to the end.
 After completing one pass through the array, the largest element will be in its correct position at the end.
 Repeat steps 2 and 3 for the remaining unsorted portion of the array (excluding the last sorted element).
 Continue these iterations until the entire array is sorted, and no more swaps are required.
 The array is now sorted in ascending order.
Here’s how Bubble Sort works using numbers as an example:
Consider an array of numbers: [5, 2, 8, 1, 6]
 Start with the first element (5) and compare it with the next element (2). Since 5 is greater than 2, swap them:
 [2, 5, 8, 1, 6]
 Move to the next pair of elements: 5 and 8. Since they are in the correct order, no swap is needed:
 [2, 5, 8, 1, 6]
 Continue this process for the remaining elements, comparing and swapping as necessary:
 Compare 8 and 1. Since 8 is greater, swap them:
 [2, 5, 1, 8, 6]
 Compare 8 and 6. Since 8 is greater, swap them:
 [2, 5, 1, 6, 8]
 Compare 8 and 1. Since 8 is greater, swap them:
 At this point, the largest element (8) has “bubbled” to the end of the list. Repeat the process for the remaining unsorted portion of the array.
 Repeat steps 14 until the entire array is sorted. In each iteration, the largest unsorted element will “bubble” to the end.
 After the final iteration, the array is sorted in ascending order:
 [1, 2, 5, 6, 8]
Bubble Sort has a time complexity of O(n^2) in the worst and average case, where n is the number of elements in the array. It is not considered an efficient sorting algorithm for large datasets but can be useful for educational purposes or small arrays.
Write a C function/program to implement Bubble Sort
Here’s an implementation of bubble sort in the C programming language:
#include <stdio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
for (i = 0; i < n – 1; i++) {
for (j = 0; j < n – i – 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
printf(“Unsorted array: \n”);
for (int i = 0; i < n; i++)
printf(“%d “, arr[i]);
bubbleSort(arr, n);
printf(“\nSorted array: \n”);
for (int i = 0; i < n; i++)
printf(“%d “, arr[i]);
return 0;
}
This code sorts an array of integers in ascending order using the bubble sort algorithm. The bubbleSort function takes an array arr and its size n as inputs, and sorts the array inplace. The main function initialise an array of integers, calls the bubbleSort function to sort the array, and then prints the sorted array.
Bubble Sort with Optimization
Bubble Sort can be optimized to improve its efficiency by adding a flag to track if any swaps were made during a pass. If no swaps are made during a pass, it means the array is already sorted, and the algorithm can terminate early. This optimization reduces the number of unnecessary comparisons and swaps.
Here’s the procedure for the optimized Bubble Sort algorithm:
 Start with an unsorted array of elements.
 Set a flag to track if any swaps were made during a pass. Initially, set the flag to false.
 Repeat the following steps until the array is fully sorted or no swaps are made during a pass:
a. Reset the flag to false at the beginning of each pass.
b. Compare adjacent elements in pairs, starting from the first pair.
c. If the elements are in the wrong order, swap them and set the flag to true.
d. Move to the next pair of elements and repeat the comparison and swapping process.
e. Continue this process until you reach the end of the unsorted portion of the array.
 After completing one pass through the array, the largest element will be in its correct position at the end.
 If the flag remains false after a pass, it means no swaps were made, indicating that the array is already sorted. Terminate the algorithm.
 Repeat steps 35 for the remaining unsorted portion of the array (excluding the last sorted element).
 Continue these iterations until the entire array is sorted, and no more swaps are required.
 The array is now sorted in ascending order.
Here’s an optimized Bubble Sort implementation in C:
#include <stdio.h>
void bubbleSort(int arr[], int size) {
for (int i = 0; i < size – 1; i++) {
int swapped = 0; // Flag to track if any swaps were made
for (int j = 0; j < size – i – 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap the elements
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = 1; // Set the flag to true
}
}
// If no swaps were made during the pass, the array is already sorted
if (swapped == 0) {
break;
}
}
}
int main() {
int arr[] = {5, 2, 8, 1, 6};
int size = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, size);
printf(“Sorted array: “);
for (int i = 0; i < size; i++) {
printf(“%d “, arr[i]);
}
printf(“\n”);
return 0;
}
This optimized implementation of Bubble Sort terminates early if the array is already sorted. It checks the flag after each pass and breaks out of the outer loop if no swaps were made.
Recall the Complexity of Bubble Sort
Here’s a comparison of the time complexity of Bubble Sort with and without optimization in the best, average, and worst cases:
Without Optimization:
 Best Case Time Complexity: O(n) – The best case occurs when the input array is already sorted. In this case, Bubble Sort will still perform n1 comparisons, but no swaps are required.
 Average Case Time Complexity: O(n^2) – The average case occurs when the input array is partially sorted or randomly ordered. Bubble Sort will make approximately (n^2)/2 comparisons and swaps.
 Worst Case Time Complexity: O(n^2) – The worst case occurs when the input array is sorted in reverse order. Bubble Sort will make (n^2)/2 comparisons and swaps.
With Optimization:
 Best Case Time Complexity: O(n) – The best case occurs when the input array is already sorted. With the optimization, Bubble Sort will detect the sorted array after one pass and terminate early, resulting in linear time complexity.
 Average Case Time Complexity: O(n^2) – The average case occurs when the input array is partially sorted or randomly ordered. Although the optimization reduces the number of comparisons and swaps, Bubble Sort still performs (n^2)/2 operations on average.
 Worst Case Time Complexity: O(n^2) – The worst case occurs when the input array is sorted in reverse order. In this case, the optimization has minimal impact, and Bubble Sort still makes (n^2)/2 comparisons and swaps.
It’s important to note that the space complexity of Bubble Sort is constant O(1) as it only requires a few additional variables for swapping elements. The mentioned complexities assume a basic implementation of the Bubble Sort algorithm.
Bubble Sort is an inplace sorting algorithm. Inplace sorting algorithms modify the input array directly, without requiring any additional memory proportional to the size of the input.
Bubble Sort achieves inplace sorting by swapping adjacent elements within the array to gradually move larger elements towards the end. The swapping is done directly within the array without the need for creating a separate copy or allocating additional memory.
Since Bubble Sort operates inplace, it has a space complexity of O(1), making it memoryefficient and suitable for sorting arrays with limited memory resources.
Describe Selection Sort
Selection Sort is a simple comparisonbased sorting algorithm that divides the input array into two parts: the sorted portion and the unsorted portion. It repeatedly selects the smallest (or largest) element from the unsorted portion and swaps it with the element at the beginning of the unsorted portion.
This process continues until the entire array is sorted. Here’s the stepbystep procedure for Selection Sort:
 Start with an unsorted array of elements.
 Set the initial boundary of the sorted and unsorted portions of the array. The boundary is initially set to the first element, assuming it is the only element in the sorted portion.
 Repeat the following steps until the entire array is sorted:
a. Find the minimum (or maximum) element from the unsorted portion of the array.
b. Swap the found minimum (or maximum) element with the element at the boundary between the sorted and unsorted portions.
c. Move the boundary one position ahead to include the newly sorted element in the sorted portion.
 After each iteration, the smallest (or largest) element is placed at its correct position in the sorted portion.
 Repeat steps 3 and 4 until the entire array is sorted.
 The array is now sorted in ascending (or descending) order.
Let’s apply the Selection Sort procedure on a set of numbers to demonstrate how the algorithm works. Consider the following set of numbers:
9, 5, 2, 7, 1, 8
 Initially, the entire array is unsorted. The sorted portion is empty, and the unsorted portion includes all the numbers.
 Find the smallest element in the unsorted portion. In this case, the smallest element is 1.
 Swap the smallest element (1) with the element at the beginning of the unsorted portion (9).
Sorted: 1  Unsorted: 5, 2, 7, 9, 8  Move the boundary between the sorted and unsorted portions one position ahead.
Sorted: 1, 5  Unsorted: 2, 7, 9, 8  Repeat steps 24 until the entire array is sorted.
Sorted: 1, 2, 5  Unsorted: 7, 9, 8
Sorted: 1, 2, 5, 7  Unsorted: 9, 8
Sorted: 1, 2, 5, 7, 8  Unsorted: 9  The array is now fully sorted in ascending order: 1, 2, 5, 7, 8, 9.
This example demonstrates how the Selection Sort algorithm progressively selects the smallest element from the unsorted portion and moves it to its correct position in the sorted portion. The process continues until the entire array is sorted.
Write a C Function/Program to implement Selection Sort
Here’s a sample implementation of selection sort in C:
#include <stdio.h>
void selectionSort(int arr[], int size) {
for (int i = 0; i < size – 1; i++) {
int minIndex = i;
// Find the index of the minimum element in the unsorted portion
for (int j = i + 1; j < size; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the minimum element with the element at the boundary
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
int main() {
int arr[] = {5, 2, 8, 1, 6};
int size = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, size);
printf(“Sorted array: “);
for (int i = 0; i < size; i++) {
printf(“%d “, arr[i]);
}
printf(“\n”);
return 0;
}
Recall the Complexity of Selection Sort
Here’s a recap of the time complexity of Selection Sort in the best, average, and worst cases:
 Best Case Time Complexity: O(n^2) – The best case occurs when the input array is already sorted or nearly sorted. Even in this case, Selection Sort requires the same number of comparisons and swaps as in the average and worst cases. It needs to iterate through the entire array, finding the minimum element and swapping it with the first element of the unsorted portion, for each iteration.
 Average Case Time Complexity: O(n^2) – The average case occurs when the input array is randomly ordered. Selection Sort requires two nested loops: one to iterate through the array, and another to find the minimum element in the unsorted portion. This results in roughly (n^2)/2 comparisons and (n^2)/2 swaps on average.
 Worst Case Time Complexity: O(n^2) – The worst case occurs when the input array is sorted in reverse order. Selection Sort still requires the same number of comparisons and swaps as in the average case. It needs to iterate through the entire array, finding the minimum element and swapping it with the first element of the unsorted portion, for each iteration.
The space complexity of Selection Sort is O(1) since it performs inplace sorting, requiring only a constant amount of additional memory for temporary variables used during swapping.
It’s worth noting that while Selection Sort has a quadratic time complexity, it can be useful for small input sizes or when minimizing the number of swaps is a priority, as it performs fewer swaps compared to other sorting algorithms like Bubble Sort. However, for larger datasets, more efficient sorting algorithms like Merge Sort or Quick Sort are generally preferred.
Describe Insertion Sort
Insertion Sort is a simple comparisonbased sorting algorithm that builds the final sorted array one element at a time. It works by repeatedly inserting an element from the unsorted portion into its correct position in the sorted portion of the array.
Here’s the stepbystep procedure for Insertion Sort:
 Start with an unsorted array of elements.
 Assume the first element in the array is already sorted. The sorted portion initially consists of only the first element, and the unsorted portion includes the remaining elements.
 Iterate through the unsorted portion of the array, starting from the second element.
 For each iteration, compare the current element with the elements in the sorted portion, moving from right to left.
 If the current element is smaller (or larger, for descending order) than an element in the sorted portion, shift that element to the right to make space for the current element.
 Continue this shifting process until you find the correct position for the current element in the sorted portion.
 Insert the current element into its correct position in the sorted portion.
 Move to the next element in the unsorted portion and repeat steps 47.
 Repeat this process until the entire array is sorted.
 The array is now sorted in ascending (or descending) order.
Let’s walk through an example of applying Insertion Sort to a set of numbers to demonstrate how the algorithm works. Consider the following set of numbers:
7, 4, 2, 8, 1, 6
 Start with the first element (7) and consider it as the sorted portion.
Sorted: 7  Unsorted: 4, 2, 8, 1, 6  Take the second element (4) from the unsorted portion and compare it with the elements in the sorted portion.
Since 4 is smaller than 7, we shift 7 to the right.
Sorted: 4, 7  Unsorted: 2, 8, 1, 6  Take the next element (2) from the unsorted portion and compare it with the elements in the sorted portion.
Since 2 is smaller than 7 and 4, we shift both 4 and 7 to the right.
Sorted: 2, 4, 7  Unsorted: 8, 1, 6  Take the next element (8) from the unsorted portion and compare it with the elements in the sorted portion.
8 is greater than all the elements in the sorted portion, so it remains in place.
Sorted: 2, 4, 7, 8  Unsorted: 1, 6  Take the next element (1) from the unsorted portion and compare it with the elements in the sorted portion.
Since 1 is smaller than 8, 7, 4, and 2, we shift all these elements to the right.
Sorted: 1, 2, 4, 7, 8  Unsorted: 6  Take the last element (6) from the unsorted portion and compare it with the elements in the sorted portion.
6 is smaller than 8, so we shift 8 to the right.
Sorted: 1, 2, 4, 6, 7, 8  Unsorted: (empty)  The array is now fully sorted in ascending order: 1, 2, 4, 6, 7, 8.
This example demonstrates how Insertion Sort builds the sorted array by repeatedly inserting elements from the unsorted portion into their correct positions in the sorted portion. The process continues until the entire array is sorted.
Write a C Function/Program to implement Insertion Sort
Here’s a C implementation of Insertion Sort:
#include <stdio.h>
void insertionSort(int arr[], int size) {
for (int i = 1; i < size; i++) {
int key = arr[i];
int j = i – 1;
// Move elements of the sorted portion to the right if they are greater than the key
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j–;
}
// Insert the key into its correct position
arr[j + 1] = key;
}
}
int main() {
int arr[] = {5, 2, 8, 1, 6};
int size = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, size);
printf(“Sorted array: “);
for (int i = 0; i < size; i++) {
printf(“%d “, arr[i]);
}
printf(“\n”);
return 0;
}
Recall the Complexity of Insertion Sort
Here’s a recap of the time complexity of Insertion Sort in the best, average, and worst cases:
 Best Case Time Complexity: O(n) – The best case occurs when the input array is already sorted or nearly sorted. In this case, Insertion Sort has to make only one comparison per element, resulting in a linear time complexity.
 Average Case Time Complexity: O(n^2) – The average case occurs when the input array is randomly ordered. In this case, Insertion Sort requires comparing each element with the elements in the sorted portion, resulting in roughly (n^2)/2 comparisons and swaps on average.
 Worst Case Time Complexity: O(n^2) – The worst case occurs when the input array is sorted in reverse order. In this case, each element in the unsorted portion needs to be compared with all the elements in the sorted portion and potentially shifted to their correct positions. This results in roughly (n^2)/2 comparisons and (n^2)/2 swaps.
The space complexity of Insertion Sort is O(1) since it performs inplace sorting, requiring only a constant amount of additional memory for temporary variables used during swapping.
It’s worth noting that while Insertion Sort has a quadratic time complexity, it can be efficient for small input sizes or when the array is already partially sorted. It also has the advantage of being stable (preserving the relative order of equal elements) and easy to implement. However, for larger datasets, more efficient sorting algorithms like Merge Sort or Quick Sort are generally preferred.
Describe Quick Sort
Quick Sort is a popular divideandconquer sorting algorithm that efficiently sorts an array by partitioning it into smaller subarrays, sorting those subarrays independently, and then combining them to obtain the final sorted array.
Here’s the stepbystep procedure for Quick Sort:
 Choose a pivot element from the array. The pivot can be selected in various ways, such as picking the first, last, or middle element of the array.
 Rearrange the array such that all elements smaller than the pivot are placed before the pivot, and all elements greater than the pivot are placed after the pivot. This step is called partitioning.
 Recursively apply steps 1 and 2 to the subarrays formed by the partitioning. One subarray contains elements smaller than the pivot, and the other subarray contains elements greater than the pivot.
 Continue the recursion until the subarrays contain only one element or are empty.
 Finally, combine the sorted subarrays to obtain the fully sorted array.
Here’s a highlevel overview of the Quick Sort algorithm:
 Choose a pivot element from the array.
 Partition the array such that elements smaller than the pivot are placed before it, and elements greater than the pivot are placed after it. The pivot element is now in its final sorted position.
 Recursively apply Quick Sort to the subarrays formed by the partitioning. One subarray contains elements smaller than the pivot, and the other subarray contains elements greater than the pivot.
 Repeat steps 13 until the subarrays contain only one element or are empty.
 Combine the sorted subarrays to obtain the fully sorted array.
Let’s apply the Quick Sort algorithm on a set of numbers to demonstrate how it works. Consider the following set of numbers:
7, 4, 2, 8, 1, 6
 Choose a pivot element. In this example, let’s choose the last element as the pivot: 6.
 Partition the array around the pivot. Rearrange the elements such that all elements smaller than the pivot are placed before it, and all elements greater than the pivot are placed after it.
After the partitioning step, the array becomes:
4, 2, 1, 6, 8, 7
 Recursively apply Quick Sort to the subarrays formed by the partitioning.
Applying Quick Sort to the subarray before the pivot (4, 2, 1): Choose the last element (1) as the pivot.
Partition the subarray:

 1, 2, 4
 The subarray is already sorted.
Applying Quick Sort to the subarray after the pivot (8, 7):

 Choose the first element (8) as the pivot.
Partition the subarray:

 7, 8
 The subarray is already sorted.
 Combine the sorted subarrays to obtain the fully sorted array.
The sorted array is: 1, 2, 4, 6, 7, 8
This example demonstrates how Quick Sort divides the array into smaller subarrays, based on the chosen pivot, and sorts them independently. The process continues recursively until the subarrays contain only one element or are empty. Finally, the sorted subarrays are combined to obtain the fully sorted array.
Write a C Function/Program to implement Quick Sort
Here is an implementation of the Quick Sort algorithm in C:
#include <stdio.h>
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
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], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return i + 1;
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);
quickSort(arr, low, pivotIndex – 1);
quickSort(arr, pivotIndex + 1, high);
}
}
int main() {
int arr[] = {5, 2, 8, 1, 6};
int size = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, size – 1);
printf(“Sorted array: “);
for (int i = 0; i < size; i++) {
printf(“%d “, arr[i]);
}
printf(“\n”);
return 0;
}
Recall the Complexity of Quick Sort
Here’s a recap of the time complexity of Quick Sort in the best, average, and worst cases:
 Best Case Time Complexity: O(n log n) – The best case occurs when the pivot is chosen such that it partitions the array into two equalsized subarrays. In this case, the array is divided evenly at each level of recursion, resulting in a balanced partitioning. As a result, Quick Sort exhibits optimal performance with a time complexity of O(n log n).
 Average Case Time Complexity: O(n log n) – The average case occurs when the pivot is chosen randomly or using a medianofthree approach. Quick Sort typically exhibits good averagecase performance with a time complexity of O(n log n). Although partitioning is not perfectly balanced, the overall time complexity remains O(n log n) due to the logarithmic nature of the recursion.
 Worst Case Time Complexity: O(n^2) – The worst case occurs when the pivot is consistently chosen as the smallest or largest element, resulting in highly imbalanced partitioning. This happens, for example, when the array is already sorted in ascending or descending order. In the worst case, Quick Sort degrades to a quadratic time complexity of O(n^2), as each partitioning step only reduces the size of the subarray by one element.
However, it’s important to note that the worstcase scenario is relatively rare in practice when using randomized or medianofthree pivot selection strategies. These strategies significantly reduce the chances of encountering the worst case and, on average, Quick Sort performs efficiently with a time complexity of O(n log n).
The space complexity of Quick Sort is O(log n) due to the recursive nature of the algorithm. Each recursive call requires a certain amount of auxiliary space on the call stack. However, the space complexity is generally considered negligible compared to the time complexity.
Describe Merge Sort
Merge Sort is a popular comparisonbased sorting algorithm that follows the divideandconquer approach to efficiently sort an array. It works by repeatedly dividing the array into two halves, sorting those halves recursively, and then merging them to obtain the final sorted array.
Here’s the stepbystep procedure for Merge Sort:
 Divide the unsorted array into two halves by finding the middle index.
 Recursively apply Merge Sort to the first half of the array.
 Recursively apply Merge Sort to the second half of the array.
 Merge the two sorted halves to obtain a single sorted array.
Here’s a highlevel overview of the Merge Sort algorithm:
 Divide: Split the unsorted array into two equalsized (or approximately equalsized) subarrays by finding the middle index.
 Recursively Sort: Apply Merge Sort to the first half of the array and the second half of the array. This step is performed by recursively calling the Merge Sort function on the subarrays.
 Merge: Merge the sorted subarrays into a single sorted array. This step involves comparing the elements from the two subarrays and placing them in the correct order.
 Repeat: Repeat steps 13 until the subarrays contain only one element or are empty.
 The merged subarrays form the final sorted array.
Let’s apply the Merge Sort algorithm on a set of numbers to demonstrate how it works. Consider the following set of numbers:
7, 4, 2, 8, 1, 6
 Divide the array into two halves:
Left half: 7, 4, 2
Right half: 8, 1, 6
 Recursively apply Merge Sort to the left and right halves.
Left half: 7, 4, 2
Divide the left half further:
Left subarray: 7
Right subarray: 4, 2
Recursively sort the left subarray (7) and the right subarray (4, 2).
Right half: 8, 1, 6
Divide the right half further:
Left subarray: 8
Right subarray: 1, 6
Recursively sort the left subarray (8) and the right subarray (1, 6).
 Merge the sorted subarrays.
Merging the sorted subarrays for the left half: Merge the left subarray (7) and the right subarray (4, 2).
 The merged subarray becomes: 2, 4, 7.
Merging the sorted subarrays for the right half:

 Merge the left subarray (8) and the right subarray (1, 6).
 The merged subarray becomes: 1, 6, 8.
 Merge the two sorted halves.
Merging the left half (2, 4, 7) and the right half (1, 6, 8): Compare the elements from the two halves and place them in the correct order.
 The merged subarray becomes: 1, 2, 4, 6, 7, 8.
The final sorted array is: 1, 2, 4, 6, 7, 8.
This example demonstrates how Merge Sort recursively divides the array into smaller halves, sorts them independently, and then merges them to obtain the final sorted array. The merging step involves comparing the elements from the two halves and placing them in the correct order. By repeatedly dividing and merging the array, Merge Sort efficiently sorts the entire array in ascending order.
Write a C function/Program to implement Merge Sort
Here’s a C implementation of the Merge Sort algorithm:
#include <stdio.h>
void merge(int arr[], int left[], int leftSize, int right[], int rightSize) {
int i = 0; // Index for left subarray
int j = 0; // Index for right subarray
int k = 0; // Index for merged array
while (i < leftSize && j < rightSize) {
if (left[i] <= right[j]) {
arr[k++] = left[i++];
} else {
arr[k++] = right[j++];
}
}
// Copy the remaining elements of left subarray, if any
while (i < leftSize) {
arr[k++] = left[i++];
}
// Copy the remaining elements of right subarray, if any
while (j < rightSize) {
arr[k++] = right[j++];
}
}
void mergeSort(int arr[], int size) {
if (size <= 1) {
return; // Base case: array is already sorted or empty
}
int mid = size / 2;
// Divide the array into two halves
int left[mid];
int right[size – mid];
// Copy elements to left and right subarrays
for (int i = 0; i < mid; i++) {
left[i] = arr[i];
}
for (int i = mid; i < size; i++) {
right[i – mid] = arr[i];
}
// Recursively sort the left and right subarrays
mergeSort(left, mid);
mergeSort(right, size – mid);
// Merge the sorted subarrays
merge(arr, left, mid, right, size – mid);
}
int main() {
int arr[] = {5, 2, 8, 1, 6};
int size = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, size);
printf(“Sorted array: “);
for (int i = 0; i < size; i++) {
printf(“%d “, arr[i]);
}
printf(“\n”);
return 0;
}
Recall the Complexity of Merge Sort
Here’s a recap of the time complexity of Merge Sort in the best, average, and worst cases:
 Best Case Time Complexity: O(n log n) – The best case occurs when the array is already sorted or nearly sorted. In this case, the Merge Sort algorithm still performs the dividing and merging steps, but it does not need to perform many comparisons during the merging step. As a result, Merge Sort exhibits optimal performance with a time complexity of O(n log n).
 Average Case Time Complexity: O(n log n) – The average case occurs when the input array is randomly shuffled or has no particular order. Merge Sort’s time complexity is O(n log n) in the average case due to its divideandconquer nature. It consistently divides the array into two halves and merges them, with a logarithmic number of recursive steps.
 Worst Case Time Complexity: O(n log n) – The worst case occurs when the input array is in reverse order. In this case, each level of recursion requires merging two subarrays of roughly equal size. The worstcase time complexity of Merge Sort is still O(n log n) because the array is repeatedly divided into smaller halves and merged until the subarrays contain only one element.
The space complexity of Merge Sort is O(n) because it requires additional space to store the merged subarrays during the merging step. This additional space is proportional to the size of the input array. However, Merge Sort’s space complexity can be reduced to O(1) if the merging is performed inplace, although this would require additional complexity in the implementation.
Merge Sort’s consistent time complexity makes it a reliable choice for sorting large datasets. Its performance remains efficient in various scenarios, which contributes to its popularity in practice.
Recall the procedure to install Eclipse C C++ Development Tool (CDT) 8.1.2 Eclipse 4.2.2 on windows
To install the Eclipse C/C++ Development Tool (CDT) 8.1.2 on Windows, you can follow these steps:
 Download Eclipse:
 Visit the Eclipse website (https://www.eclipse.org/downloads/) in your web browser.
 Click on “Download Packages” or a similar button to access the download page.
 Scroll down and find the Eclipse version 4.2.2 (Juno) release.
 Download the appropriate package for your Windows version (32bit or 64bit) by clicking on the corresponding link.
 Extract Eclipse:
 Locate the downloaded Eclipse package (e.g., “eclipsecppjunoSR2win32.zip” for 32bit or “eclipsecppjunoSR2win32x86_64.zip” for 64bit).
 Extract the contents of the downloaded ZIP file to a directory of your choice on your Windows system (e.g., “C:\eclipse”).
 Launch Eclipse:
 Navigate to the directory where you extracted Eclipse.
 Inside the directory, locate the “eclipse.exe” file and doubleclick on it to launch Eclipse.
 Install CDT:
 Once Eclipse is launched, go to “Help” in the menu bar and select “Eclipse Marketplace”.
 In the Eclipse Marketplace window, search for “CDT” or “C/C++ Development Tool”.
 From the search results, select “C/C++ Development Tooling” and click on “Go to the Marketplace” button.
 On the C/C++ Development Tooling page, click on the “Install” button to begin the installation process.
 Follow the onscreen instructions to complete the installation.
 Accept any necessary licenses and confirm the installation when prompted.
 Restart Eclipse:
 After the installation is complete, you will be prompted to restart Eclipse. Close Eclipse and reopen it to apply the changes.
Once you have completed these steps, Eclipse with the CDT plugin will be installed on your Windows system. You can now use Eclipse for C/C++ development by creating new projects or importing existing ones into the workspace.
Write and execute your First C/C++ Program in Eclipse
To write and execute your first C/C++ program in Eclipse, you can follow these steps:
 Launch Eclipse:
 Navigate to the directory where you extracted Eclipse.
 Inside the directory, locate the “eclipse.exe” file and doubleclick on it to launch Eclipse.
 Create a new project:
 In the Eclipse welcome screen, click on “Create a new C/C++ Project” or go to “File” > “New” > “C/C++ Project”.
 Select “C Project” or “C++ Project” depending on whether you want to write a C or C++ program.
 Click “Next”.
 Configure project settings:
 Enter a project name of your choice.
 Choose the project type (Executable or Library).
 Select the toolchain you want to use (e.g., “GNU Autotools”, “MinGW GCC”, “Microsoft Visual C++”, etc.).
 Configure any additional project settings as desired.
 Click “Finish”.
 Create a new source file:
 Rightclick on the project in the “Project Explorer” view (left side of the Eclipse window).
 Select “New” > “Source File” or “New” > “C++ Class” if you prefer to create a class file.
 Enter a name for your source file (e.g., “main.c” or “main.cpp”).
 Click “Finish”.
 Write your program:
 In the newly created source file, write your C/C++ program. For example, you can start with a simple “Hello, World!” program:
#include <stdio.h>
int main()
{
printf(“Hello, World!\n”);
return 0;
}
 Save your program:
 Save the source file by pressing Ctrl + S or going to “File” > “Save” in the Eclipse menu.
 Build and execute the program:
 Rightclick on the project in the “Project Explorer” view.
 Select “Build Project” to compile your program.
 Once the build is successful, rightclick on the source file or project.
 Choose “Run As” > “Local C/C++ Application” or “Local C/C++ Application” depending on your project type.
 The program will execute, and you should see the output in the “Console” view at the bottom of the Eclipse window.
That’s it! You have successfully written and executed your first C/C++ program in Eclipse. You can continue to explore more features of Eclipse for C/C++ development and build more complex programs using the same process.
Describe Radix Sort
Radix Sort is a noncomparative sorting algorithm that sorts elements based on their digits or characters. It works by distributing the elements into buckets or queues based on the value of a specific digit or character position. Radix Sort operates on the principle of least significant digit (LSD) or most significant digit (MSD) depending on the variant used.
Here’s a stepbystep description of the Radix Sort algorithm:
 Determine the maximum number of digits or characters in the input elements. This will determine the number of passes required for sorting.
 Start with the least significant digit (LSD) or the most significant digit (MSD) depending on the chosen variant.
 Distribute the input elements into 10 buckets (0 to 9) or as many buckets as there are possible values for the digit or character being considered.
 Reorder the elements by collecting them from the buckets in the order they were distributed. Preserve the relative order of elements within the same bucket.
 Repeat steps 34 for each subsequent digit or character position until all digits or characters have been considered.
 The final collection of elements will be sorted in ascending or descending order based on the chosen radix and the order of digit or character positions.
Radix Sort is often used for sorting elements that have a fixedlength representation, such as integers or strings of fixed length. It has linear time complexity, making it efficient for large datasets. However, the number of passes required depends on the maximum number of digits or characters, which can impact performance for very large numbers or long strings.
It’s worth noting that Radix Sort is stable, meaning it preserves the relative order of elements with equal values. This property makes it useful in scenarios where the stability of sorting is important.
Overall, Radix Sort provides an effective way to sort elements by their digits or characters, leveraging the distribution and collection process to achieve the desired order.
Let’s apply the Radix Sort algorithm to sort a set of numbers in ascending order. Consider the following set of numbers:
83, 47, 312, 999, 168, 525
 Determine the maximum number of digits in the input elements, which in this case is 3 digits.
 Start with the least significant digit (LSD).
 Distribute the numbers into 10 buckets based on the value of the LSD:
Bucket 0: –
Bucket 1: 312
Bucket 2: 83
Bucket 3: –
Bucket 4: 47
Bucket 5: 525
Bucket 6: 168
Bucket 7: –
Bucket 8: –
Bucket 9: 999
 Reorder the numbers by collecting them from the buckets in the order they were distributed:
Sorted numbers after the first pass: 312, 83, 47, 525, 168, 999  Move to the next significant digit (tens place):
Distribute the numbers into buckets based on the value of the tens digit:
Bucket 0: 312, 83, 47
Bucket 1: 168
Bucket 2: 525
Bucket 3: –
Bucket 4: –
Bucket 5: –
Bucket 6: –
Bucket 7: –
Bucket 8: –
Bucket 9: 999
 Reorder the numbers by collecting them from the buckets:
Sorted numbers after the second pass: 312, 83, 47, 168, 525, 999  Move to the most significant digit (hundreds place):
Distribute the numbers into buckets based on the value of the hundreds digit:
Bucket 0: 47, 83, 168
Bucket 1: –
Bucket 2: 312
Bucket 3: –
Bucket 4: –
Bucket 5: 525
Bucket 6: –
Bucket 7: –
Bucket 8: –
Bucket 9: 999
 Reorder the numbers by collecting them from the buckets:
Sorted numbers after the third pass: 47, 83, 168, 312, 525, 999  Since there are no more digits to consider, the numbers are sorted in ascending order.
The final sorted array is: 47, 83, 168, 312, 525, 999.
This example demonstrates how the Radix Sort algorithm distributes and collects the numbers based on their digits. By repeatedly going through each digit position, the algorithm sorts the numbers in the desired order.
Write an Algorithm for Radix Sort
Here’s a basic algorithm for Radix Sort:
 Find the maximum element in the input array to determine the maximum number of digits.
 Initialize a digit variable to 1 (representing the least significant digit).
 Create 10 buckets (0 to 9) to hold the elements.
 Repeat the following steps until the maximum number of digits have been processed:
 Distribute the elements into the buckets based on the value of the current digit:
 Extract the digit from each element by performing modulus division by 10^(digit1).
 Place the element into the corresponding bucket based on the extracted digit.
 Collect the elements from the buckets in the original order and overwrite the input array:
 Start with bucket 0 and append the elements to the input array.
 Move to bucket 1 and append its elements, then bucket 2, and so on, until bucket 9.
 Ensure to maintain the relative order of elements within the same bucket.
 Increment the digit variable to move to the next significant digit.
 Distribute the elements into the buckets based on the value of the current digit:
 After completing the loop, the input array will be sorted.
Here is the pseudocode representation of the algorithm:
RadixSort(arr):
maxNum = FindMaximum(arr)
digit = 1
buckets = Array of 10 empty lists
while digit <= maxNum:
for element in arr:
digitValue = (element / (10^(digit1))) % 10
buckets[digitValue].append(element)
arrIndex = 0
for i in range(10):
for element in buckets[i]:
arr[arrIndex] = element
arrIndex += 1
Clear all buckets
digit += 1
return arr
Note: The FindMaximum function is used to find the maximum element in the input array, which is needed to determine the maximum number of digits.
This algorithm outlines the key steps involved in Radix Sort, including the distribution of elements into buckets based on digits and the collection of elements to form the sorted array.
Recall the Complexity of Radix Sort
The time complexity of Radix Sort is O(d * (n + k)), where ‘n’ is the number of elements to be sorted, ‘d’ is the number of digits or characters in the largest element, and ‘k’ is the range of possible values for each digit or character position.
In the best case, average case, and worst case scenarios, the time complexity remains the same. Since each pass distributes the elements into buckets and collects them back, the time complexity is linear with respect to the number of elements (n) and the number of digits (d).
The space complexity of Radix Sort is O(n + k), where ‘n’ is the number of elements and ‘k’ is the range of possible values for each digit or character position. This space complexity is required to store the buckets and temporary arrays used during the sorting process.