0% found this document useful (0 votes)
343 views8 pages

Comb Sort: Submitted BY

Comb sort is a sorting algorithm that improves upon bubble sort. It works by starting with a large gap between elements and shrinking the gap over multiple iterations until it reaches a gap of 1, at which point it becomes a bubble sort. This allows it to avoid turtles that slow bubble sort. It has an average case performance of O(nlogn) but worst case of O(n^2). Examples are provided showing it sorting lists in best, average, and worst case scenarios.

Uploaded by

rida fatima
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
343 views8 pages

Comb Sort: Submitted BY

Comb sort is a sorting algorithm that improves upon bubble sort. It works by starting with a large gap between elements and shrinking the gap over multiple iterations until it reaches a gap of 1, at which point it becomes a bubble sort. This allows it to avoid turtles that slow bubble sort. It has an average case performance of O(nlogn) but worst case of O(n^2). Examples are provided showing it sorting lists in best, average, and worst case scenarios.

Uploaded by

rida fatima
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

COMB SORT

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)

Set gap to list length

set shrink factor~1.3

set swapped as true

while (gap>1 or swapped)

if gap>1 then

gap=floor of(gap/shrink factor)

set swapped as false

initialize index to 1

while (gap + index) <= list length

if list[index] > list[index+ gap] then

swap list[index] <-> list[index +gap]

set swapped as true

increment index

Average Case Performance: O(nlgn)

Worst Case Performance: O(n²)

Space Complexity: O(1)


*****Applying Comb Sort On A List Of Numbers *****

Reverse Sorted scenario:(Worst case)

INPUT: 9 8 7 6 5 4 3 2 1

list_length = 9

The different stages are:

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

When index = 5 (gap + Index) > list length so,

gap =4,swapped = 0

For index = 1 to 2, list remains unchanged, at index 3

List = 2 1 3 6 5 4 7 9 8

swapped = 1

List remains unchanged for index = 4 & 5,for index = 6

gap = 3
swapped = 0

List remains unchanged for index = 1 to 6,for index = 7

gap =2,swapped =0

For index = 1 to 3 list remains unchanged, for index = 4

list = 2 1 3 4 5 6 7 9 8

swapped =1

for index = 5 to 7 list remains unchanged,For index = 8, gap = 1, swapped =0

Third Iteration:

index = 1

list = 1 2 3 4 5 6 7 9 8

swapped = 1

For index = 2 to 7 the list remains unchanged, For index = 8

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

The sorted list is:

1 2 3 4 5 6 7 8 9

Average case:

INPUT: 1 4 7 2 9 7 8 0

list length = 8

The different stages are:

gap = 6,swapped = 0,Index = 1

LIST=1 4 7 2 9 7 8 0
index = 2, list = 1 0 7 2 9 7 8 4(After swapping)

index = 3,gap = 4,swapped = 0

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

index = 5, gap = 3,swapped = 0

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

index =5, list = 1 0 7 2 4 7 8 9 (After swapping)

swapped = 1

index =6,gap = 2, swapped =0

Next Iteration:

index = 1, list= 1 0 7 2 4 7 8 9

index = 2,list= 1 0 7 2 4 7 8 9

index = 3,list= 1 0 4 2 7 7 8 9(After swapping)

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

index = 7,gap = 1,swapped =0

Next Iteration:

index = 1, list = 0 1 4 2 7 7 8 9

Index =2, list= 0 1 4 2 7 7 8 9

index =3, list = 0 1 2 4 7 7 8 9(After swapping)


swapped = 1

index =4, list= 0 1 2 4 7 7 8 9

index =5, list= 0 1 2 4 7 7 8 9

index =6, list= 0 1 2 4 7 7 8 9

index =7, list= 0 1 2 4 7 7 8 9

index =8, list= 0 1 2 4 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

index =6,l ist= 0 1 2 4 7 7 8 9

index =7, list= 0 1 2 4 7 7 8 9

index =8, list= 0 1 2 4 7 7 8 9

The sorted list is therefore :

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.

Already sorted input(Best case):


Input: 1 2 3 4 5

list_length =5

The different stages are:

gap = 4,swapped = 0
index = 1,list= 1 2 3 4 5

index = 2,gap = 3,swapped = 0

Next Iteration:

index = 1,list= 1 2 3 4 5

index = 2,list= 1 2 3 4 5

index = 3, gap =2,swapped = 0

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,gap = 1,swapped = 0

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

The sorted list is:

1 2 3 4 5

Worst Case:

Input: 5 4 3 2 1

list_length = 5

The different stages are:

gap = 4,swapped = 0

index = 1,list = 1 4 3 2 5(After swapping)

index = 2,gap = 3,swapped = 0


Next Iteration:

index = 1,list= 1 4 3 2 5

index = 2,list= 1 4 3 2 5

index = 3,gap = 2,swapped = 0

Next Iteration:

Index = 1 list=1 4 3 2 5

index = 2,list= 1 2 3 4 5(After Swapping)

index =3, list= 1 2 3 4 5

index = 4,gap = 1,swapped = 0

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

The sorted list is therefore:

1 2 3 4 5

You might also like