C Tutorial

C Tutorial C Language Environment Setup Execution flow of C program C printf and Scanf C Data type C Token Variable in C Operators in C Comments in C Escape Sequence in C C – Storage Classes C Decision control statement Loop Statement in C Break, continue and goto statement in C Type Casting in C Function in C Recursion in C String in C C Array Pointer in C Dynamic memory allocation C –Structure Nested Structure in C Union in C File Handling in C C pre-processor Static Function In C Sizeof In C Selection Sort In C Scope Of Variables In C Runtime Vs Compile Time In C Random Access Lseek In C Queue Implementation In C Pseudo Code In C Prototype In C Pointer To Pointer In C Pointer Arithmetic In C Passing Array To Function In C Null Character In C Merge Sort In C Macros In C Library Functions In C Memory Leak In C Int In C Goto And Labels In C Fibonacci Series In C Fflush In C Derived Data Types In C Data Types In C Const Vs Volatile In C Character Set In C Character Class Tests In C Calloc In C C Pointers Arrays In C Include In C Clrscr In C C Vs Java String Literals In C Types Of Pointers In C Variables In C Volatile In C Why C Is A Middle Level Language Infix To Postfix Program In C Ceil function in C LCM of two numbers in C Quick sort in C Static in C function pointer as argument in C Top Array Keywords in C Add two numbers using the function in C Armstrong program in C using function Array, Declaring Arrays and Array Initialization Limitations of Inline Function in C Merge and Merge sort with example in C Do-While Loop in C For Loop in C While-Loop in C Difference between while and do-while loop in C Array Of Structures in C Data Structures And Algorithms in C Types Of Structures In C How to Avoid Structure Padding in C Use of Structure in C Do WHILE LOOP in C Programming Examples For Loop in C Programming Examples Entry Control Loop in C Exit control loop in C Infinite loop in C Nested loop in C pow() function in C String Handling functions in C Prime Number code in C Factorial Program in C using For Loop Factorial Program in C Using While Loop Fibonacci Series in C Using For Loop Fibonacci series in C using while loop Prime Number Program in C using for Loop While Loop in C programming examples Built-in functions in C Assert() Function C vs Java Strings Call Back Function in Embedded C Else If Ladder fgets() function Ftell() Function getc() function getch() function gets() function Heap Sort Nested if-else statement Pi() Function Positioning of file Write() function abs() function in C Attributes in C C program to find factorial of a number using Recursion Ferror() in c fopen() function in C Fibonacci series program in C using Recursion Formatted Input and output function in C Snake Game in C User Defined Functions in C Beep() function in C Cbrt() function in C Hook() function in C Isalnum() function in C C Program to find the Roots of a Quadratic Equation C Switch Statements Difference between rand() and srand() function in C Difference between while and for loop in C Doubly Linked list in C Example of Iteration in C How to use atoi() function in C How to use floor() function in C How to use sine() function in C How to use Typedef Struct in C Integer Promotions in C C Program Swap Numbers in cyclic order Using Call by Reference C Program to Find Largest Number Using Dynamic Memory Allocation C Program to Find the Largest Number using Ternary Operator C/C++ Program to Find the Size of int, float, double and char Find the Largest Three Distinct Elements in an Array using C/C++ Loop Questions in C Modulus on Negative Numbers in C Multiplication table program in C using For loop Nested Loops in C Programming Examples C Program for Mean and Median of an Unsorted Array Results of Comparison Operations in C and C++ Reverse a Stack using Recursion in C Simple hash() function in C strcat() Function in C Sum of N numbers in C using For loop Use of free() function in C Write a program that produces different results in C and C++ C Function Argument and Return Values Keywords in C Bank management system in C Calendar application in C Floor() Function in C Free() Function in C How to delete a file in C How to move a text in C Remove an element from an array in C Unformatted input() and output() function in C What are linker and loader in C SJF Scheduling Program in C Socket Programming in C Structure in C Tower of Hanoi in C Union Program in C Variable Declaration in C What is Linked List in C While Loop Syntax in C fork() in C GCD program in C Branching Statements in C Comma Operator in C Control statement in C Double Specifier in C How to create a binary file in C Long int in C Palindrome Number in C Pure Virtual Function in C Run Time Polymorphism in C Types of Array in C Types of Function in C What is a buffer in C What is required in each C Program Associativity of Operators in C Bit Stuffing Program in C Actual and Formal Parameters Addition of two Numbers in C Advantages of function in C Arithmetic Progression Program in C Binomial Coefficient Program in C Difference between Array and List in C Diffie-Hellman Algorithm in C How to convert a number to words in C How to convert a string to hexadecimal in C Difference between If and Switch Statement in C C and C++ Binary Files C program that does not Suspend when Ctrl+Z is Pressed Different ways to Declare the Variable as Constant in C Range of Int in C C Program to find the size of a File FIFO Example in the C Language For loop in C Programming GCD program of two numbers in C GPA Calculator in C How to Calculate Time Complexity in C How to include graphics.h in C How to measure time taken by a function in C How to return a Pointer from a Function in C What is the main in C Addition of Matrix in C Booleans in C C Program for Extended Euclidean algorithms C Program of Fencing the Ground Ceil and Floor in C Compound Interest Program in C Displaying Array in C Distance Vector Routing Protocol Program in c Dos.h Header File in C Language DSA Program in C Explain the two-way selection in C Fee Management System in C File Operations in C Malloc function in C Multiplication Table in C Simple Programs in C Language tolower() Function in C Type Conversion in the C Why does sizeof(x++) not Increment x in C Advantages of Dynamic Memory Allocation in C Armstrong Number in C Assignment Operator Program in C Banker’s Algorithm in C Binary Search in C with Best and Worst Time Complexity Caesar Cipher Program in C Call by Value and Call by Reference in C Conditional Operator in C CRC Program in C Deadlock Detection Program in C Decimal to Binary in C Difference between If Else and Nested If Else in C Difference between Pre-increment and Post-increment in C Difference between Scope and Lifetime in C Evaluation of Arithmetic Expression in C Explain the Increment and Decrement Operators in C Fseek Function in C Functions in C How to Find Square Free Numbers in C Length of an Array Function in C OpenGL in C Projects on C language in 2023 Purpose of a Function Prototype in C Stdio.h in C Two-Dimensional array in C What is String Comparison in C C Compilers for Windows Functions and Recursion in C How to Declare Boolean in C How to Declare Character in C How to Round up a number in C How to use strlen() in C Pointer Declaration in C Algorithm for String Palindrome in C C Program to find ASCII value of a character Constant Pointer in C How to find string length in C using strlen() function Implicit and Explicit in C Indirect Recursion in C Input and Output functions in C isupper() in C Jump Statement in C Lifetime of a Variable in C Linker Error in C Language Numeric Constant in C Size of Pointer in C Square Root in C Language Static and Dynamic Memory allocation String Declaration in C Strong Number in C Symmetric Matrix in C Types of C Tokens What is a Size of Pointer in C What is Increment and Decrement Operator in C 1 2 3 4 Series Program in C Advantages and Disadvantages of C Language C Program for Polynomial Addition C Program to Count the Number of Vowels in a String C Programming Errors and Solutions Compilation Errors in C Complex C Programs Difference between Argument and Parameter in C Difference between char s[] and char *s in C Evaluation of Postfix Expression Using Stack in C Find Leftmost and Rightmost Set Bit of a Number fprintf and fscanf in C Introduction to Dynamic Array in C Print Address in C Realloc function in C Ternary Operators in C Types of Tokens in C with Examples Difference between Static and Dynamic Memory Allocation in C Addition Program in C Array Definition in C Array of Pointers in C Arrow Operator in C Average of Two Numbers in C Binary to Decimal in C Binary to Octal in C BREAK STATEMENT in C C Programming Operators Questions C Programs Asked in Interview Calculator Program in C C Program to Read and Print an Employee's Detail Using Structure Bubble Sort Algorithm in C C Program to Find Area and Perimeter of Circle C Program to Check Whether a Given Number is Even or Odd C in Roman Numerals C Program to Make a Simple Calculator Using Switch Case Insertion Sort Program in C How to take input in string in C GCC Conflicting Types in C Function Definition in C Format Specifier for Hexadecimal in C Flowchart in C Float in C Fizzbuzz Implementation in C Conditional Statement in C Conio.h functions list in C Constants in C Dynamic Array in C Decision Making Statements in C Continue Statement in C Creation of Thread in C DFS Algorithm in C Difference between parameter and arguments in C Dijkstra's Algorithm in C Leap Year Program in C Jump Statements in C Modulus Operator in C Memory Allocation in C Simple Interest Program in C Reverse Array in C Recursive Function in C Queue in C Printing Pascal’s Triangle in C Preprocessor Directives in C Perror() in C Perfect Number in C Programming Language Parameter Passing Techniques in C Pascal Triangle in C Patterm Program in C Affine cipher in C Dereferencing pointer in C Internal static variable vs External static variables in C Difference between exit(0) and exit(1) in C Booth's Algorithm in C Condition Control Statements in C Double Specifier in C Dynamic variables in C How to print alphabets in C How to print char array in c Order of Evaluation in C Order of Operations in C Semantic Error in C Size of String Variable in C SJF PREEMPTIVE SCHEDULING PROGRAM C: Tree in C Arithmetic Progression Program in C Array, Declaring Arrays, and Array Initialization ARRAYS IN C Assert() Function in c Atoi in C Bar3d() function in C Graphics Beep function in c Bigint (BIG INTEGERS) in C with Example Builtin functions of GCC compiler Fibonacci series in C Priority Queue in C 2D ARRAY IN C 7 Best IDEs for C/C++ Developers in 2022 Addition of Two Numbers in C Advantages and Disadvantages of C Language Advantages of Function in C Algorithm for String Palindrome in C What is fgets in C language 2d Shearing Program in C Recursion Questions In C Static Identifier in C Inserting elements in an array using C Language complex.h Header File in C fsetpos()(set file position) in C Comparator function of qsort() in C Print Hex in C strnset() function in C %d in C Butterfly Star pattern in C C form format in Excel Scan Line Polygon Filling in C 8 Queens Problem in C Back Trace in C BSS (Block Started by Symbol) Data Segment in C language Class C in Networking Strings in C Programming What is Circular linked list? What is Loop in C BITWISE OPERATOR IN C LANGUAGE BOMB DIFFUSION PROGRAM IN C LANGUAGE

