0% found this document useful (0 votes)
15 views5 pages

Bubble Sort

The document provides an overview of the Bubble Sort algorithm, explaining its basic functionality and providing examples of its execution. It includes C code implementations for both standard and optimized versions of Bubble Sort, as well as a recursive approach. The document also emphasizes the algorithm's simplicity and the need for multiple passes to confirm that the array is sorted.

Uploaded by

maariaam.gh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Bubble Sort

The document provides an overview of the Bubble Sort algorithm, explaining its basic functionality and providing examples of its execution. It includes C code implementations for both standard and optimized versions of Bubble Sort, as well as a recursive approach. The document also emphasizes the algorithm's simplicity and the need for multiple passes to confirm that the array is sorted.

Uploaded by

maariaam.gh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1

Bubble Sort
Background :
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent
elements if they are in wrong order.
Example:
First Pass:
( 5 1 4 2 8 ) –> ( 1 5 4 2 8 ), Here, algorithm compares the first two elements, and swaps since 5
> 1.
( 1 5 4 2 8 ) –> ( 1 4 5 2 8 ), Swap since 5 > 4
( 1 4 5 2 8 ) –> ( 1 4 2 5 8 ), Swap since 5 > 2
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (8 > 5), algorithm
does not swap them.
Second Pass:
( 1 4 2 5 8 ) –> ( 1 4 2 5 8 )
( 1 4 2 5 8 ) –> ( 1 2 4 5 8 ), Swap since 4 > 2
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The
algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
( 1 2 4 5 8 ) –> ( 1 2 4 5 8 )
2

// C program for implementation of 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;
}
3

// C Program with Optimized implementation of Bubble sort


#include <stdbool.h>
#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

void bubbleSort(int arr[], int n) {


for (int i = 0; i < n - 1; i++) {
// swapped variable to signal if there is a
// swap happened in the inner loop
// initially set to false
bool swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(arr + j, arr + j + 1);

// swapped is set to true if the swap is


// done
swapped = true;
}
}

// If no two elements were swapped


// by inner loop, then break
if (swapped == false)
break;
}
}

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;
}
4

How to implement it recursively?


Recursive Bubble Sort has no performance/implementation advantages, but can be
a good question to check one’s understanding of Bubble Sort and recursion.
If we take a closer look at Bubble Sort algorithm, we can notice that in first pass,
we move largest element to end (Assuming sorting in increasing order). In second
pass, we move second largest element to second last position and so on.
Recursion Idea.
1. Base Case: If array size is 1, return.
2. Do One Pass of normal Bubble Sort. This pass fixes last element of current
subarray.
3. Recur for all elements except last of current subarray.
// C program for recursive implementation
// of Bubble sort
#include <stdio.h>

// Swap function
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}

// A function to implement bubble sort


void bubbleSort(int arr[], int n)
{
// Base case
if (n == 1)
return;

int count = 0;
// One pass of bubble sort. After
// this pass, the largest element
// is moved (or bubbled) to end.
for (int i=0; i<n-1; i++)
if (arr[i] > arr[i+1]){
swap(&arr[i], &arr[i+1]);
count++;
}

// Check if any recursion happens or not


// If any recursion is not happen then return
if (count==0)
return;

// Largest element is fixed,


// recur for remaining array
bubbleSort(arr, n-1);
}

/* Function to print an array */


5

void printArray(int arr[], int n)


{
for (int i=0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

// Driver program to test above functions


int main()
{
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr)/sizeof(arr[0]);
bubbleSort(arr, n);
printf("Sorted array : \n");
printArray(arr, n);
return 0;
}

You might also like