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

Bubble Sort

Bubble sort is a sorting algorithm that repeatedly compares and swaps adjacent elements until the array is sorted. The largest unsorted element is moved to the end of the array in each iteration. The document also includes a C implementation of the bubble sort algorithm.

Uploaded by

anakinalt9
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Bubble Sort

Bubble sort is a sorting algorithm that repeatedly compares and swaps adjacent elements until the array is sorted. The largest unsorted element is moved to the end of the array in each iteration. The document also includes a C implementation of the bubble sort algorithm.

Uploaded by

anakinalt9
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Bubble Sort

Bubble sort is a sorting algorithm that compares two adjacent elements


and swaps them until they are in the intended order.

Just like the movement of air bubbles in the water that rise up to the
surface, each element of the array move to the end in each iteration.
Therefore, it is called a bubble sort.

Working of Bubble Sort

Suppose we are trying to sort the elements in ascending order.

1. First Iteration (Compare and Swap)

1. Starting from the first index, compare the first and the second
elements.

2. If the first element is greater than the second element, they are
swapped.

3. Now, compare the second and the third elements. Swap them if they
are not in order.
4. The above process goes on until the last element.

Compare the
Adjacent Elements

2. Remaining Iteration

The same process goes on for the remaining iterations.

After each iteration, the largest element among the unsorted elements is
placed at the end.
Put the largest element at
the end

In each iteration, the comparison takes place up to the last unsorted


element.

Compare the adjacent


elements

The array is sorted when all the unsorted elements are placed at their
correct positions.
The array is sorted if all elements are kept in the right order

// Bubble sort in C

#include <stdio.h>

// perform the bubble sort

void bubbleSort(int array[], int size) {

// loop to access each array element

for (int step = 0; step < size - 1; ++step) {

// loop to compare array elements

for (int i = 0; i < size - step - 1; ++i) {

// compare two adjacent elements

// change > to < to sort in descending order

if (array[i] > array[i + 1]) {

// swapping occurs if elements

// are not in the intended order

int temp = array[i];


array[i] = array[i + 1];

array[i + 1] = temp;

// print array

void printArray(int array[], int size) {

for (int i = 0; i < size; ++i) {

printf("%d ", array[i]);

printf("\n");

int main() {

int data[] = {-2, 45, 0, 11, -9};

// find the array's length

int size = sizeof(data) / sizeof(data[0]);

bubbleSort(data, size);

printf("Sorted Array in Ascending Order:\n");

printArray(data, size);

You might also like