-
Notifications
You must be signed in to change notification settings - Fork 302
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
shell_sort.c: Add Shell sort algorithm in C
This implementation uses half of the array's size as the gap. Then it keeps dividing it by 2. Closes #135
- Loading branch information
1 parent
59ab891
commit f0e3dbd
Showing
2 changed files
with
31 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#include <stdio.h> | ||
|
||
void shellsort(int size, int *arr) { | ||
// start with a big gap and reduce it | ||
for (int gap = size/2; gap > 0; gap = gap/2) { | ||
for (int i = gap; i < size; i = i+1) { | ||
int temp = arr[i]; | ||
|
||
int j; | ||
// shifting elements until the location for arr[i] is found | ||
for (j = i; j >= gap && arr[j-gap] > temp; j = j-gap) { | ||
arr[j] = arr[j-gap]; | ||
} | ||
arr[j] = temp; | ||
} | ||
} | ||
} | ||
|
||
int main() { | ||
int arr_size = 6; | ||
int arr[6] = {10, 9, 8, 7, 6, 5 }; | ||
shellsort(arr_size, arr); | ||
|
||
for (int i = 0; i < arr_size; i++) { | ||
printf("%d ", arr[i]); | ||
} | ||
printf("\n"); | ||
|
||
return 0; | ||
} |