# Comb Sort

Brush sort is a fairly direct orchestrating computation at first arranged by Wlodzimierz Dobosiewicz and Artur Borowy in 1980, later rediscovered (and given the name "Combsort") by Stephen Lacey and Richard Box in 1991.

Comb sort enhances bubble sort similarly that Shellsort enhances inclusion sort.

nist.gov's "decreasing augmentation sort" definition specifies the term 'brush sort' as picturing iterative passes of the information, "where the teeth of a brush contact;" the previous term is connected to Don Knuth.

## Algorithm:

The fundamental thought is to wipe out turtles, or little qualities close to the furthest limit of the rundown, since in an air pocket sort these sluggish the arranging down immensely.

Bunnies, huge qualities around the start of the rundown, don't represent an issue in bubble sort.

In bubble sort, when any two components are looked at, they generally have a hole (distance from one another) of 1.

The fundamental thought of brush sort is that the hole can be considerably more than 1.

The inward circle of air pocket sort, which does the real trade, is changed to such an extent that the hole between traded components goes down (for every emphasis of external circle) in strides of a "shrivel factor" k: [ n/k, n/k2, n/k3, ..., 1 ].

The hole begins as the length of the rundown n being arranged partitioned by the psychologist factor k (by and large 1.3; see beneath) and one pass of the previously mentioned changed bubble sort is applied with that hole.

Then the hole is separated by the psychologist factor once more, the rundown is arranged with this new hole, and the cycle rehashes until the hole is 1.

As of now, brush sort keeps utilizing a hole of 1 until the rundown is completely arranged. The last phase of the sort is consequently comparable to an air pocket sort, yet at this point most turtles have been managed, so an air pocket sort will be proficient.

The psychologist factor significantly affects the proficiency of brush sort. k = 1.3 has been recommended as an ideal psychologist factor by the writers of the first article after observational testing on north of 200,000 irregular records. A worth too little dials the calculation back by making superfluously numerous correlations, while a worth too huge neglects to really manage turtles, causing it to require many passes with 1 hole size.

The example of continued arranging passes with diminishing holes is like Shellsort, yet in Shellsort the exhibit is arranged totally each give prior to going to the following littlest hole.

Brush sort's passes don't totally sort the components.

This is the explanation that Shellsort hole groupings have a bigger ideal psychologist variable of around 2.2.

## Psuedocode:

``````function combsort(array input) is
hole := input.size//Initialize hole size
contract := 1.3//Set the hole shrivel factor

// Update the hole an incentive for a next brush
hole := floor(gap/recoil)
on the off chance that hole ≤ 1,
hole := 1
arranged := valid//If there are no trades this pass, we are finished
end if

// A solitary "brush" over the info list
I := 0
circle while I + hole < input.size//See Shell sort for a comparable thought
in the event that input[i] > input[i+gap],
swap(input[i], input[i+gap])
arranged := false
// Assuming this task never occurs inside the circle,
// then, at that point, there have been no trades and the rundown is arranged.
end if

I := I + 1
end circle
end circle
end function
``````

//program

``````//python code
from math import floor
def combsort_inplace(data):
length = len(data)
shrink = 1.3
hole = length
arranged = False
while not arranged:
hole = floor(gap/shrivel)
if hole <= 1:
arranged = True
hole = 1
# comparable to 'I = 0; while (I + hole) < length: ...{loop body}... I += 1'
for I in range(length - hole):
sm = hole + i
in the event that data[i] > data[sm]:
# since Python is exceptionally great, this achieves the trade
data[i], data[sm] = data[sm], data[i]
arranged = False

def combsort(data):
length = len(data)
shrink = 1.3
hole = length
out = list(data)
is_sorted = False
while not is_sorted:
hole = floor(gap/shrivel)
in the event that hole <= 1:
is_sorted = True
hole = 1
for i in range(length - hole):
sm = hole + i
if out[i] > out[sm]:
out[i], out[sm] = out[sm], out[i]
is_sorted = False
return out
``````
• class: Sorting calculation
• Information structure:  Array
• Most pessimistic scenario performance:           O(n^{2})}O(n^{2})
• Best-case performance :{\displaystyle \Theta (n\log n)}\Theta(n \log n)
• Normal performance:    {\displaystyle \Omega           (n^{2}/2^{p})}\Omega(n^2/2^p), where p is the quantity of increments
• Most pessimistic scenario space complexity: O(1)}O(1)

## Steps for combo sort Algorithm:

Instate variable hole as the size of the cluster and variable traded as evident.

1.Proclaim the consistent variable SHRINK_FACTOR as 1.3.

2.While the hole isn't 1 or traded is set to true do the accompanying:

4.Set hole as (int)gap/SHRINK_FACTOR.

5.For each component in the reach 0 to n - hole do the accompanying - if A[i] > A[i+gap], swap(A[i], A[i+gap]) and set traded to valid.

## Complexity of Combo Sort Algorithm:

Normal Case

The time intricacy is of the request for [Big Theta]: O(n2/2p) where p is the quantity of additions.

Worst case:

The most pessimistic scenario time intricacy is [Big O]: O(n2).

Best Case

The best-case happens when the cluster is arranged or almost arranged. The best-case time intricacy is [Big Omega]: O(nlogn). It is a critical improvement over the best-case time intricacy of air pocket sort.

Space Complexity

Space Complexity for this calculation is O(n) in light of the fact that the brush sort calculation requires no extra space other than brief factors.

//JAVA PROGRAM CODE

``````// Java program for implementation of Comb Sort
class CombSorting
{
// To find gap between elements
int getNextGap(int gap1)
{
// Shrink gap by Shrink factor
Gap1 = (gap1*10)/13;
if (gap1 < 1)
return 1;
return gap1;
}

// Function to sort arr[] using Comb Sort
void sort(int arr[])
{
int n1 = arr.length;

// initialize gap
int gap = n1;

// Initialize swapped as true to make sure that
// loop runs
boolean swapped = true;

// Keep running while gap is more than 1 and last
// iteration caused a swap
while (gap1 != 1 || swapped == true)
{
// Find next gap
Gap1 = getNextGap(gap1);

// Initialize swapped as false so that we can
// check if swap happened or not
swapped = false;

// Compare all elements with current gap
for (int i=0; i<n-gap; i++)
{
if (arr[i] > arr[i+gap])
{
// Swap arr[i] and arr[i+gap]
int temp = arr[i];
arr[i] = arr[i+gap];
arr[i+gap] = temp;

// Set swapped
swapped = true;
}
}
}
}

// Driver method
public static void main(String args[])
{
CombSort ob = new CombSort();
int arr[] = {enter list of elements};
ob.sort(arr);

System.out.println("sorted array");
for (int i=0; i<arr.length; ++i)
System.out.print(arr[i] + " ");

}
}
``````