Vous êtes sur la page 1sur 5

ABHISHEK SHUKLA

Sorting
Bubble Sort

Sorting in general sense is any process of arranging items systematically. In


computer science, arranging in an ordered sequence is called "sorting". Sorting
is a common operation in many applications, and efficient algorithms to
perform it have been developed.
Any number of practical applications in computing require things to be in
order. Even before we start computing, the importance of sorting is drilled into
us.
For example if we want to purchase a product from any e-commerce website
we have an option to sort the items according to our need.

"1

ABHISHEK SHUKLA

Other practical examples include words in dictionary , ranking of colleges.


So sorting is arranging the element in a list or collection in increasing or
decreasing order of some property.
Example : This is a list of integers : 2,3,9,4,6
Now if we want to sort in increasing order the list will be : 2,3,4,6,9
if we sort in decreasing order: 9,6,4,3,2
In increasing order of number of factors : 2,3,9,4,6
There are many type of sorting techniques.

Bubble Sort : Exchange two adjacent elements if they are out of order.
Repeat until array is sorted.

Insertion sort : Scan successive elements for an out-of-order item, then insert
the item in the proper place.

Selection sort : Find the smallest element in the array, and put it in the
proper place. Swap it with the value in the first position. Repeat until array is
sorted.

Quick sort : Partition the array into two segments. In the first segment, all
elements are less than or equal to the pivot value. In the second segment, all
elements are greater than or equal to the pivot value. Finally, sort the two
segments recursively.

Merge sort : Divide the list of elements in two parts, sort the two parts
individually and then merge it.

Bubble sort
Let us consider that we have a list of integers in a form of an array A with index
0 to 5.

"2

ABHISHEK SHUKLA

We want to rearrange the elements in increasing order. What we are going to


do is that we will scan the elements form left to right multiple times and we call
each scan one pass on array. In this algorithm when we scanning the array and
when we are at any particular position we will compare the elements at that
particular with adjacent position.

We would compare the element of current position with element at adjacent


position and if element at current position is greater than element at adjacent
position ( for sorting in increasing order ) we will swap the the two elements ( In
the above case 2 is not greater than 7 therefore no swapping would be done.)
and we will move on to the next position and again compare the elements and
the process going on till all elements are sorted.
The pseudocode for Bubble Sort will be:
for (int i = 0; i < array.length; i++)
for (int j = 0; j < array.length- 1; j++)
{
if (data[j] > data[j + 1])
{
tmp = data[j];

data[j] = data[j + 1];


data[j + 1] = tmp;
}
}

"3

ABHISHEK SHUKLA

Now lets analyse the Time Complexity of this algorithm.


Lets assume that the if statement in the above code take constant time C in
the worst case. The first loop will run n-1 times and the second loop will also run
n-1 times. So the total time taken:

T(n) = (n-1)*(n-1)*C
= Cn2 - 2Cn + 1

Hence the time complexity will be highest power of n i.e. O(n) .Since this is
O(n) runtime, and hence is very slow for large data sets.
We can do couple of things to improve time complexity . The first thing we can
do is we need not to run the second loop always till array.length-2 times
because at any stage the array will have some part sorted some not. There is no
point to run the loop for sorted path because there will be not swapping in that
part.For the first pass we can run the inner loop till array.length-2 and
for the second pass we can run the inner loop till array.length-3 and we
will be good. and hence we can run the inner loop till array.length-i-1
Hence our new code will be:

for (int i = 0; i < array.length; i++)


for (int j = 0; j < array.length - i ; j++)
{
if (data[j] > data[j + 1])
{
tmp = data[j];
data[j] = data[j + 1];
data[j + 1] = tmp;
}
}

But in this case the time complexity will again be O(n).


Sometimes the elements in the array is sorted after 3 or 4 passes and later passes
are only redundant . To tackle this problem we will make some change in our
algorithm. We would take a variable flag and initialise it to 0.
"4

ABHISHEK SHUKLA

for (int i = 0; i < array.length; i++)


flag = 0;
for (int j = 0; j < array.length - i ; j++)
{
if (data[j] > data[j + 1])
{
tmp = data[j];
data[j] = data[j + 1];
data[j + 1] = tmp;
flag=1;
}
if(flag == 0)
break;
}

And in this way we will avoid the redundant passes when the array is already
sorted. By this modification if we input an already sorted array the inner loop
will execute only once.

The single best advantage of a bubble sort is that it is very simple to
understand and it is a stable sort that requires no additional memory, since all
swaps are made in place.

"5

Vous aimerez peut-être aussi