Comparator function of qsort() in C

In this article, you will learn about the comparator function ofthe qsort() function in C++ with different methods.

Introduction:

The qsort function in C is a standard library function used for sorting arrays. It takes four parameters: a pointer to the base of the array, the number of elements in the array, the size of each element, and a comparison function that is used to determine the order of elements.

Syntax:

It has the following syntax:

int compare(const void *a, const void *b);

The compare function takes two parameters, a and b, which are pointers to the elements being compared. The function should return an integer less than, equal to, or greater than zero if a is considered to be less than, equal to, or greater than b, respectively.

Program:

Let's take an example to illustrate the use of qsort() in C++:

#include <stdio.h>

#include <stdlib.h>

// Comparison function for qsort

int compare(const void *a, const void *b) {

return (*(int*)a - *(int*)b);

}

int main() {

// An array of integers

int arr[] = {5, 2, 8, 1, 6};

// Calculate the number of elements in the array

int n = sizeof(arr) / sizeof(arr[0]);

// Use sort to sort the array in ascending order

qsort(arr, n, sizeof(int), compare);

// Print the sorted array

printf("Sorted array: ");

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

printf("%d ", arr[i]);

}

return 0;

}

Output:

Sorted array: 1 2 5 6 8

Explanation:

Header Files:

  • #include <stdio.h>: It includes the standard input/output header file for basic I/O operations.
  • #include <stdlib.h>: It includes the standard library header file for general utility functions, including memory allocation.

