0% found this document useful (0 votes)
2 views

Bubble Sort_Selection Sort

The document provides C implementations of two sorting algorithms: Bubble Sort and Selection Sort. Both algorithms have a time complexity of O(n^2) due to their nested loop structures. The Bubble Sort function repeatedly swaps adjacent elements if they are in the wrong order, while the Selection Sort function finds the minimum element from the unsorted part and swaps it with the first unsorted element.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Bubble Sort_Selection Sort

The document provides C implementations of two sorting algorithms: Bubble Sort and Selection Sort. Both algorithms have a time complexity of O(n^2) due to their nested loop structures. The Bubble Sort function repeatedly swaps adjacent elements if they are in the wrong order, while the Selection Sort function finds the minimum element from the unsorted part and swaps it with the first unsorted element.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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.

You might also like