Difference between revisions of "Interview Preparation Bubble Sort"

From Embedded Systems Learning Academy
Jump to: navigation, search
(Created page with "I think this ia a simplest sorting algorithm for implementation. Algorithm for placing an array of elements n in an ascending order is listed below, a. Begin from the first...")
 
Line 1: Line 1:
 
I think this ia a simplest sorting algorithm for implementation.  
 
I think this ia a simplest sorting algorithm for implementation.  
 
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,  
 +
 
a. Begin from the first element and start comparing two elements.
 
a. Begin from the first element and start comparing two elements.
 +
 
b. If the element at smaller index is greater swap the element.
 
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.
 
c. Increment the index by one and repeat step 2 for n-1 iteration.
 +
 
d. Repeat step b and C for n-1 times.
 
d. Repeat step b and C for n-1 times.
 +
  
 
Similar algorithm can be framed for placing elements in descending order with minute changes.
 
Similar algorithm can be framed for placing elements in descending order 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:17, 25 November 2016

I think this ia a simplest sorting algorithm for implementation. 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 algorithm can be framed for placing elements in descending order with minute changes.

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