Last active
March 31, 2024 13:24
-
-
Save w33ladalah/6f51b086520e0bf87476619001e3c11c to your computer and use it in GitHub Desktop.
Shell Short di C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Shell Sort in C programming | |
#include <stdio.h> | |
// Shell sort | |
void shellSort(int array[], int n) { | |
// Urutkan angka-angka dengan interval n/2, kemudian n/4 atau (n/2)/2, n/8, dan seterusnya | |
for (int interval = n / 2; interval > 0; interval /= 2) { | |
printf("%d \n", interval); | |
for (int i = interval; i < n; i += 1) { | |
int temp = array[i]; | |
int j; | |
for (j = i; j >= interval && array[j - interval] > temp; j -= interval) { | |
array[j] = array[j - interval]; | |
} | |
array[j] = temp; | |
} | |
} | |
} | |
// Tampilkan array | |
void printArray(int array[], int size) { | |
for (int i = 0; i < size; ++i) { | |
printf("%d ", array[i]); | |
} | |
printf("\n"); | |
} | |
int main() { | |
int data[] = {9, 8, 3, 7, 5, 6, 4, 1, 10}; | |
int size = sizeof(data) / sizeof(data[0]); | |
shellSort(data, size); | |
printf("Sorted array: \n"); | |
printArray(data, size); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment