Skip to content

Commit

Permalink
factorial
Browse files Browse the repository at this point in the history
Signed-off-by: deemak <deema_k@mail.ru>
  • Loading branch information
deemakuzovkin committed Jun 11, 2021
1 parent 4d1966d commit 96836b5
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 31 deletions.
78 changes: 52 additions & 26 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand All @@ -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
Expand Down
30 changes: 30 additions & 0 deletions pkg/sort/merge-sort.go
Original file line number Diff line number Diff line change
@@ -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
}
35 changes: 35 additions & 0 deletions pkg/sort/merge-sort_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
12 changes: 8 additions & 4 deletions pkg/sort/sort-by-insertion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sort

import (
"crypto/rand"
"log"
"math/big"
"testing"
)
Expand All @@ -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)
}
})
Expand All @@ -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
Expand Down

0 comments on commit 96836b5

Please sign in to comment.