Bubble Sort Algorithm
Lesson 4 of 11 in Coddy's Bubble Sort course.
We have discussed how to iterate and perform swapping, but that's not the complete algorithm for bubble sort. We have to repeat this process until the whole list is sorted.
As discussed in the previous lessons, we noticed that with the first pass, we have the maximum element at the end of the list. And this is what we will try to achieve through the subsequent passes.
So, how many passes are required? What if we have a list of n elements?
Yes, n passes are required to sort the whole list.
Challenge
Easy Create a function named bubble_sort that receives an array and size of the array . Iterate through the array, compare and swap adjacent elements. Repeat this process n times, to get a final sorted list. Return the sorted list.
Try it yourself
#include <stdio.h>
#include <stdlib.h>
int* bubble_sort(int* arr, int arr_size, int n, int* returnSize) {
// Write code here
*returnSize = n;
return arr;
}
All lessons in Bubble Sort
1Basics of Bubble Sort
IntroductionWorking of the Bubble SortSwap adjacent elementsBubble Sort Algorithm