Sie sind auf Seite 1von 6

EGERTON UNIVERSITY

 DEPARTMENT OF COMPUTER SCIENCE
COMP 302

ANALYSIS OF COMB SORT ALGORITHM

GROUP 11
MEMBERS:
1. Edward Mungai S13/20025/08
2. Irene Njuki S13/20043/08
3. Grace Mburu SP13/ 20465/08
4. Eric Wambugu
5. Samuel Wanjiku S13/20031/08

SUBMITED TO: egertondatastructures@gmail.com

Copyright Notice: This report and all codes attached is distributed


under GNU Public licence or higher. You are legally allowed to
copy, modify or redistribute part(s) of the report, the java, C and
php programs attached.
BACKGROUND INFORMATION :
Bubble sort is inarguably the simplest sorting algorithm in programming. In
bubble sort, large values around the beginning of the list do not pose a problem
as they are shifted to the end pretty fast. Small values around the end of the list
however pose the greatest problem. They slow down the sorting tremendously.
They are called turtles.

The aim of Comb sort is to eliminate this turtles.

COMB SORT:
In bubble sort, when any two elements are compared, they always have a gap
(distance from each other) of 1. The basic idea of comb sort is that the gap can be
much more than one..Another algorithm that uses this idea is Shell sort. The gap
starts out as the length of the list divided by a shrink factor. The ideal shrink
factor is 1.3 . This value is crucial because a small value means that it would be
slower for the gap to degrade to 1, slowing down the process, while a large value
will not effectively kill turtles.
Shrink factor is calculated as :
1
1
(1− phi ) =1.247330950103979
e
where phi is a golden ratio=1.61803399
1+ √ 5
(phi is Mathematically calcutated as: )
2
The initial gap, therefore is list.length/1.3. You then compare values from the
beginning of the list and those gap value away and swap accordingly. This idea
can practically kill turtles because some of them would "jump" to the beginning of
the list early on. The gap is then lessened by the shrink factor until it becomes 1
when the algorithm degrades to a bubble sort. The bubble sort is however
efficient as the turtles have been killed.

ALGORITHM :
combsort(list[])
{
gap<-list.length; //initialize gap to the array size
swapped<-true; // a boolean value to test if swapping took place
while(gap>1 || swapped) //loop until swapping is complete
if (gap > 1)
gap <- (int) (gap / 1.247330950103979); //calculate the new gap
else if(gap<1)
gap<-1; //The smallest gap can only be 1

i <- 0;
swapped <- false; //nothing has been swapped
while(i+gap<list.length)
if(list[i]>list[i+gap]) //swap the values
temp<-list[i];
list[i]<-list[i+gap];
list[i+gap]<-temp;
swapped<-true;
i<-i+1;
}

EXAMPLE:
If we take an unsorted array 5, 7, 10, 3, 1, 4, 8, 2, 6,0 ,9. The shrink factor is 1.3 and the
size of the list is 11.

5 7 10 3 1 4 8 2 6 0 9

Iteration 1 gap=11/1.3=8

5 7 10 3 1 4 8 2 6 0 9

5 0 10 3 1 4 8 2 6 7 9

5 0 9 3 1 4 8 2 6 7 10

Iteration 2 gap=8/1.3=6

5 0 9 3 1 4 8 2 6 7 10

5 0 9 3 1 4 8 2 6 7 10

5 0 6 3 1 4 8 2 9 7 10

5 0 6 3 1 4 8 2 9 7 10

5 0 6 3 1 4 8 2 9 7 10

Iteration 3 gap=6/1.3=4

1 0 6 3 5 4 8 2 9 7 10

1 0 6 3 5 4 8 2 9 7 10

1 0 6 3 5 4 8 2 9 7 10

1 0 6 2 5 4 8 3 9 7 10

1 0 6 2 5 4 8 3 9 7 10
1 0 6 2 5 4 8 3 9 7 10

1 0 6 2 5 4 8 3 9 7 10

Iteration 4 gap=4/1.3=3

1 0 6 2 5 4 8 3 9 7 10

1 0 6 2 5 4 8 3 9 7 10

1 0 4 2 5 6 8 3 9 7 10

1 0 4 2 5 6 8 3 9 7 10

1 0 4 2 3 6 8 5 9 7 10

1 0 4 2 3 6 8 5 9 7 10

1 0 4 2 3 6 7 5 9 8 10

1 0 4 2 3 6 7 5 9 8 10

Iteration 5 gap=3/1.3=2

1 0 4 2 3 6 7 5 9 8 10

1 0 4 2 3 6 7 5 9 8 10

1 0 3 2 4 6 7 5 9 8 10

1 0 3 2 4 6 7 5 9 8 10

1 0 3 2 4 6 7 5 9 8 10

1 0 3 2 4 5 7 6 9 8 10

1 0 3 2 4 5 7 6 9 8 10

1 0 3 2 4 5 7 6 9 8 10
1 0 3 2 4 5 7 6 9 8 10

Iteration 6 gap=2/1.3=1

0 1 3 2 4 5 7 6 9 8 10

0 1 3 2 4 5 7 6 9 8 10

0 1 2 3 4 5 7 6 9 8 10

0 1 2 3 4 5 7 6 9 8 10

0 1 2 3 4 5 7 6 9 8 10

0 1 2 3 4 5 7 6 9 8 10

0 1 2 3 4 5 6 7 9 8 10

0 1 2 3 4 5 6 7 9 8 10

0 1 2 3 4 5 6 7 8 9 10

0 1 2 3 4 5 6 7 8 9 10

Iteration 7.
Since sorting is done no swaps will be made in this iteration and the algorithm ends.

For purposes of practice only, we have included sample programs written in different  
programming languages to accomplish this algorithm. They include a JAVA,C and PHP  
programs.

ANALYSIS OF THE ALGORITHM:
Bubblesort sorts 10,000 items in 1.36 seconds while Combsort. combsort sorts
10,000 items in 0.0042 seconds, only 1.1 times longer than C++ quicksort. It's amazing
that such a simple change to bubblesort makes it nearly as good as quicksort. What's
more, combsort doesn't need any special code to keep from degenerating in the presence
of already sorted lists.

APPLICATION OF THE SORTING ALGORITHM:
Just like any other algorithm, Comb sort is used to sort values. In speed, it rivals
algorithms like Quicksort. Comb sort therefore finds application in areas where
speed is crucial. Some of this applications include :
1. Sorting results of a search in search engines – Results rendered by search
engines are sorted according the page rank in a descending order. The
speed in which results are returning is very important to the engine and
hence the use of Comb sort.
2. Preparation of Account Statements- Account statements are ordered
according to the date of transaction and the check numbers.
3. Job scheduling- Jobs in a CPU are arranged according to their priority and
age. This helps to determine which job is executed when without delaying
others or being unfair to others. Comb sort is used to carry out this sorting.

Das könnte Ihnen auch gefallen