# go语言十大排序算法总结

guoer9973 · · 3278 次点击 · · 开始浏览

## 选择排序

``````package main

import (
"fmt"
)

type SortInterface interface {
sort()
}
type Sortor struct {
name string
}

func main() {
arry := []int{6, 1, 3, 5, 8, 4, 2, 0, 9, 7}
learnsort := Sortor{name: "选择排序--从小到大--不稳定--n*n---"}
learnsort.sort(arry)

fmt.Println(learnsort.name, arry)
}

func (sorter Sortor) sort(arry []int) {
arrylength := len(arry)
for i := 0; i < arrylength; i++ {
min := i
for j := i + 1; j < arrylength; j++ {
if arry[j] < arry[min] {
min = j
}
}
t := arry[i]
arry[i] = arry[min]
arry[min] = t

}
}

``````

``````/usr/local/go/bin/go build -i [/Users/liuhanlin/GO/src/go_learn]

/Users/liuhanlin/GO/src/go_learn/go_learn  [/Users/liuhanlin/GO/src/go_learn]

class EbullitionSorter
{
public void Sort(int[] arr)
{
int i, j, temp;
bool done = false;
j = 1;
while ((j < arr.Length) && (!done))//判断长度
{
done = true;
for (i = 0; i < arr.Length - j; i++)
{
if (arr[i] > arr[i + 1])
{
done = false;
temp = arr[i];
arr[i] = arr[i + 1];//交换数据
arr[i + 1] = temp;
}
}
j++;
}
}
}

class QuickSorter
{
private void swap(ref int l, ref int r)
{
int temp;
temp = l;
l = r;
r = temp;
}
public void Sort(int[] list, int low, int high)
{
int pivot;//存储分支点
int l, r;
int mid;
if (high <= low)
return;
else if (high == low + 1)
{
if (list[low] > list[high])
swap(ref list[low], ref list[high]);
return;
}
mid = (low + high) >> 1;
pivot = list[mid];
swap(ref list[low], ref list[mid]);
l = low + 1;
r = high;
do
{
while (l <= r && list[l] < pivot)
l++;
while (list[r] >= pivot)
r–;
if (l < r)
swap(ref list[l], ref list[r]);
} while (l < r);
list[low] = list[r];
list[r] = pivot;
if (low + 1 < r)
Sort(list, low, r - 1);
if (r + 1 < high)
Sort(list, r + 1, high);
}
}

public class InsertionSorter
{
public void Sort(int[] arr)
{
for (int i = 1; i < arr.Length; i++)
{
int t = arr[i];
int j = i;
while ((j > 0) && (arr[j - 1] > t))
{
arr[j] = arr[j - 1];//交换顺序
–j;
}
arr[j] = t;
}
}
}

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

## 选择排序

``````package main

import (
"fmt"
)

type SortInterface interface {
sort()
}
type Sortor struct {
name string
}

func main() {
arry := []int{6, 1, 3, 5, 8, 4, 2, 0, 9, 7}
learnsort := Sortor{name: "选择排序--从小到大--不稳定--n*n---"}
learnsort.sort(arry)

fmt.Println(learnsort.name, arry)
}

func (sorter Sortor) sort(arry []int) {
arrylength := len(arry)
for i := 0; i < arrylength; i++ {
min := i
for j := i + 1; j < arrylength; j++ {
if arry[j] < arry[min] {
min = j
}
}
t := arry[i]
arry[i] = arry[min]
arry[min] = t

}
}

``````

``````/usr/local/go/bin/go build -i [/Users/liuhanlin/GO/src/go_learn]

/Users/liuhanlin/GO/src/go_learn/go_learn  [/Users/liuhanlin/GO/src/go_learn]

class EbullitionSorter
{
public void Sort(int[] arr)
{
int i, j, temp;
bool done = false;
j = 1;
while ((j < arr.Length) && (!done))//判断长度
{
done = true;
for (i = 0; i < arr.Length - j; i++)
{
if (arr[i] > arr[i + 1])
{
done = false;
temp = arr[i];
arr[i] = arr[i + 1];//交换数据
arr[i + 1] = temp;
}
}
j++;
}
}
}

class QuickSorter
{
private void swap(ref int l, ref int r)
{
int temp;
temp = l;
l = r;
r = temp;
}
public void Sort(int[] list, int low, int high)
{
int pivot;//存储分支点
int l, r;
int mid;
if (high <= low)
return;
else if (high == low + 1)
{
if (list[low] > list[high])
swap(ref list[low], ref list[high]);
return;
}
mid = (low + high) >> 1;
pivot = list[mid];
swap(ref list[low], ref list[mid]);
l = low + 1;
r = high;
do
{
while (l <= r && list[l] < pivot)
l++;
while (list[r] >= pivot)
r–;
if (l < r)
swap(ref list[l], ref list[r]);
} while (l < r);
list[low] = list[r];
list[r] = pivot;
if (low + 1 < r)
Sort(list, low, r - 1);
if (r + 1 < high)
Sort(list, r + 1, high);
}
}

public class InsertionSorter
{
public void Sort(int[] arr)
{
for (int i = 1; i < arr.Length; i++)
{
int t = arr[i];
int j = i;
while ((j > 0) && (arr[j - 1] > t))
{
arr[j] = arr[j - 1];//交换顺序
–j;
}
arr[j] = t;
}
}
}