3、选择排序
每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完。
/// <summary>
/// 简单选择排序.
/// </summary>
public void SortMethod4()
{
recNum = 0;
int count = m_playerList.Count;
int i,j = 0 ;
for(i = 0;i < count - 1;i++)
{
Player tempPlayer = null;
int index = i;
for(j = i+1;j < count;j++)
{
if(m_playerList[index].level < m_playerList[j].level)
{
index = j;
}
recNum++;
Debug.Log("SortMethod2-----------"+recNum);
}
tempPlayer = m_playerList[index];
m_playerList[index] = m_playerList[i];
m_playerList[i] = tempPlayer;
}
OutPutMsg();
}
排序结果:
选择排序的时间复杂度0(n*n) ,比较次数为n(n-1)/2次.