Skip to content

Commit

Permalink
feat: 增加题目
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeilin01 committed Aug 21, 2020
1 parent b8451fb commit 7bd16c1
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 1 deletion.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,9 @@
- [字符串之验证回文串](algorithm/docs/verify-palindrome.md)
- [滑动窗口最大值](algorithm/docs/sliding-window-maximum.md)
- [最长公共前缀](algorithm/docs/longest-common-prefix.md)
- [两个数组的交集](algorithm/docs/array-intersection.md)
- [两个数组的交集](algorithm/docs/array-intersection.md)

### 排序算法

- [冒泡排序](algorithm/docs/bubble-sort.md)
- [选择排序](algorithm/docs/selection-sort.md)
44 changes: 44 additions & 0 deletions algorithm/docs/bubble-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# 冒泡排序

冒泡排序(Bubble Sort)也是一种简单直观的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。

作为最简单的排序算法之一,冒泡排序给我的感觉就像 Abandon 在单词书里出现的感觉一样,每次都在第一页第一位,所以最熟悉。冒泡排序还有一种优化算法,就是立一个 flag,当在一趟序列遍历中元素没有发生交换,则证明该序列已经有序。但这种改进对于提升性能来说并没有什么太大作用。

## 1. 算法步骤

比较相邻的元素。如果第一个比第二个大,就交换他们两个。

对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。这步做完后,最后的元素会是最大的数。

针对所有的元素重复以上的步骤,除了最后一个。

持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。

## 2. 动图演示

![](../../images/bubbleSort.b7d216a5.gif)

## 3. 最慢和最快

正序时最快,反序时最慢
## Golang实现

```go
func bubbleSort(arr []int) []int {
if len(arr) == 0 {
return arr
}
for i := 0; i < len(arr); i++ {
for j := 0; j < len(arr); j++ {
if arr[i] > arr[j] {
arr[j], arr[i] = arr[i], arr[j]
}
}
}
return arr
}
```




33 changes: 33 additions & 0 deletions algorithm/docs/insertion-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# 插入排序

插入排序的代码实现虽然没有冒泡排序和选择排序那么简单粗暴,但它的原理应该是最容易理解的了,因为只要打过扑克牌的人都应该能够秒懂。插入排序是一种最简单直观的排序算法,它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。

插入排序和冒泡排序一样,也有一种优化算法,叫做拆半插入。

## 1. 算法步骤

- 将第一待排序序列第一个元素看做一个有序序列,把第二个元素到最后一个元素当成是未排序序列。
- 从头到尾依次扫描未排序序列,将扫描到的每个元素插入有序序列的适当位置。(如果待插入的元素与有序序列中的某个元素相等,则将待插入元素插入到相等元素的后面。)

## 2. 动图演示

![](../../images/insertionSort.be81c151.gif)

## 3. Golang 实现

```go
func insertionSort(arr []int) []int {
l := len(arr)
if l == 0 {
return arr
}
for i := 0; i < l - 1; i++ {
for j := i + 1; j > 0; j-- {
if arr[j] < arr[j - 1] {
arr[j],arr[j - 1] = arr[j - 1],arr[j]
}
}
}
return arr
}
```
36 changes: 36 additions & 0 deletions algorithm/docs/selection-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# 选择排序

选择排序是一种简单直观的排序算法,无论什么数据进去都是 O(n²) 的时间复杂度。所以用到它的时候,数据规模越小越好。唯一的好处可能就是不占用额外的内存空间了吧。

## 1. 算法步骤

- 首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置
- 再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。
- 重复第二步,直到所有元素均排序完毕。

## 2. 动图演示

![](../../images/selectionSort.44be35da.gif)

## 3. Go 代码实现

```go
func selectionSort(arr []int) []int {
l := len(arr)
if l == 0 {
return arr
}
for i := 0; i < l; i++ {
min := i
for j := i + 1; j < l; j++ {
if arr[j] < arr[min] {
min = j
}
}
arr[i],arr[min] = arr[min],arr[i]
}
return arr
}
```


22 changes: 22 additions & 0 deletions algorithm/sort/bubble_sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import "fmt"

func main() {
arr := []int{8, 12, 46, 12, 2, 4, 15, 3, 9, 44, 5, 6, 1, 59, 2}
fmt.Println(bubbleSort(arr))
}

func bubbleSort(arr []int) []int {
if len(arr) == 0 {
return arr
}
for i := 0; i < len(arr); i++ {
for j := 0; j < len(arr); j++ {
if arr[i] > arr[j] {
arr[j], arr[i] = arr[i], arr[j]
}
}
}
return arr
}
23 changes: 23 additions & 0 deletions algorithm/sort/insertion_sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import "fmt"

func main() {
arr := []int{8, 12, 46, 12, 2, 4, 15, 3, 9, 44, 5, 6, 1, 59, 2}
fmt.Println(insertionSort(arr))
}

func insertionSort(arr []int) []int {
l := len(arr)
if l == 0 {
return arr
}
for i := 0; i < l-1; i++ {
for j := i + 1; j > 0; j-- {
if arr[j] < arr[j-1] {
arr[j], arr[j-1] = arr[j-1], arr[j]
}
}
}
return arr
}
25 changes: 25 additions & 0 deletions algorithm/sort/selection_sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import "fmt"

func main() {
arr := []int{8, 12, 46, 12, 2, 4, 15, 3, 9, 44, 5, 6, 1, 59, 2}
fmt.Println(selectionSort(arr))
}

func selectionSort(arr []int) []int {
l := len(arr)
if l == 0 {
return arr
}
for i := 0; i < l; i++ {
min := i
for j := i + 1; j < l; j++ {
if arr[j] < arr[min] {
min = j
}
}
arr[i], arr[min] = arr[min], arr[i]
}
return arr
}
Binary file added images/array-intersection-1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/array-intersection-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/array-intersection-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/array-intersection-4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/array-intersection.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/bubbleSort.b7d216a5.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/insertionSort.be81c151.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/selectionSort.44be35da.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 7bd16c1

Please sign in to comment.