Skip to content

Commit 2fa8452

Browse files
authored
Merge pull request deutranium#75 from adithyaakrishna/implement-comb-sort
Implement Comb Sort
2 parents 07e714a + f9b6419 commit 2fa8452

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed

sortingAlgo/combSort/combSort.cpp

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// C++ implementation of Comb Sort
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
// To find gap between elements
6+
int getNextGap(int gap)
7+
{
8+
// Shrink gap by Shrink factor
9+
gap = (gap * 10) / 13;
10+
11+
if (gap < 1)
12+
return 1;
13+
return gap;
14+
}
15+
16+
// Function to sort a[0..n-1] using Comb Sort
17+
void combSort(int a[], int n)
18+
{
19+
// Initialize gap
20+
int gap = n;
21+
22+
// Initialize swapped as true to make sure that
23+
// loop runs
24+
bool swapped = true;
25+
26+
// Keep running while gap is more than 1 and last
27+
// iteration caused a swap
28+
while (gap != 1 || swapped == true)
29+
{
30+
// Find next gap
31+
gap = getNextGap(gap);
32+
33+
// Initialize swapped as false so that we can
34+
// check if swap happened or not
35+
swapped = false;
36+
37+
// Compare all elements with current gap
38+
for (int i = 0; i < n - gap; i++)
39+
{
40+
if (a[i] > a[i + gap])
41+
{
42+
swap(a[i], a[i + gap]);
43+
swapped = true;
44+
}
45+
}
46+
}
47+
}
48+
49+
// Driver program
50+
int main()
51+
{
52+
int a[] = {8, 4, 1, 56, 3, -44, 23, -6, 28, 0};
53+
int n = sizeof(a) / sizeof(a[0]);
54+
55+
combSort(a, n);
56+
57+
printf("Sorted array: \n");
58+
for (int i = 0; i < n; i++)
59+
printf("%d ", a[i]);
60+
61+
return 0;
62+
}

sortingAlgo/combSort/combSort.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// C# program for implementation of Comb Sort
2+
using System;
3+
4+
class GFG
5+
{
6+
// To find gap between elements
7+
static int getNextGap(int gap)
8+
{
9+
// Shrink gap by Shrink factor
10+
gap = (gap*10)/13;
11+
if (gap < 1)
12+
return 1;
13+
return gap;
14+
}
15+
16+
// Function to sort arr[] using Comb Sort
17+
static void sort(int []arr)
18+
{
19+
int n = arr.Length;
20+
21+
// initialize gap
22+
int gap = n;
23+
24+
// Initialize swapped as true to
25+
// make sure that loop runs
26+
bool swapped = true;
27+
28+
// Keep running while gap is more than
29+
// 1 and last iteration caused a swap
30+
while (gap != 1 || swapped == true)
31+
{
32+
// Find next gap
33+
gap = getNextGap(gap);
34+
35+
// Initialize swapped as false so that we can
36+
// check if swap happened or not
37+
swapped = false;
38+
39+
// Compare all elements with current gap
40+
for (int i=0; i<n-gap; i++)
41+
{
42+
if (arr[i] > arr[i+gap])
43+
{
44+
// Swap arr[i] and arr[i+gap]
45+
int temp = arr[i];
46+
arr[i] = arr[i+gap];
47+
arr[i+gap] = temp;
48+
49+
// Set swapped
50+
swapped = true;
51+
}
52+
}
53+
}
54+
}
55+
56+
// Driver method
57+
public static void Main()
58+
{
59+
int []arr = {8, 4, 1, 56, 3, -44, 23, -6, 28, 0};
60+
sort(arr);
61+
62+
Console.WriteLine("sorted array");
63+
for (int i=0; i<arr.Length; ++i)
64+
Console.Write(arr[i] + " ");
65+
66+
}
67+
}

sortingAlgo/combSort/combSort.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Java program for implementation of Comb Sort
2+
class CombSort
3+
{
4+
// To find gap between elements
5+
int getNextGap(int gap)
6+
{
7+
// Shrink gap by Shrink factor
8+
gap = (gap*10)/13;
9+
if (gap < 1)
10+
return 1;
11+
return gap;
12+
}
13+
14+
// Function to sort arr[] using Comb Sort
15+
void sort(int arr[])
16+
{
17+
int n = arr.length;
18+
19+
// initialize gap
20+
int gap = n;
21+
22+
// Initialize swapped as true to make sure that
23+
// loop runs
24+
boolean swapped = true;
25+
26+
// Keep running while gap is more than 1 and last
27+
// iteration caused a swap
28+
while (gap != 1 || swapped == true)
29+
{
30+
// Find next gap
31+
gap = getNextGap(gap);
32+
33+
// Initialize swapped as false so that we can
34+
// check if swap happened or not
35+
swapped = false;
36+
37+
// Compare all elements with current gap
38+
for (int i=0; i<n-gap; i++)
39+
{
40+
if (arr[i] > arr[i+gap])
41+
{
42+
// Swap arr[i] and arr[i+gap]
43+
int temp = arr[i];
44+
arr[i] = arr[i+gap];
45+
arr[i+gap] = temp;
46+
47+
// Set swapped
48+
swapped = true;
49+
}
50+
}
51+
}
52+
}
53+
54+
// Driver method
55+
public static void main(String args[])
56+
{
57+
CombSort ob = new CombSort();
58+
int arr[] = {8, 4, 1, 56, 3, -44, 23, -6, 28, 0};
59+
ob.sort(arr);
60+
61+
System.out.println("sorted array");
62+
for (int i=0; i<arr.length; ++i)
63+
System.out.print(arr[i] + " ");
64+
65+
}
66+
}

0 commit comments

Comments
 (0)