WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C · Advanced · question 52 of 100

What are the common sorting algorithms in C, and how do their time complexities compare?

📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

There are several sorting algorithms in C that are commonly used, each with its own advantages and disadvantages. Some of the most common sorting algorithms are:

Bubble Sort: This is a simple sorting algorithm that repeatedly swaps adjacent elements if they are in the wrong order. It has a worst-case time complexity of O(n2̂).

Example code:

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

Selection Sort: This algorithm sorts an array by repeatedly finding the minimum element from the unsorted part of the array and putting it at the beginning. It has a worst-case time complexity of O(n2̂).

Example code:

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

Insertion Sort: This algorithm works by iterating over an array and inserting each element into its correct position in a sorted sub-array. It has a worst-case time complexity of O(n2̂).

Example code:

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

Merge Sort: This algorithm uses a divide-and-conquer approach to recursively divide an array into two halves, sort each half, and then merge the two sorted halves. It has a worst-case time complexity of O(n log n).

Example code:

    void merge(int arr[], int l, int m, int r) {
        int i, j, k;
        int n1 = m - l + 1;
        int n2 = r - m;
        int L[n1], R[n2];
        
        for (i = 0; i < n1; i++) {
            L[i] = arr[l + i];
        }
        for (j = 0; j < n2; j++) {
            R[j] = arr[m + 1 + j];
        }
        
        i = 0;
        j = 0;
        k = l;
        
        while (i < n1 && j < n2) {
            if (L[i] <= R[j]) {
                arr[k] = L[i];
                i++;
            } else {
                arr[k] = R[j];
                j++;
            }
            k++;
        }
        
        while (i < n1) {
            arr[k] = L[i];
            i++;
            k++;
        }
    }
Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C interview — then scores it.
📞 Practice C — free 15 min
📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

All 100 C questions · All topics