Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. C
  4. Mastering Sorting Algorithms in C: Bubble, Selection, and Insertion Sort

Mastering Sorting Algorithms in C: Bubble, Selection, and Insertion Sort

Date- Mar 14,2026 128
c sorting

Overview of Sorting Algorithms

Sorting algorithms are procedures that arrange the elements of a list or array in a specific order, typically in ascending or descending order. Efficient sorting is crucial in computer science as it optimizes data retrieval and improves the performance of algorithms that rely on sorted data. By mastering sorting algorithms like Bubble Sort, Selection Sort, and Insertion Sort, programmers can enhance their problem-solving skills and algorithmic thinking.

Prerequisites

  • Basic understanding of C programming language
  • Familiarity with arrays and loops
  • Knowledge of functions and their usage in C
  • Concept of algorithm complexity

Bubble Sort Algorithm

Bubble Sort is one of the simplest sorting algorithms. It repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The process is repeated until the list is sorted.

#include 

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

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

int main() {
    int arr[] = {64, 34, 25, 12, 22, 11, 90};
    int n = sizeof(arr)/sizeof(arr[0]);
    bubbleSort(arr, n);
    printf("Sorted array: ");
    printArray(arr, n);
    return 0;
}

This code defines a bubbleSort function that takes an array and its size as parameters. It contains two nested loops: the outer loop runs through the array, while the inner loop compares adjacent elements.

The condition if (arr[j] > arr[j+1]) checks whether the current element is greater than the next one. If it is, the elements are swapped using a temporary variable. The process continues until the entire array is sorted.

Selection Sort Algorithm

Selection Sort works by dividing the input list into two parts: a sorted part and an unsorted part. It repeatedly selects the smallest (or largest) element from the unsorted part and moves it to the sorted part.

#include 

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

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

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

The selectionSort function iterates through the array, keeping track of the index of the minimum element. The inner loop searches for the smallest element in the unsorted part of the array. Once found, it swaps this element with the first element of the unsorted part.

Insertion Sort Algorithm

Insertion Sort builds a sorted array one element at a time. It is much less efficient on large lists than more advanced algorithms like quicksort, heapsort, or merge sort, but it has the advantage of being simple and efficient for small datasets.

#include 

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];
            j--;
        }
        arr[j + 1] = key;
    }
}

void printArray(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");
}

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

The insertionSort function starts with the second element (key) and compares it to the elements before it. If the key is smaller, it shifts the larger elements to the right until it finds the correct position for the key, effectively inserting it into the sorted part of the array.

Best Practices and Common Mistakes

When working with sorting algorithms, keep the following best practices in mind:

  • Always validate your input data before sorting.
  • Be mindful of the algorithm's time complexity — for small datasets, simpler algorithms like Insertion Sort can be efficient, while larger datasets may require more complex algorithms.
  • Consider using built-in sorting functions provided by libraries when performance is critical.

Common mistakes include:

  • Not accounting for edge cases, such as empty arrays or arrays with one element.
  • Using the wrong comparison operator, leading to incorrect sorting.
  • Failing to optimize for best-case scenarios, especially in algorithms like Insertion Sort.

Conclusion

In this blog post, we explored three fundamental sorting algorithms: Bubble Sort, Selection Sort, and Insertion Sort. Each of these algorithms has its strengths and weaknesses, and understanding them is crucial for optimizing data handling in your applications. Remember to consider the size of your dataset and the context in which these algorithms will be used to select the most appropriate sorting method.

By practicing these algorithms and recognizing common pitfalls, you will build a solid foundation in algorithm design and analysis, which is essential for any aspiring software engineer.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Mastering Bitwise Operators in C: A Comprehensive Guide
Mar 15, 2026
Understanding Searching Algorithms in C: Linear and Binary Search Explained
Mar 14, 2026
Mastering Linked Lists in C: A Comprehensive Guide
Mar 14, 2026
Understanding Preprocessor Directives in C: A Beginner's Guide
Mar 13, 2026
Previous in C
Understanding Stacks and Queues in C: A Beginner's Guide
Next in C
Understanding Searching Algorithms in C: Linear and Binary Search…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    HTTP Error 500.32 Failed to load ASP NET Core runtime 6,938 views
  • 2
    Error-An error occurred while processing your request in .… 11,273 views
  • 3
    Comprehensive Guide to Error Handling in Express.js 235 views
  • 4
    ConfigurationBuilder does not contain a definition for Set… 19,459 views
  • 5
    Mastering JavaScript Error Handling with Try, Catch, and F… 162 views
  • 6
    Mastering Unconditional Statements in C: A Complete Guide … 21,497 views
  • 7
    Unable to connect to any of the specified MySQL hosts 6,232 views

On this page

More in C

  • Mastering Unconditional Statements in C: A Complete Guide wi… 21497 views
  • Understanding C: A Complete Guide with Examples 5147 views
  • Mastering Unconditional Statements in C: A Complete Guide wi… 4217 views
  • Mastering 2-D Arrays in C: A Complete Guide with Examples 3935 views
  • Introduction to C: A Step-by-Step Guide with Examples 3586 views
View all C posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor