-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Insertion sort #3
Open
calee0219
wants to merge
3
commits into
goSTL:master
Choose a base branch
from
calee0219:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,119 @@ | ||
package sort | ||
|
||
import "reflect" | ||
|
||
// This is a custom Merge Sort | ||
// You MUST define the compare function by yourself | ||
// compare function type: func(i,j interface{}) bool | ||
// | ||
// input: func([]interface{}, func(i,j interface{}) bool) []interface{} | ||
// | ||
// output slice and won't change the origin one | ||
// data = InsertionSort(data, cmp), if you want to change origin slice | ||
func InsertionSort(data interface{}, cmp func(i, j interface{}) bool) []interface{} { | ||
value := reflect.ValueOf(data) | ||
dataS := make([]interface{}, value.Len()) | ||
len := value.Len() | ||
for a := 0; a < len; a++ { | ||
dataS[a] = value.Index(a).Interface() | ||
} | ||
for i := 1; i < len; i++ { | ||
tmp := dataS[i] | ||
j := i - 1 | ||
for ; j >= 0 && cmp(dataS[j], tmp); j-- { | ||
dataS[j+1] = dataS[j] | ||
} | ||
dataS[j+1] = tmp | ||
} | ||
return dataS | ||
} | ||
|
||
// INT | ||
// Insertion Sort in Int type, sorting from small to big | ||
// | ||
// Input slice func([]int) | ||
// | ||
// No output, just change origin slice | ||
func InsertionSortInt(dataS []int) { | ||
len := len(dataS) | ||
for i := 1; i < len; i++ { | ||
tmp := dataS[i] | ||
j := i - 1 | ||
for ; j >= 0 && dataS[j] > tmp; j-- { | ||
dataS[j+1] = dataS[j] | ||
} | ||
dataS[j+1] = tmp | ||
} | ||
} | ||
|
||
// INT32 | ||
// Insertion Sort in Int32 type, sorting from small to big | ||
// | ||
// Input slice func([]int32) | ||
// | ||
// No output, just change origin slice | ||
func InsertionSortInt32(dataS []int32) { | ||
len := len(dataS) | ||
for i := 1; i < len; i++ { | ||
tmp := dataS[i] | ||
j := i - 1 | ||
for ; j >= 0 && dataS[j] > tmp; j-- { | ||
dataS[j+1] = dataS[j] | ||
} | ||
dataS[j+1] = tmp | ||
} | ||
} | ||
|
||
// INT64 | ||
// Insertion Sort in Int64 type, sorting from small to big | ||
// | ||
// Input slice func([]int64) | ||
// | ||
// No output, just change origin slice | ||
func InsertionSortInt64(dataS []int64) { | ||
len := len(dataS) | ||
for i := 1; i < len; i++ { | ||
tmp := dataS[i] | ||
j := i - 1 | ||
for ; j >= 0 && dataS[j] > tmp; j-- { | ||
dataS[j+1] = dataS[j] | ||
} | ||
dataS[j+1] = tmp | ||
} | ||
} | ||
|
||
// FLOAT32 | ||
// Insertion Sort in Float32 type, sorting from small to big | ||
// | ||
// Input slice func([]float32) | ||
// | ||
// No output, just change origin slice | ||
func InsertionSortFloat32(dataS []float32) { | ||
len := len(dataS) | ||
for i := 1; i < len; i++ { | ||
tmp := dataS[i] | ||
j := i - 1 | ||
for ; j >= 0 && dataS[j] > tmp; j-- { | ||
dataS[j+1] = dataS[j] | ||
} | ||
dataS[j+1] = tmp | ||
} | ||
} | ||
|
||
// FLOAT64 | ||
// Insertion Sort in Float64 type, sorting from small to big | ||
// | ||
// Input slice func([]float64) | ||
// | ||
// No output, just change origin slice | ||
func InsertionSortFloat64(dataS []float64) { | ||
len := len(dataS) | ||
for i := 1; i < len; i++ { | ||
tmp := dataS[i] | ||
j := i - 1 | ||
for ; j >= 0 && dataS[j] > tmp; j-- { | ||
dataS[j+1] = dataS[j] | ||
} | ||
dataS[j+1] = tmp | ||
} | ||
} |
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 @@ | ||
package sort |
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,18 @@ | ||
package sort_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/goSTL/sort" | ||
) | ||
|
||
func ExampleInsertionSortInt() { | ||
sample := []int{3, 138, 1, 674, 213, 23, 5, 2} | ||
fmt.Println(sample) | ||
|
||
sort.InsertionSortInt(sample) | ||
fmt.Println(sample) | ||
//Output: | ||
//[3 138 1 674 213 23 5 2] | ||
//[1 2 3 5 23 138 213 674] | ||
} |
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,21 @@ | ||
package sort_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/goSTL/sort" | ||
) | ||
|
||
func ExampleInsertionSort() { | ||
stu := []student{{5, "sam"}, {3, "lily"}, {7, "jacky"}, {1, "willy"}, {2, "steve"}, {3, "kally"}, {1, "gay"}, {-1, "10"}} | ||
fmt.Println("original: \t", stu) | ||
|
||
out := sort.InsertionSort(stu, cmp) | ||
fmt.Println("[]interface{}: \t", out) | ||
|
||
stu2 := make([]student, len(out)) | ||
for a := 0; a < len(out); a++ { | ||
stu2[a] = out[a].(student) | ||
} | ||
fmt.Println("[]student: \t", stu2) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't it Insertion Sort ?