现在的位置: 首页 > 综合 > 正文

冒泡和快速排序

2013年07月11日 ⁄ 综合 ⁄ 共 759字 ⁄ 字号 评论关闭

也没专门去研究过排序。只是平常用过这两种和这两种的变形:冒泡和快速。收录一下用的时候直接Copy就OK。

 public class Method
    {
       public static void QuickSort(List<int> arrays, int low, int high)
        {
            int i = low;

            int j = high;

            if (low < high)
            {
                int key = arrays[low];

                while (i < j)
                {
                    while (i < j && arrays[j] > key)
                    {
                        j--;
                    }
                    if (i < j)
                    {
                        arrays[i] = arrays[j];
                        i++;
                    }
                    while (i < j && arrays[i] < key)
                    {
                        i++;
                    }
                    if (i < j)
                    {
                        arrays[j] = arrays[i];

                        j--;
                    }
                }

                arrays[i] = key;

                QuickSort(arrays, low, i - 1);

                QuickSort(arrays, i + 1, high);
            }
        }

        public static void Bubble(List<int> sortList)
        {
            if (sortList == null || sortList.Count <= 0)
            {
                return;
            }

            int flag = 1;

            int i, j;

            int itemCount = sortList.Count;

            int itemTemp;

            for (i = 1; i < itemCount && flag == 1; i++)
            {
                flag = 0;

                for (j = 0; j < itemCount - i; j++)
                {
                    int countfore = sortList[j];

                    int countback = sortList[j + 1];

                    if (countfore > countback)
                    {
                        flag = 1;

                        itemTemp = sortList[j];

                        sortList[j] = sortList[j + 1];

                        sortList[j + 1] = itemTemp;
                    }
                }
            }
        }
    }

抱歉!评论已关闭.