using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class 选择排序 : MonoBehaviour
{
private int[] arr = new int[] { 22, 3, 43, 24, 6, 9, 10, 12, 5, 1, 8, 7 };
public void Start()
{
int[] temp = 选择排序Test(arr);
for (int i = 0; i < temp.Length; i++)
{
Debug.Log(temp[i]);
}
}
public int[] 选择排序Test(int[] arr)
{
for (int i = 0; i < arr.Length; i++)
{
int minIndex = i;
for (int j = i + 1; j < arr.Length; j++)
{
if (arr[j] < arr[minIndex])
{
minIndex = j;
}
}
if (i != minIndex)
{
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
return arr;
}
}
得出结果1 / 3 / 5 / 6 / 7 / 8 / 9 / 10 / 12 / 22 / 24 / 43 /
选择排序的原理是记录一个最小的值,和第一个值比较交换