Array数组集合的排序

时间:2022-02-05 08:36:53
/*
#########
############
#############
## ###########
### ###### #####
### ####### ####
### ########## ####
#### ########### ####
#### ########### #####
##### ### ######## #####
##### ### ######## ######
###### ### ########### ######
###### #### ############## ######
####### ##################### ######
####### ###################### ######
####### ###### ################# ######
####### ###### ###### ######### ######
####### ## ###### ###### ######
####### ###### ##### #####
###### ##### ##### ####
##### #### ##### ###
##### ### ### #
### ### ###
## ### ###
__________#_______####_______####______________ 身是菩提树,心如明镜台,时时勤拂拭,勿使惹尘埃。
我们的未来没有BUG
* ==============================================================================
* Filename: Array
* Created: 2017/5/3
* Author: ShangHai WangYuChen
* ==============================================================================
*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic; public class Array : MonoBehaviour {
List<int> list = new List<int>() { 11,5,9,33,15,32,15};
/// <summary>
/// 直接插入排序
/// </summary>
/// <param name="list"></param>
public static void InsertSort(List<int> list) {
//从第二个数开始循环, 循环n-1次
for (int i = 1; i < list .Count; i++)
{
//将待排序的数拿出来, 以便后面挪位子
int temp = list[i];
//j就是最后确定的那个最大/最小数的下标
int j = i;
while (j>=1&&temp <list[j-1])
{
//将满足条件的数据向后移动一位, 腾空位, 为插入挪位子
list[j] = list[j - 1];
j--;
}
list[j] = temp;
}
}
/// <summary>
/// 希尔排序
/// </summary>
/// <param name="list"></param>
public static void ShellSort(List<int> list) {
int step = list.Count / 2;
while (step>=1)
{
for (int i = step; i < list.Count; i++)
{
int temp = list[i];
int j = i;
while (j>=step&&temp>list[j-step])
{
list[j] = list[j - step];
j -= step;
}
list[j] = temp;
}
step = step / 2;
}
}
/// <summary>
/// 直接选择排序
/// </summary>
/// <param name="list"></param>
public static void SelectionSort(List<int> list) {
for (int i = 0; i < list.Count -1; i++)
{
//假设tempIndex的下标的值最小
int tempIndex = i;
for (int j = i+1; j < list.Count; j++)
{
//如果tempIndex下标的值大于j下标的值,则记录较小值下标j
if (list [tempIndex]>list [j])
{
tempIndex = j;
}
}
//最后将假想最小值跟真的最小值进行交换
int temp = list[tempIndex];
list[tempIndex] = list[i];
list[i] = temp;
}
}
/// <summary>
/// 冒泡排序算法
/// </summary>
/// <param name="list"></param>
public static void BubbleSort(List<int> list) {
for (int i = 0; i < list.Count -1; i++)
{
for (int j = 1; j < list.Count ; j++)
{
//如果前面一个数大于后面一个数则交换
if (list [j-1]>list [j])
{
int temp = list[j - 1];
list[j - 1] = list[j];
list[j] = temp;
}
}
}
}
/// <summary>
/// 冒泡排序算法(自己喜欢常用的冒泡排序)
/// </summary>
/// <param name="list"></param>
public static void loveSort(List<int> list)
{
for (int i = 0; i < list.Count; i++)
{
for (int j = i+1; j < list.Count; j++)
{
//如果前面一个数大于后面一个数则交换
if (list[i] > list[j])
{
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
}
} void Start () {
// InsertSort(list);
//ShellSort(list);
//SelectionSort(list);
//BubbleSort(list);
loveSort(list);
for (int i = 0; i < list.Count; i++)
{
Debug.Log("第"+i+"是:"+list [i]);
}
}
}