ADVANCED DATA STRUCTURES PROGRAM • LEVEL 12 — ADVANCED SORTING
Sort with Comb Sort
Learn how to sort with comb sort using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
No input required
Sample output
-44 -6 1 3 4 8 23 56
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
void print_values(const int values[], int count) { for (int i = 0; i < count; i++) printf("%d%c", values[i], i == count - 1 ? '\n' : ' '); }
int main(void)
{
int values[] = {8, 4, 1, 56, 3, -44, 23, -6}, gap = 8, swapped = 1;
while (gap > 1 || swapped) {
gap = gap * 10 / 13; if (gap < 1) gap = 1; swapped = 0;
for (int i = 0; i + gap < 8; i++) if (values[i] > values[i + gap]) { int t = values[i]; values[i] = values[i + gap]; values[i + gap] = t; swapped = 1; }
}
print_values(values, 8); return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
-44 -6 1 3 4 8 23 56
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Shrink a large comparison gap to remove turtles before the final adjacent pass.
- Display the computed result.
Shrink a large comparison gap to remove turtles before the final adjacent pass.
EFFICIENCY
Time and space complexity
Time complexity
Average O(n^2 / 2^p)
Auxiliary space
O(1)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
