Sunday 5 October 2014

what is Bubble sorting

Bubble sort
Bubble sort  sometimes referred to as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, compaire Bring each pair of adjacent items and swapping them if they are in the wrong order
The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted
The algorithm gets its name from the way smaller elements "bubble" to the top of the list.
Because it only uses comparisons to operate on elements, it is a comparison sort

 Performance of bubble sort

Rabbits and turtles

The positions of the elements in bubble sort will play a large part in determining its performance.
Large elements at the beginning of the list do not pose a problem, as they are quickly swapped.
Small elements towards the end, however, move to the beginning extremely slowly.
This has led to these types of elements being named rabbits and turtles respectively.
Various efforts have been made to eliminate turtles to improve upon the speed of bubble sort


Complexity of bubble sort

  • It is measured in terms of number of comparisons. 
  •  It requires n-1 comparisons to sort list of n numbers  

Algorithm of bubble sort

(Bubble Sort) BUBBLE (DATA, N)

1.Repeat steps 2 and 3 for K= 1 to N-1

2. Set PTR:=1 [initializes pass pointer PTR]

3.ReRpeat while PTR <= N-K [executes pass]

           (a) If DATA[PTR] > DATA[PTR+1], then:

           Interchange DATA[PTR] and DATA[PTR+1]

  [End of if structure]

            (b) Set PTR:= PTR+1

         [end of inner loop]

  [end of step 1 outer loop]

4.    Exit
 



0 comments:

Post a Comment