1. What is the difference between Array List and Linked List ?
2. Inserting a node at the beginning of linked list
3. Insert a node after a given node in linked list
4. How to insert a node at the end of a given node? - Codzify.com Tutorials
5. Bubble Sort Algorithm with practical program in C
6. Insertion Sort Algorithm with practical program in C
7. Merge Sort Algorithm with practical program in C
8. BFS and DFS in Data Structure | Inorder, Preorder and Postorder Traversal with practical example.
9. Kruskals Algorithm with practical example
10. How to calculate the complexity of Algorithms?
11. How to insert a new node after a given node in a linked list? - Codzify.com Tutorials
12. How to insert a new node after a given node in a linked list? - Codzify.com

**Article by:** Manish Methani

**Last Updated:** October 10, 2021 at 10:04am IST

Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order.

Suppose we have to sort the array of elements(1,8,5,4,2) in ascending order using **Bubble Sort:**

**1) Array is 1, 8, 5, 4, 2 . **

In the given array first two elements are compared **1 & 8** and since **1 < 8**, no swapping takes place.

**2) Array becomes 1, 8, 5, 4, 2. **

Now the next two elements are compared **8 & 5** and since **8 > 5,** a swap takes place.

After swapping 8 & 5 array looks like this 1,5,8,4,2.

**3) Array becomes 1, 5, 8, 4, 2. **

Now the next two elements are compared **8 & 4** as and since **8 > 4, a swap takes place.**

After swapping 8 & 4, the array looks like this 1,5,4,8,2.

**4) Array becomes 1, 5, 4, 8, 2. **

Now the next two elements are compared **8 & 2** as and since **8 > 2, a swap takes place.**

After swapping 8 & 2, the array looks like this 1, 5, 4, 2, 8.

**1) Array becomes 1, 5, 4, 2, 8. **

Now the next two elements are compared **1 & 5** and since **1 < 5,** no swap here.

**2) Array becomes 1, 5, 4, 2, 8. **

Now the next two elements are compared **5 & 4** as and since **5 > 4, a swap takes place.**

After swapping 5 & 4, the array looks like this 1, 4, 5, 2, 8.

**3) Array becomes 1, 4, 5, 2, 8. **

Now the next two elements are compared **5 & 2** as and since **5 > 2, a swap takes place.**

After swapping 5 & 2, the array looks like this 1, 4, 2, 5, 8.

**1) Array becomes 1, 4, 2, 5, 8. **

Now the next two elements are compared **1 & 4** and since **1 < 4,** no swap here.

**2) Array becomes 1, 4, 2, 5, 8. **

Now the next two elements are compared **4 & 2** as and since **4 > 2, a swap takes place.**

After swapping 5 & 4, the array looks like this 1, 2, 4, 5, 8.

**1) Array becomes 1, 2, 4, 5, 8. **

Now the next two elements are compared **1 & 2** and since **1 < 2,** no swap here.

Notice that a total of 4 passes was required to sort the array of 5 elements. **In the first pass**, 4 comparisons were done and the largest element was bubbled out at the end of the array. **Similarly, in the second pass**, 3 comparisons were done, **in the third pass**, 2 comparisons were done, and so on... At the end of each pass, the largest element gets **bubbled up** near the end of the array.

**In general,**

To bubble sort an array of N elements, total **N-1** passes are required in the worst case.

**In the first pass,** N-1 comparisons will be done, **in the second pass**, N-2 comparisons will be made, **in the third pass**, N-3 comparisons and so on until only 1 comparison is needed.

**So total number of comparisons **

= (N-1) + (N-2) + (N-3) + .... + 1 = (N-1)(N-2) / 2 = O(N2) ....ignoring constants and lower order terms..

**Thus, the complexity of bubble sort in the worst case is O(N2) where N is the number of elements of the array.**

This basic Bubble Sort in C programming explains the concept of how to sort the elements using bubble sort in Data Structures.

#include void swap(int *a, int *b){ *a = *a + *b; *b = *a - *b; *a = *a -*b; } void bubbleSort(int N, int arr[]) { int pass = 0; int flag = 1; int i; for(pass = 0; pass < N && flag; pass++) { printf("Pass %d : ", pass); for (i = 0; i < N - pass -1; ++i) { if (arr[i] > arr[i+1]) { swap(&arr[i], &arr[i+1]); flag = 1; } else{ flag = 0;+ } } for (i = 0; i < N; ++i) { printf("%d ", arr[i]); } printf(" "); printf(" "); } } int main(void) { int N; scanf("%d", &N); int arr[N], i; for(i = 0; i < N; i++) { scanf("%d", &arr[i]); } printf("Input Array is: "); for(i = 0; i < N; i++) { printf("%d ", arr[i]); } printf(" "); bubbleSort(N, arr); printf("Sortrd Array: "); for(i = 0; i < N; i++) { printf("%d ", arr[i]); } printf(" "); return 0; }

Firebase & Angular.

Looking to learn how to create the production-ready apps

from scratch? Well, you are at the right place.

₹1299 or $15.56

## Learn to create the Dating App using No-Code Tool FlutterFlow

Level: **Advanced**

Mock Tests.

Test your C Programming skills with this comprehensive mock test on C Programming.

Take TestSolve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take TestSolve most asked GATE Questions in Operating Systems and test your Gate Score.

Take TestThis is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take TestSolve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take TestMobile App