Data Structures & Algorithms
Prof. Ravi Prakash Gorthi Aug-Dec, 2010
Data Structures & Algorithms
Topics Why Study DS & Algorithms? Abstract Data Types Arrays and Linked Lists Stacks & Queues Trees & Graphs Sorting and Searching Complexity of Algorithms
Bubbling All the Elements
2 1 3 4 5
# of comparisons is on an average N/2
Number of Iterations is (N 1)
1 42 1 35 1 12 1 12 1 5
2 35 2 12 2 35 2 5 2 12
3 12 3 42 3 5 3 35 3 35
4 77 4 5 4 42 4 42 4 42
5 5 5 77 5 77 5 77 5 77
6 101 6 101 6 101 6 101 6 101
Bubble Sort Algorithm
Sorted-List[] := Bubble-Sort (USL[], N); /* USL*1:N+ contains the N Unsorted Integers */ { int J := N 1; boolean did-swap := true; while ((J > 0) AND (did-swap)) do /* this while loop performs (N-1) iterations */ { did-swap := false;
In the worst-case, the number of iterations is (N-1)!
for (K = 1 to J) do /* this for loop bubbles-up the next largest integer into the (J+1) position progressively*/ { if (USL[K] > USL[K + 1]) then { Swap (USL[K], USL[K + 1]); did-swap := true; In the worst-case, } the number of } comparisons is N/2! J := J 1; } return (USL[]); }
Bubble Sort Algorithm
Sorted-List[] := Bubble-Sort (USL[], N); /* USL*1:N+ contains the N Unsorted Integers */ { int J := N 1; boolean did-swap := true; while ((J > 0) AND (did-swap)) do /* this while loop performs (N-1) iterations */ { did-swap := false;
In the best-case, the number of iterations is 1!
for (K = 1 to J) do /* this for loop bubbles-up the next largest integer into the (J+1) position progressively*/ { if (USL[K] > USL[K + 1]) then { Swap (USL[K], USL[K + 1]); did-swap := true; In the best-case, the } the number number of comparisons of } comparisons is (N-1)! is N! J := J 1; } return (USL[]); }
Complexity of Bubble Sort Algorithm
In the worst-case, this algorithm performs (N 1) iterations and on an average (N/2) comparisons in each iteration; that is, (N 1) * (N/2). Worst-case Complexity is O (n2). In the Best-case, when the given list is already sorted, this algorithm makes N comparisons; i.e. complexity is O (n). Assume that there are 1million integers and each comparison takes 1 micro-second. This sort algorithm takes ((10) ** (12)) * ( (10) ** (-6)) seconds, which is (10**(6)) seconds or approximately 278 hours or 11 days.
Insertion Sort
Example: sorting numbered cards
23
1
17
2
45
3
18
4
12
5
22
6
Example: sorting numbered cards
17
1 2
45
3
18
4
12
5
22
6
23
1 2
Example: sorting numbered cards
45
1 2 3
18
4
12
5
22
6
17
1
23
2
Example: sorting numbered cards
18
1 2 3 4
12
5
22
6
17
1
23
2
45
3 4
5 6
Example: sorting numbered cards
18
1 2 3 4
12
5
22
6
17
1
23
2
45
3 4
5 6
Example: sorting numbered cards
12
1 2 3 4 5
22
6
17
1
18
2
23
3
45
4
5 6
Example: sorting numbered cards
12
1 2 3 4 5
22
6
17
1
18
2
23
3
45
4
5 6
Example: sorting numbered cards
22
1 2 3 4 5 6
12
1
17
2
18
3
23
4
45
5 6
Example: sorting numbered cards
22
1 2 3 4 5 6
12
1
17
2
18
3
23
4
45
5 6
Example: sorting numbered cards
12
1
17
2
18
3
22
4
23
5
45
6
Insertion Sort
Sorted-List[] := Insertion-Sort (USL[], N);
{ USL[1:N] is the unsorted list of integers */ int array Temp-SL[1:N]; int I, J, K; Temp-SL[1] := USL[1]; for (I = 2 to N) do {/* insert element USL[I] into Temp-SL[] */
In the worst-case, the number of iterations is (N-1)!
Temp-SL [] := Insert-element (USL[I], Temp-SL[], I); } return (Temp-SL[]); }
Insertion Sort (Contd.)
Temp-SL[] := Insert-element (element, Temp-SL[], I);
{/* insert element into the right position in Temp-SL[] */ boolean did-insert := false; int J := I 1; while ((!did-insert) && (J != 0)) do { if (element > Temp-Sl[J] then { Temp-SL[J + 1] := element; did-insert := true; } else Temp-SL [J + 1] := Temp-SL [J]; J := J 1; } if (!did-insert) then Temp-SL [1] := element; return (Temp-SL[]); }
In the worst-case, the number of comparisons is N/2!
In the best-case, the number of comparisons is 1!
Complexity of Insertion Sort Algorithm
In the worst-case, this algorithm performs (N 1) iterations and on an average (N/2) comparisons in each iteration; that is, (N 1) * (N/2). Worst-case Complexity is O (n2). In the Best-case, when the given list is already sorted, this algorithm makes N comparisons; i.e. complexity is O (n).