Comparison Function (compar):

  • The compare function is a callback function used by the sort function for comparing elements during the sorting process.
  • It takes two pointers (a and b) to elements and returns an integer.
  • The compare function compares the values pointed to by a and b.
  • The result of the comparison determines the order of elements:
  • If the result is negative, a is considered less than b.
  • If the result is zero, a is considered equal to b.
  • If the result is positive, a is considered greater than b.

Main Function:

  • An array of integers, arr, is declared and initialized.
  • The variable n is calculated to determine the number of elements in the array.
  • The qsort function is used to sort the array in ascending order:
  • It takes the array, the number of elements, the size of each element, and the comparison function (compare) as arguments.
  • After that, the sorted array is printed using a simple loop.
  • The program concludes by returning 0 from the main Function.

Execution Result:

  • When the program is executed, the output will display the sorted array in ascending order.

Complexity analysis:

Time Complexity:

  • The time complexity of the provided code is dominated by the sorting operation performed by the sort function. The time complexity of qsort is generally O(n log n) on average, where "n" is the number of elements in the array.
  • The comparison function (compare) is called multiple times during the sorting process, contributing to the overall time complexity. The exact number of comparisons depends on the sorting algorithm used by qsort.
  • In the worst case, qsort could degrade to O(n^2) time complexity if a poor choice of pivots leads to inefficient partitioning.

