Difference between revisions of "Interview Preparation Bubble Sort"

From Embedded Systems Learning Academy
Jump to: navigation, search
Line 1: Line 1:
I think this ia a simplest sorting algorithm for implementation.  
+
I think this is simplest sorting algorithm among all other sorting algorithms.  
 
Algorithm for placing an array of elements n in an ascending order is listed below,  
 
Algorithm for placing an array of elements n in an ascending order is listed below,  
  
Line 11: Line 11:
  
  
Similar algorithm can be framed for placing elements in descending order with minute changes.
+
Similar to above algorithm, Algorithm for placing an array of elements n in descending order can be formulated with minute changes.
  
 
For an array with n number of elements. Worst case complexity = О(n^2) and average complexity = О(n^2).
 
For an array with n number of elements. Worst case complexity = О(n^2) and average complexity = О(n^2).

Revision as of 12:48, 25 November 2016

I think this is simplest sorting algorithm among all other sorting algorithms. Algorithm for placing an array of elements n in an ascending order is listed below,

a. Begin from the first element and start comparing two elements.

b. If the element at smaller index is greater swap the element.

c. Increment the index by one and repeat step 2 for n-1 iteration.

d. Repeat step b and C for n-1 times.


Similar to above algorithm, Algorithm for placing an array of elements n in descending order can be formulated with minute changes.

For an array with n number of elements. Worst case complexity = О(n^2) and average complexity = О(n^2).