Quick Sort

Medium Sorting Algorithms
Solve in Playground →

Problem Statement

Write a C program to implement Quick Sort to sort an array of integers in ascending order.

Input Format

First line: N. Second line: N space-separated integers.

Output Format

Print the sorted array separated by a space.

Constraints

1 <= N <= 1000

Sample Input

5
10 7 8 9 1

Sample Output

1 7 8 9 10

Explanation

Partition-based sorting algorithm dividing around a pivot.

Starter Code

#include <stdio.h>

void swap(int* a, int* b) {
    int t = *a;
    *a = *b;
    *b = t;
}

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 pi = partition(arr, low, high);
        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}

int main() {
    int n;
    if (scanf("%d", &n) == 1) {
        int arr[n];
        for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
        quickSort(arr, 0, n - 1);
        for (int i = 0; i < n; i++) {
            printf("%d", arr[i]);
            if (i < n - 1) printf(" ");
        }
        printf("n");
    }
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used