Space Complexity:

  • The space complexity of the code is primarily determined by the space required for the array of integers. Additionally, this sort of function may use additional space for its internal operations, depending on the implementation.
  • The space complexity of the provided code is O(n), where "n" is the number of elements in the array. It is because the array requires O(n) space, and the space used by the qsort Function is typically O(log n) or O(n) in the worst case.

Method 1: Using Quicksort:

Divide and Conquer:

Quicksort is a divide-and-conquer algorithm. It divides the array into sub-arrays, recursively sorts these sub-arrays, and then combines them to obtain a sorted array.

The key step is the partitioning of the array, where a pivot element is chosen, and the array is rearranged such that elements smaller than the pivot are on its left, and elements greater than the pivot are on its right.

Partitioning:

The partitioning process is a critical step in Quicksort. It selects a pivot (often the last element in the array) and rearranges the elements such that elements smaller than the pivot come before it, and elements greater than the pivot come after it.

After that, the pivot is placed in its final sorted position.

Pivot Choice:

The choice of the pivot can affect the performance of Quicksort. In the code provided, the last element is chosen as the pivot. Other strategies include choosing the first element, a random element, or selecting the median of three elements to

Program:

Let's take an example to illustrate the use of comparator of qsort() using quicksort in C++:

#include <stdio.h>

//Function to partition the array and return the index of the pivot element

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] (pivot)

int temp = arr[i + 1];

arr[i + 1] = arr[high];

arr[high] = temp;

return (i + 1);

}

// Quicksort function

void quicksort(int arr[], int low, int high) {

if (low < high) {

// Find the partitioning index

int pivotIndex = partition(arr, low, high);

// Recursively sort the sub-arrays

quicksort(arr, low, pivotIndex - 1);

quicksort(arr, pivotIndex + 1, high);

}

}

// Main Function to demonstrate Quicksort

int main() {

int arr[] = {5, 2, 8, 1, 6};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");

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

printf("%d ", arr[i]);

}

// Apply Quicksort

quicksort(arr, 0, n - 1);

printf("\nSorted array: ");

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

printf("%d ", arr[i]);

}

return 0;

}

Output:

Original array: 5 2 8 1 6

Sorted array: 1 2 5 6 8

Explanation:

Partition Function:

  • Partition function takes an array (arr), a low index (low), and a high index (high) as parameters.
  • It selects the last element (arr[high]) as the pivot.
  • It uses a loop to iterate through the array, placing elements smaller than the pivot to its left and elements greater than the pivot to its right.
  • The partition function returns the index of the pivot after the partitioning.

Quicksort Function:

  • Quicksort function takes an array (arr), a low index (low), and a high index (high) as parameters.
  • It checks if the array has more than one element to sort (low < high).
  • If so, it finds the partitioning index using the partition function and recursively applies quicksort to the left and right sub-arrays.

Main Function:

  • It declares an array of integers (arr) and its size n.
  • It prints the original array.
  • It calls the quicksort function to sort the array.
  • It prints the sorted array.

Complexity analysis:

Time Complexity:

Average Case: O(n log n)

Quicksort has an average-case time complexity of O(n log n), which makes it very efficient for large datasets. This average-case performance is achieved when the pivot choices are reasonably balanced, leading to roughly equal partitioning of the array.

Worst Case: O(n^2)

The worst-case time complexity of Quicksort is O(n^2). It occurs when the pivot choice consistently results in highly unbalanced partitions, such as always selecting the smallest or largest element as the pivot. Various techniques, including randomizing the pivot selection or using the median-of-three strategy, can help mitigate the likelihood of worst-case behavior.

Space Complexity:

Average Case: O(log n)

