Comb Sort: Submitted BY
Comb Sort: Submitted BY
Submitted BY:
Rida Fatima
(2008-CS-183)
Saleha Rehman
(2008-CS-101)
Description:
Comb sort is a sorting algorithm. It basically derives its idea from bubble sort. In bubble
sort when two elements are compared they always have a gap of 1, so sometimes it gets
slowdown because of turtles (very small values at the end of list). Thus,The Comb Sort,
along with the Cocktail Sort, was developed as an improvement to the Bubble Sort using
the idea of killing the turtles. Turtles drastically reduce the efficiency of the Bubble Sort
and hence are an obvious place to attempt optimization. Comb sort improves on bubble sort.
It does so by making slight modifications to bubble sort and introducing shrink
factor(usually 1.3) .In comb sort the gap starts out as the length of the list being sorted
divided by the shrink factor , and the list is sorted with that value (rounded down to an
integer if needed) for the gap. Then the gap is divided by the shrink factor again, the list
is sorted with this new gap, and the process repeats until the gap is 1. At this point, comb
sort continues using a gap of 1 until the list is fully sorted. This modification enhances the
efficiency of the algorithm.
Pseudocode:
Combsort (list)
if gap>1 then
initialize index to 1
increment index
INPUT: 9 8 7 6 5 4 3 2 1
list_length = 9
First iteration:
gap = 7
swapped =0
index = 1
list= 2 8 7 6 5 4 3 9 1
Second Iteration:
swapped = 1
index =2
list = 2 1 7 6 5 4 3 9 8
Third Iteration:
swapped =1
index = 3
gap = 5
swapped = 0
Index keeps incrementing from 1 to 4 without changing the list which remain as:
2 1 7 6 5 4 3 9 8
gap =4,swapped = 0
List = 2 1 3 6 5 4 7 9 8
swapped = 1
gap = 3
swapped = 0
gap =2,swapped =0
list = 2 1 3 4 5 6 7 9 8
swapped =1
Third Iteration:
index = 1
list = 1 2 3 4 5 6 7 9 8
swapped = 1
list = 1 2 3 4 5 6 7 8 9
swapped = 1
index = 9
swapped = 0
Fourth iteration:
index = 1 to 9 the list remains unchaged only the index keeps incrementing to chek for the condition that
list[gap+index]>list[index], at the end
1 2 3 4 5 6 7 8 9
Average case:
INPUT: 1 4 7 2 9 7 8 0
list length = 8
LIST=1 4 7 2 9 7 8 0
index = 2, list = 1 0 7 2 9 7 8 4(After swapping)
Next Iteration:
index = 1, list= 1 0 7 2 9 7 8 4
index = 2 , list= 1 0 7 2 9 7 8 4
index = 3, list= 1 0 7 2 9 7 8 4
index = 4 , list= 1 0 7 2 9 7 8 4
Next Iteration:
index = 1, list= 1 0 7 2 9 7 8 4
index = 2, list= 1 0 7 2 9 7 8 4
index = 3,list= 1 0 7 2 9 7 8 4
index = 4, list= 1 0 7 2 9 7 8 4
swapped = 1
Next Iteration:
index = 1, list= 1 0 7 2 4 7 8 9
index = 2,list= 1 0 7 2 4 7 8 9
swapped = 1
index = 4, list= 1 0 4 2 7 7 8 9
index = 5, list= 1 0 4 2 7 7 8 9
index = 6,list= 1 0 4 2 7 7 8 9
Next Iteration:
index = 1, list = 0 1 4 2 7 7 8 9
swapped = 0
Next Iteration:
index = 1, list= 0 1 2 4 7 7 8 9
index = 2, list= 0 1 2 4 7 7 8 9
index = 3, list= 0 1 2 4 7 7 8 9
index = 4, list= 0 1 2 4 7 7 8 9
index = 5, list= 0 1 2 4 7 7 8 9
0 1 2 4 7 7 8 9
Empty Case:
Input:[ ] (Empty)
As gap<1 so, the while loop will not execute and the result will be the same empty list.
list_length =5
gap = 4,swapped = 0
index = 1,list= 1 2 3 4 5
Next Iteration:
index = 1,list= 1 2 3 4 5
index = 2,list= 1 2 3 4 5
Next Iteration:
index = 1,list= 1 2 3 4 5
index = 2,list= 1 2 3 4 5
index = 3,list= 1 2 3 4 5
Next Iteration:
index = 1,list= 1 2 3 4 5
index = 2,list= 1 2 3 4 5
index = 3,list= 1 2 3 4 5
index = 4,list= 1 2 3 4 5
index = 5,list= 1 2 3 4 5
1 2 3 4 5
Worst Case:
Input: 5 4 3 2 1
list_length = 5
gap = 4,swapped = 0
index = 1,list= 1 4 3 2 5
index = 2,list= 1 4 3 2 5
Next Iteration:
Index = 1 list=1 4 3 2 5
Next Iteration:
index = 1,ilst= 1 2 3 4 5
index = 2,list= 1 2 3 4 5
index = 3,list= 1 2 3 4 5
index =4,list= 1 2 3 4 5
index =5,list= 1 2 3 4 5
1 2 3 4 5