Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a957e45

Browse files
committed
fixes #51
1 parent 62e6cd8 commit a957e45

File tree

2 files changed

+104
-2
lines changed

2 files changed

+104
-2
lines changed

‎algorithm/README.md

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ func binaySearch(nums[]int, left int, right int, target int,index int) int{
2222
if(nums[mid]<target){
2323
left=mid+1
2424
index=left
25-
return helper(nums,left,right,target,index)
25+
return binaySearch(nums,left,right,target,index)
2626
}else if(nums[mid]>target){
2727
right=mid-1
28-
return helper(nums,left,right,target,index)
28+
return binaySearch(nums,left,right,target,index)
2929
}else{
3030
return mid
3131
}
@@ -36,6 +36,51 @@ func binaySearch(nums[]int, left int, right int, target int,index int) int{
3636
- Merge Sort
3737
- Quick Sort
3838

39+
Code Sample
40+
41+
```xml
42+
43+
44+
func QuickSort(arr []int) []int {
45+
newArr := make([]int, len(arr))
46+
47+
for i, v := range arr {
48+
newArr[i] = v
49+
}
50+
51+
sort(newArr, 0, len(arr)-1)
52+
53+
return newArr
54+
}
55+
56+
func sort(arr []int, start, end int) {
57+
if (end - start) < 1 {
58+
return
59+
}
60+
61+
pivot := arr[end]
62+
splitIndex := start
63+
64+
for i := start; i < end; i++ {
65+
if arr[i] < pivot {
66+
temp := arr[splitIndex]
67+
68+
arr[splitIndex] = arr[i]
69+
arr[i] = temp
70+
71+
splitIndex++
72+
}
73+
}
74+
75+
arr[end] = arr[splitIndex]
76+
arr[splitIndex] = pivot
77+
78+
sort(arr, start, splitIndex-1)
79+
sort(arr, splitIndex+1, end)
80+
}
81+
82+
```
83+
3984
## Some Problems
4085
- Longest Common Prefix
4186
- Median of Two Sorted Arrays

‎algorithm/algos.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package algorithm
2+
3+
func binaySearch(nums[]int, left int, right int, target int,index int) int{
4+
if(left>right){
5+
return index
6+
}
7+
mid:=(left+right)/2
8+
if(nums[mid]<target){
9+
left=mid+1
10+
index=left
11+
return binaySearch(nums,left,right,target,index)
12+
}else if(nums[mid]>target){
13+
right=mid-1
14+
return binaySearch(nums,left,right,target,index)
15+
}else{
16+
return mid
17+
}
18+
}
19+
20+
21+
func QuickSort(arr []int) []int {
22+
newArr := make([]int, len(arr))
23+
24+
for i, v := range arr {
25+
newArr[i] = v
26+
}
27+
28+
sort(newArr, 0, len(arr)-1)
29+
30+
return newArr
31+
}
32+
33+
func sort(arr []int, start, end int) {
34+
if (end - start) < 1 {
35+
return
36+
}
37+
38+
pivot := arr[end]
39+
splitIndex := start
40+
41+
for i := start; i < end; i++ {
42+
if arr[i] < pivot {
43+
temp := arr[splitIndex]
44+
45+
arr[splitIndex] = arr[i]
46+
arr[i] = temp
47+
48+
splitIndex++
49+
}
50+
}
51+
52+
arr[end] = arr[splitIndex]
53+
arr[splitIndex] = pivot
54+
55+
sort(arr, start, splitIndex-1)
56+
sort(arr, splitIndex+1, end)
57+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /