-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
07311b6
commit a6d641f
Showing
2 changed files
with
28 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package sorting | ||
|
||
type ShellSort struct{} | ||
|
||
func (i *ShellSort) Sort(arr []int) { | ||
// 1. start with a large gap, then reduce the gap until it becomes 1 | ||
// 2. perform insertion sort on the elements with the gap | ||
|
||
for gap := len(arr) / 2; gap > 0; gap /= 2 { | ||
// start from the gap element and move to the right | ||
for i := gap; i < len(arr); i++ { | ||
// store the current element | ||
temp := arr[i] | ||
j := i | ||
|
||
// run the insertion sort on the elements with the gap | ||
// shift the elements to the right until the correct position is found | ||
for j >= gap && arr[j-gap] > temp { | ||
arr[j] = arr[j-gap] | ||
j -= gap | ||
} | ||
|
||
// insert the temp element at the correct position | ||
arr[j] = temp | ||
} | ||
} | ||
} |
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