The average-case space complexity of Quicksort is O(log n) due to the recursive nature of the algorithm. The space required is proportional to the maximum depth of the recursive call stack.

Worst Case: O(n)

In the worst-case scenario, the space complexity of Quicksort can degrade to O(n), especially when the recursion depth becomes maximal. Each recursive call consumes space on the call stack, and in the worst case, it might lead to a stack size proportional to the size of the input array.

Method 2: Using Mergesort

Divide and Conquer:

Mergesort is a divide-and-conquer algorithm that breaks down the problem of sorting an array into smaller sub-problems. After that, it solves each sub-problem independently and combines the solutions to obtain the final sorted array.

Divide Step:

The array is recursively divided into two halves until each sub-array contains only one element. It is the base case for the recursion.

Conquer Step:

The conquer step involves sorting the individual sub-arrays. It is inherently sorted because each sub-array contains only one element at the base case.

Merge Step:

The merge step is the key operation in Mergesort. It involves merging two sorted sub-arrays to create a larger sorted array. This process is repeated until the entire array is merged.

Stable Sorting:

Mergesort is a stable sorting algorithm, meaning that it maintains the relative order of equal elements in the sorted output as they were in the input.

Program:

Let's take an example to illustrate the use of the mergesort to sort an array in C++:

#include <stdio.h>

#include <stdlib.h>

// Merge two sub-arrays of arr[]

// First sub-array is arr[l..m]

// Second sub-array is arr[m+1..r]

void merge(int arr[], int l, int m, int r) {

int i, j, k;

int n1 = m - l + 1;

int n2 = r - m;

// Create temporary arrays

int L[n1], R[n2];

// Copy data to temporary arrays L[] and R[]

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

L[i] = arr[l + i];

for (j = 0; j < n2; j++)

R[j] = arr[m + 1 + j];

// Merge the temporary arrays back into arr[l..r]

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++;

}

// Copy the remaining elements of L[], if there are any

while (i < n1) {

arr[k] = L[i];

i++;

k++;

}

// Copy the remaining elements of R[], if there are any

while (j < n2) {

arr[k] = R[j];

j++;

k++;

}

}

// Main Function to perform mergesort

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

if (l < r) {

// Same as (l+r)/2, but avoids overflow for large l and r

int m = l + (r - l) / 2;

// Sort first and second halves

mergesort(arr, l, m);

mergesort(arr, m + 1, r);

// Merge the sorted halves

merge(arr, l, m, r);

}

}

// Main Function to demonstrate Mergesort

int main() {

int arr[] = {12, 11, 13, 5, 6, 7};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");

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

printf("%d ", arr[i]);

}

// Apply Mergesort

mergesort(arr, 0, n - 1);

printf("\nSorted array: ");

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

printf("%d ", arr[i]);

}

return 0;

}

Output:

Original array: 12 11 13 5 6 7

Sorted array: 5 6 7 11 12 13

Explanation:

Merge Function:

  • Merge function takes an array (arr[]) and indices l, m, and r representing two sorted sub-arrays to be merged.
  • It creates temporary arrays (L[] and R[]) to hold the values of the sub-arrays.
  • After that, the merge function merges the two sub-arrays back into the original array in sorted order.

Mergesort Function:

  • The mergesort function is the main recursive function for the Mergesort algorithm.
  • It recursively divides the array into halves until the base case (single-element sub-arrays) is reached.
  • After that, it recursively sorts and merges the sub-arrays until the entire array is sorted.

Main Function:

  • It declares an array of integers (arr) and its size n.
  • It prints the original array.
  • It calls the mergesort Function to sort the array.
  • It prints the sorted array.

Complexity analysis:

Time Complexity:

Average Case: O(n log n)

Mergesort consistently exhibits O(n log n) time complexity on average, making it efficient for a wide range of inputs.

Worst Case: O(n log n)

Mergesort's worst-case time complexity is also O(n log n), ensuring reliable performance even in scenarios where the input array is not well-behaved.

Best Case: O(n log n)

Unlike some other sorting algorithms, Mergesort does not have a separate best-case time complexity. It consistently performs well, regardless of the input distribution.

Space Complexity:

Additional Space (for Merging): O(n)

Mergesort requires additional space for merging, creating temporary arrays for each merging operation. The size of these temporary arrays is proportional to the size of the input array.

Total Space (including Input Array): O(n)

