forked from lifei6671/interview-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
lifeilin01
committed
Aug 21, 2020
1 parent
b8451fb
commit 7bd16c1
Showing
15 changed files
with
189 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
``` | ||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.