选择排序优化三点

来源:互联网 发布:北航软件学院毕业生 编辑:程序博客网 时间:2024/04/30 10:34
public void ChoiceSort(string[] arrPrice)
        {//优化3点:arrPrice.Length -1(最后一次不用比较了)、j = i + 1(已经比较过了)、if (minIndex != i)不用交换
            Double temp = 0;
            int minIndex = 0;
            for (int i = 0; i < arrPrice.Length -1; i++)
            {
                minIndex = i;
                for (int j = i + 1; j < arrPrice.Length; j++)
                {
                    if (Convert.ToDouble(arrPrice[j]) < Convert.ToDouble(arrPrice[minIndex]))
                    {
                        minIndex = j;
                    }
                }
                if (minIndex != i)
                {
                    temp = Convert.ToDouble(arrPrice[minIndex]);
                    arrPrice[minIndex] = arrPrice[i];
                    arrPrice[i] = Convert.ToString(temp);
                }
            }
        }
0 0
原创粉丝点击