Bubble SortBubble Sort also known as Exchange Sort, is a simple sorting algorithm. It works by repeatedly stepping throughout the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order. The pass through the list is duplicated until no swaps are desired, which means the list is sorted. This is the easiest method among all sorting algorithms. BUBBLE SORT (A) 1. for i ← 1 to length [A] 2. for k ← length [A] down to i+1 3. if A[k] <A[k1] 4. exchange (A[k], A [k1]) Analysis:
In pass 1: n1 comparisons are required In pass 2: n2 comparisons are required In pass 3: n3 comparisons are required ............................................................................ ............................................................................... In pass n1: 1 comparisons is required Total comparisons: T (n) = (n1) + (n2) +...........+ 1 = = o (n^{2}) Therefore complexity is of order n^{2} Example:Unsorted List: A = {7, 2, 1, 4, 5, 9, 6} i.e., A [] =
And A [] =
Then A [] =
Then A [] =
And A [] =
Then A [] =
And A [] =
And A [] =
is the sorted array.
Next TopicSelection Sort