The total space complexity of Mergesort, including the input array and the additional space that is needed for merging, is O(n).

Method 3: Using Bubble Sort:

Simple Comparison and Swapping:

Bubble Sort is a straightforward sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.

Repeated Passes:

The algorithm makes multiple passes through the array, with each pass comparing and swapping adjacent elements as necessary.

Stable Sorting:

Bubble Sort is a stable sorting algorithm, meaning that it maintains the relative order of equal elements in the sorted output as they were in the input.

Inefficient for Large Datasets:

While easy to understand and implement, Bubble Sort is generally inefficient for large datasets compared to more advanced sorting algorithms such as Quicksort or Mergesort.

Program:

Let's take an example to illustrate the use of bubblesort to sort an array in C++:

#include <stdio.h>

//Function to perform Bubble Sort

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

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

for (int j = 0; j < n - i - 1; j++) {

// Compare adjacent elements and swap if they are in the wrong order

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;

}

}

}

}

// Main Function to demonstrate Bubble Sort

int main() {

int arr[] = {64, 34, 25, 12, 22, 11, 90};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");

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

printf("%d ", arr[i]);

}

// Apply Bubble Sort

bubbleSort(arr, n);

printf("\nSorted array: ");

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

printf("%d ", arr[i]);

}

return 0;

}

Output:

Original array: 64 34 25 12 22 11 90

Sorted array: 11 12 22 25 34 64 90

Explanation:

Bubble Sort Function (bubbleSort):

  • The outer loop (for i) represents each pass through the array, and the inner loop (for j) compares and swaps adjacent elements within that pass.
  • If an element at position j is greater than the element at position j + 1, the two elements are swapped.

Main Function:

  • It declares an array of integers (arr) and its size n.
  • It prints the original array.
  • It calls the bubbleSort Function to sort the array.
  • It prints the sorted array.

Complexity analysis:

Time Complexity:

Average Case: O(n^2)

In the average case, Bubble Sort compares and swaps elements in a quadratic fashion, resulting in a time complexity of O(n^2).

Worst Case: O(n^2)

The worst-case time complexity occurs when the array is sorted in reverse order, and every element needs to be compared and swapped in each pass, resulting in O(n^2) comparisons and swaps.

Best Case: O(n)

The best-case time complexity occurs when the array is already sorted. In this case, Bubble Sort still makes passes through the array, but no swaps are needed. The number of comparisons is still O(n), resulting in a best-case time complexity of O(n).

Space Complexity:

Additional Space: O(1)

Bubble Sort is an in-place sorting algorithm, meaning that it doesn't require additional space proportional to the input size. It only uses a constant amount of additional space for temporary variables, regardless of the size of the input array.

Method 4: Using Insertion Sort

Incremental Building:

Insertion Sort is a simple sorting algorithm that builds the final sorted array one element at a time.

It iterates through the input array, takes one element at a time from the unsorted part, and inserts it into its correct position in the sorted part.

Comparisons and Shifting:

For each element taken from the unsorted part, it is compared with the elements in the sorted part to find its correct position.

If the element is smaller (or larger, depending on the sorting order) than the compared element, it is shifted to make room for the new element.

Stable Sorting:

Insertion Sort is a stable sorting algorithm, meaning that it maintains the relative order of equal elements in the sorted output as they were in the input.

Efficient for Small Datasets:

While Insertion Sort has a quadratic time complexity; it is efficient for small datasets or nearly sorted datasets.

Program:

Let's take an example to illustrate the use of the insertion sort to sort an array in C++:

#include <stdio.h>

//Function to perform Insertion Sort

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

int i, key, j;

for (i = 1; i < n; i++) {

key = arr[i];

j = i - 1;

// Move elements 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;

}

// Insert the key into its correct position

arr[j + 1] = key;

}

}

// Main Function to demonstrate Insertion Sort

int main() {

int arr[] = {12, 11, 13, 5, 6};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");

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

printf("%d ", arr[i]);

}

// Apply Insertion Sort

insertionSort(arr, n);

printf("\nSorted array: ");

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

printf("%d ", arr[i]);

}

return 0;

}

Output:

Original array: 12 11 13 5 6

Sorted array: 5 6 11 12 13

Explanation:

