排序的介绍可以看看之前我转载的一篇排序介绍:http://blog.csdn.net/nateyang/article/details/7655296
1.插入排序:
每次将一个待排序的数据元素,插入到前面已经排好序的数列中的适当位置,使数列依然有序;直到待排序数据元素全部插入完为止。
/// <summary>
/// 直接插入排序.
/// </summary>
public void SortMethod2()
{
recNum = 0;
int count = m_playerList.Count;
int i,j = 0 ;
for(i = 1;i< count;i++)
{
if(m_playerList[i -1].level < m_playerList[i].level)
{
Player tempPlayer = m_playerList[i];
for(j = i - 1;j >= 0;)
{
if(m_playerList[j].level < tempPlayer.level)
{
m_playerList[j +1] = m_playerList[j];
j--;
}
else{
break;
}
recNum++;
Debug.Log("SortMethod2-----------"+recNum);
}
m_playerList[j +1] = tempPlayer;
}
}
OutPutMsg();
}
排序结果:
直接插入排序时间复杂度:
最好为0(n) ,最坏为0(n*n).比较次数为:(n-1)(n+1)/2.