What is Bubble Sort Algorithm?


Bubble Sort Algorithm:

Bubble sort is an algorithm which is used to sort N elements that are given in a memory. Bubble sort compares all the elements one by one and sort them based on their values.

Algorithm: (Bubble sort) BUBBLE (DATA, N)

Here, DATA is an array with n elements.

The algorithm sorts the array.

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

2.      Set PTR = 1 [Initializes pass pointer PTR.]

3.      Repeat while PTR ≤ N - K [Execute 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.

No comments

Powered by Blogger.