Insertion Sort Function (insertionSort):

  • The insertionsort function iterates through the array starting from the second element (i = 1).
  • For each element, it compares it with the elements in the sorted part of the array and inserts it into its correct position by shifting the larger elements.

Main Function:

  • It declares an array of integers (arr) and its size n.
  • It prints the original array.
  • It calls the insertionSort Function to sort the array.
  • It prints the sorted array.

Complexity analysis:

Time Complexity:

Average Case: O(n^2)

In the average case, Insertion Sort compares and shifts elements in a quadratic fashion, resulting in a time complexity of O(n^2).

Worst Case: O(n^2)

The worst-case time complexity occurs when the array is sorted in reverse order, and every element needs to be compared and shifted in each pass, resulting in O(n^2) comparisons and shifts.

Best Case: O(n)

The best-case time complexity occurs when the array is already sorted. In this case, Insertion Sort still makes passes through the array, but no shifting is needed. The number of comparisons is still O(n), resulting in a best-case time complexity of O(n).

Space Complexity:

Additional Space: O(1)

Insertion Sort is an in-place sorting algorithm, meaning that it doesn't require additional space proportional to the input size. It only uses a constant amount of additional space for temporary variables, regardless of the size of the input array.

Method 5: Using Selection Sort

Divide into Sorted and Unsorted Regions:

Selection Sort divides the array into two regions: a sorted region and an unsorted region.

The sorted region starts as empty, and the unsorted region contains all the elements.

Select and Swap:

The algorithm iteratively selects the smallest (or largest, depending on the desired sorting order) element from the unsorted region and swaps it with the first element of the unsorted region.

Repeated Process:

This process is repeated until the entire array is sorted. In each iteration, the size of the sorted region increases, and the size of the unsorted region decreases.

In-Place Sorting:

Selection Sort is an in-place sorting algorithm, meaning that it doesn't require additional space proportional to the input size.

Stable Sorting:

Selection Sort is not a stable sorting algorithm, as the relative order of equal elements may change during the sorting process.

Program:

Let's take an example to illustrate the use of the selectionsort method to sort an array in C++:

#include <stdio.h>

//Function to perform Selection Sort

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

int i, j, min_idx;

// One by one move the boundary of the unsorted region

for (i = 0; i < n - 1; i++) {

// Find the minimum element in the unsorted region

min_idx = i;

for (j = i + 1; j < n; j++) {

if (arr[j] < arr[min_idx]) {

min_idx = j;

}

}

// Swap the found minimum element with the first element of the unsorted region

int temp = arr[min_idx];

arr[min_idx] = arr[i];

arr[i] = temp;

}

}

// Main Function to demonstrate Selection Sort

int main() {

int arr[] = {64, 25, 12, 22, 11};

int n = sizeof(arr) / sizeof(arr[0]);

printf("Original array: ");

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

printf("%d ", arr[i]);

}

// Apply Selection Sort

selectionSort(arr, n);

printf("\nSorted array: ");

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

printf("%d ", arr[i]);

}

return 0;

}

Output:

Original array: 64 25 12 22 11

Sorted array: 11 12 22 25 64

Explanation:

Selection Sort Function (selectionSort):

  • The outer loop (for i) represents the boundary between the sorted and unsorted regions. It iterates from the beginning to the second-to-last element.
  • The inner loop (for j) finds the index of the minimum element in the unsorted region.
  • After that, the minimum element is swapped with the first element of the unsorted region.

Main Function:

  • It declares an array of integers (arr) and its size n.
  • It prints the original array.
  • It calls the selectionSort function to sort the array.
  • It prints the sorted array.

Complexity analysis:

Time Complexity:

Average Case: O(n^2)

In the average case, Selection Sort performs nested loops, resulting in a quadratic time complexity.

Worst Case: O(n^2)

The worst-case time complexity also occurs when the array is sorted in reverse order, as the algorithm makes the same number of comparisons and swaps in each iteration.

Best Case: O(n^2)

Unlike some other sorting algorithms, Selection Sort doesn't have a separate best-case time complexity. It consistently performs the same number of comparisons and swaps, regardless of the initial order of the elements.

Space Complexity:

Additional Space: O(1)

Selection Sort is an in-place sorting algorithm. It doesn't require additional space proportional to the input size. The only additional space used is for a constant number of temporary variables, making the space complexity O(1)