diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 2056ff0..47dfee2 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,8 +2,11 @@ + + + @@ -427,52 +449,56 @@ - + - - - + + + - + - - + + - + - - + + - + - - + + - + - - + + + - - + + + - - + + + - - + + + - + - + diff --git a/README.md b/README.md index d04695b..cc90238 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,8 @@ ## Menu `Sorted` -* [Sort-insertion](https://github.com/digital-technology-agency/math/wiki/Sort-insertion) +* [Sort insertion](https://github.com/digital-technology-agency/math/wiki/Sort-insertion) +* [Merge sort](https://github.com/digital-technology-agency/math/wiki/Merge-sort) `Mathematical` @@ -24,6 +25,7 @@ * [Permutation](https://github.com/digital-technology-agency/math/wiki/Permutation) * [Placements](https://github.com/digital-technology-agency/math/wiki/Placements) + ## Examples ### C from n to k diff --git a/pkg/sort/merge-sort.go b/pkg/sort/merge-sort.go new file mode 100644 index 0000000..7c14ebf --- /dev/null +++ b/pkg/sort/merge-sort.go @@ -0,0 +1,30 @@ +package sort + +func Merge(input []int) []int { + length := len(input) + if length < 2 { + return input + } + mid := length / 2 + return internalMerge(Merge(input[:mid]), Merge(input[mid:])) +} + +func internalMerge(left, right []int) []int { + result := []int{} + for len(left) > 0 && len(right) > 0 { + if left[0] < right[0] { + result = append(result, left[0]) + left = left[1:] + } else { + result = append(result, right[0]) + right = right[1:] + } + } + if len(left) > 0 { + result = append(result, left...) + } + if len(right) > 0 { + result = append(result, right...) + } + return result +} diff --git a/pkg/sort/merge-sort_test.go b/pkg/sort/merge-sort_test.go new file mode 100644 index 0000000..1751492 --- /dev/null +++ b/pkg/sort/merge-sort_test.go @@ -0,0 +1,35 @@ +package sort + +import ( + "log" + "testing" +) + +func TestMerge(t *testing.T) { + type args struct { + size int + } + tests := []struct { + name string + args args + want int + }{ + { + name: "Merge sort", + args: args{ + size: 100, + }, + want: 0, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + inputArray := randomArray(tt.args.size) + got := Merge(inputArray) + log.Printf(`Result: %v`, got) + if got[0] < tt.want { + t.Errorf("Merge() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/pkg/sort/sort-by-insertion_test.go b/pkg/sort/sort-by-insertion_test.go index 6f51631..2c3b5ad 100644 --- a/pkg/sort/sort-by-insertion_test.go +++ b/pkg/sort/sort-by-insertion_test.go @@ -2,6 +2,7 @@ package sort import ( "crypto/rand" + "log" "math/big" "testing" ) @@ -22,15 +23,17 @@ func TestInsertion(t *testing.T) { { name: "Sort by insertion", args: args{ - size: 10000, + size: 100, }, - want: 1, + want: 0, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { inputArray := randomArray(tt.args.size) - if got := Insertion(inputArray); got[0] != tt.want && got[0] > tt.want { + got := Insertion(inputArray) + log.Printf(`Result: %v`, got) + if got[0] < tt.want { t.Errorf("Insertion() = %v, want %v", got, tt.want) } }) @@ -42,7 +45,8 @@ func randomArray(size int) []int { n, _ := rand.Int(rand.Reader, big.NewInt(int64(size))) total := n.Int64() for total > 0 { - result = append(result, int(total)) + value, _ := rand.Int(rand.Reader, big.NewInt(int64(size))) + result = append(result, int(value.Int64())) total-- } return result