**By: Fatima Zahra Khan**.

#### What is Data Sorting?

As you all know Sorting is actually arranging something, either in ascending or descending order. Here I will talk about Data Sorting; data can be alphabetical or numerical. I will try to cover the sorting algorithms with respect to BSCS (Bachelors of Computer Science). I already discussed the DAA in my last blog, if you didn’t go through it then you should first study this.

##### Let’s start with Bubble Sort.

Bubble sort, also known as sinking sort, is a very simple sorting algorithm. It repeatedly steps through the array, compares adjacent elements of the array, and swaps them if they are in the wrong order. The pass through the array is repeated until the array is sorted in the desired order. Let’s take an example.

Suppose we have the following unsorted array of numbers named array1 with 5 elements:

Index | 0 | 1 | 2 | 3 | 4 |

Array1 | 15 | 16 | 6 | 8 | 5 |

So n (number of elements) = 5

According to bubble sort first, we have to compare first two elements (array1[0] == array1[1]) to check if they are in the correct order if yes then leave it as is and move to the next two elements and do it until the last element reached this process is also known as Pass.

##### e.g:

As you see in the end of 1^{st} Pass the highest number 16 bubbled in the last of an array. Now we will apply the same logic until the array1 completely sorted. Let’s start Pass 2

In the start of pass 2 we have the array as below.

We will not swap the elements as we see the elements are in correct order. Here our 2^{nd} pass is completed. Let’s move to 3^{rd} pass.

After completion of 3^{rd} pass we see that the all three largest elements are placed in the last of the array at their correct position. To complete the array sorting we now have to perform one more pass. Let’s do it now.

After step 1 of pass 4, there is no need to swap as all numbers are placed in their correct order.

That is how bubble sort is done. Always remember when start coding this sorting, avoid unnecessary compressions and passes, you have write optimized code.

** Y our are always welcome to write comments and suggestions **