Bubble Sort
#include <stdio.h>
void swap(int* arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
// Last i elements are already in place, so the loop
// will only num n - i - 1 times
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1])
swap(arr, j, j + 1);
} }}
int main() {
int arr[] = { 6, 0, 3, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
// Calling bubble sort on array arr
bubbleSort(arr, n);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
Time Complexity: O(n2), where n is the number of items in the list.
Selection Sort
// C program for implementation of selection sort
#include <stdio.h>
void selectionSort(int arr[], int N) {
// Start with the whole array as unsorted and one by
// one move boundary of unsorted subarray towards right
for (int i = 0; i < N - 1; i++) {
// Find the minimum element in unsorted array
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 in the unsorted part
int temp = arr[min_idx];
arr[min_idx] = arr[i];
arr[i] = temp;
int main() {
int arr[] = {64, 25, 12, 22, 11};
int N = sizeof(arr) / sizeof(arr[0]);
printf("Unsorted array: \n");
for (int i = 0; i < N; i++) {
printf("%d ", arr[i]);
printf("\n");
// Calling selection sort
selectionSort(arr, N);
printf("Sorted array: \n");
for (int i = 0; i < N; i++) {
printf("%d ", arr[i]);
printf("\n");
return 0;
Time Complexity: O(N2), as there are two nested loops.