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 7d35a60

Browse files
committed
merge
1 parent 2946759 commit 7d35a60

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

‎algorithm/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,45 @@ func findCss(nums [] int, start int, end int,mid int) int{
221221
```
222222
- Majority Element
223223
- Kth Largest Element in an Array
224+
225+
Code sample
226+
227+
```xml
228+
229+
func findKthLargest(nums []int, k int) int {
230+
sort(nums,0,len(nums)-1)
231+
return nums[len(nums)-k]
232+
}
233+
234+
func sort(arr []int, start, end int) {
235+
if (end - start) < 1 {
236+
return
237+
}
238+
239+
pivot := arr[end]
240+
splitIndex := start
241+
242+
for i := start; i < end; i++ {
243+
if arr[i] < pivot {
244+
temp := arr[splitIndex]
245+
246+
arr[splitIndex] = arr[i]
247+
arr[i] = temp
248+
249+
splitIndex++
250+
}
251+
}
252+
253+
arr[end] = arr[splitIndex]
254+
arr[splitIndex] = pivot
255+
256+
sort(arr, start, splitIndex-1)
257+
sort(arr, splitIndex+1, end)
258+
}
259+
260+
261+
```
262+
224263
- Search in Rotated Sorted Array
225264
- Find First and Last Position of Element in Sorted Array
226265
- Search Insert Position

‎algorithm/kth_largest_element.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package algorithm
2+
3+
func findKthLargest(nums []int, k int) int {
4+
sort(nums,0,len(nums)-1)
5+
return nums[len(nums)-k]
6+
}
7+
8+
func sort(arr []int, start, end int) {
9+
if (end - start) < 1 {
10+
return
11+
}
12+
13+
pivot := arr[end]
14+
splitIndex := start
15+
16+
for i := start; i < end; i++ {
17+
if arr[i] < pivot {
18+
temp := arr[splitIndex]
19+
20+
arr[splitIndex] = arr[i]
21+
arr[i] = temp
22+
23+
splitIndex++
24+
}
25+
}
26+
27+
arr[end] = arr[splitIndex]
28+
arr[splitIndex] = pivot
29+
30+
sort(arr, start, splitIndex-1)
31+
sort(arr, splitIndex+1, end)
32+
}
33+

0 commit comments

Comments
(0)

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