chore(C): add shell sort (#678)

pull/699/head
Anish 2022-02-02 11:03:55 +05:30 committed by GitHub
parent 8fcdba9741
commit 5bfa1de2fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 0 deletions

View File

@ -51,6 +51,7 @@
- [Heap Sort](sorting/heap-sort.c)
- [Selection Sort](sorting/selection-sort.c)
- [Quick Sort](sorting/quick-sort.c)
- [Shell Sort](sorting/shell-sort.c)
## Strings

View File

@ -0,0 +1,51 @@
#include <stdio.h>
// Shell Sort
void ShellSort(int a[], int n)
{
int i, j, k, temp;
// Rearrange elements at each n/2, n/4, n/8, ... intervals
for (i = n / 2; i > 0; i /= 2)
{
for (j = i; j < n; j++)
{
temp = a[j];
for (k = j; k >= i && a[k - i] > temp; k -= i)
{
a[k] = a[k - i];
}
a[k] = temp;
}
}
}
// Prints the array
void printArray(int a[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("%d ", a[i]);
}
}
int main()
{
int a[] = {64, 25, 12, 22, 11, 90};
int n = sizeof(a) / sizeof(a[0]);
printf("Array before sorting:\n");
printArray(a, n);
ShellSort(a, n);
printf("\nArray after sorting:\n");
printArray(a, n);
return 0;
}
/*
Output:
Array before sorting:
64 25 12 22 11 90
Array after sorting:
11 12 22 25 64 90
Time Complexity: O(n log n)
*/