排序算法

 /// <summary>
        /// 冒泡排序
        /// </summary>
        /// <param name="arr"></param>
        public static void BuildderScort(int[] arr)
        {
            for (int i = 0; i < arr.Length- 1; i++)
            {
                for (int j = 0; j < arr.Length-1-i; j++)
                {
                    if (arr[j] > arr[j + 1])
                    {
                        int temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
            }
        }
        /// <summary>
        /// 选择排序
        /// </summary>
        /// <param name="arr"></param>
        public static void SelectScort(int[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                int minValue = arr[i];
                int minIndex = i;
                for (int j = i; j < arr.Length; j++)
                {
                    if (minValue > arr[j])
                    {
                        minValue = arr[j];
                        minIndex = j;
                    }
                }
                int temp = arr[minIndex];
                arr[minIndex] = arr[i];
                arr[i] = temp;
            }
        }
        /// <summary>
        /// 插入排序
        /// </summary>
        /// <param name="arr"></param>
        public static void InsertScort(int[] arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                int insertValue = arr[i];
                int insertIndex = i- 1;
                while (insertIndex >= 0 && insertValue <= arr[insertIndex])
                {
                    arr[insertIndex + 1] = arr[insertIndex];
                    insertIndex--;
                }
                arr[insertIndex + 1] = insertValue;
            }
        }
        /// <summary>
        /// 快速排序
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <param name="arr"></param>
        /// <returns></returns>
        private static int Quick(int left, int right, int[] arr)
        {
            int temp = arr[left];
            while (left < right)
            {
                while (left < right && arr[right] >= temp)
                {
                    right--;
                }
                if (left < right)
                {
                    arr[left] = arr[right];
                    left++;
                }
                while (left < right && arr[left] < temp)
                {
                    left++;
                }
                if (left < right)
                {
                    arr[right] = arr[left];
                    right--;
                }
            }
            arr[left] = temp;
            return left;
        }
        public static void QuickScort(int left, int right, int[] arr)
        {
            if (left < right)
            {
                int temp = Quick(left, right, arr);
                QuickScort(left, temp- 1, arr);
                QuickScort(temp + 1, right, arr);
            }
        }

 

posted @ 2013-11-15 13:47  Lukas89  阅读(211)  评论(0编辑  收藏  举报