C# 四种基本排序算法(冒泡排序,插入排序,选择排序,快速排序)外加折半排序

View
class Program
    {


        static void Main(string[] args)
        {
            //BubbleSort();
            //SelectSort();
            //int[] arr = new int[] { 49, 38, 65, 97, 76, 13, 27 };
            int[] arr = GetNumber();
            QuickSort(arr, 0, arr.Length - 1);
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.Read();
        }
        #region Random a new number

        public static int[] GetNumber()
        {
            int[] arr = new int[10];
            Random random = new Random();
            for (int i = 0; i < 10; i++)
            {
                arr[i] = random.Next(DateTime.Now.Millisecond);
            }
            return arr;
        }
        #endregion Random a new number
        #region 冒泡排序

        public static void BubbleSort(int[] arr)
        {
            //int[] arr = new int[] { 13, 4, 6, 3, 8 };
            int temp;
            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])
                    {
                        temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
            }         
        }
        #endregion 冒泡排序
        #region 插入排序
        public static void InsertSort(int[] arr)
        {
            //int[] arr = new int[] { 13, 4, 6, 3, 8 };
            int i, j, temp;
            for (i = 1; i < arr.Length; i++)
            {
                temp = arr[i];
                for (j = i; j > 0; j--)
                {
                    if (temp < arr[j - 1] && j > 0)
                    {
                        arr[j] = arr[j - 1];
                    }
                    else
                        break;
                }
                arr[j] = temp;
            }
        }
        #endregion 插入排序
        #region 选择排序

        public static void SelectSort(int[] arr)
        {
            //int[] arr = new int[] { 13, 4, 6, 3, 8 };
            int min, temp;
            for (int i = 0; i < arr.Length - 1; i++)
            {
                min = i;
                arr[min] = arr[i];
                for (int j = i + 1; j < arr.Length; j++)
                {
                    if (arr[min] > arr[j])
                    {
                        min = j;
                    }
                }
                temp = arr[i];
                arr[i] = arr[min];
                arr[min] = temp;
            }
        }
        #endregion 选择排序
        #region 快速排序

        public static void QuickSort(int[] arr, int left, int right)
        {
            int position;
            if (left < right)
            {
                position = KeyValuePosition(arr, left, right);
                QuickSort(arr, left, position - 1);
                QuickSort(arr, position + 1, right);
            }
        }
        //Get keyValuePosition
        public static int KeyValuePosition(int[] arr, int left, int right)
        {
            //int[] arr = new int[] {  49, 38, 65, 97, 76, 13,27 };
            int low = left, high = right, key = arr[left];
            while (low < high)
            {
                while (arr[high] > key)
                {
                    high--;
                }
                if (low < high)
                {
                    arr[low++] = arr[high];
                }
                while (arr[low] < key && low < high)
                {
                    low++;
                }
                if (low < high)
                {
                    arr[high--] = arr[low];
                }

            }
            arr[low] = key;
            return low;
        }
        #endregion 快速排序
    }
        #region 折半插入排序

        
        private static void binaryInsertSort(int[] arr)
        {
             
            for (int i = 1; i < arr.Length; i++)
            {
                if (arr[i] >= arr[i - 1])
                    continue;//为什么吾加就有错呢 
                int temp = arr[i], j;

                int low = 0, high = i - 1, tem;

                while (low <= high)
                {
                    tem = (low + high) / 2;
                    if (temp > arr[tem])
                    {
                        low = tem + 1;
                    }
                    else if (temp < arr[tem])
                    {
                        high = tem - 1;
                    }
                    else
                        break;
                }
                j = i;
                while (low < j)
                {
                    arr[j] = arr[j - 1];
                    j--;
                }


                arr[j] = temp;
            }
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.Read();
        }
        #endregion 折半插入排序

  

  

Code
 class Program
    {


        static void Main(string[] args)
        {
            //BubbleSort();
            //SelectSort();
            //int[] arr = new int[] { 49, 38, 65, 97, 76, 13, 27 };
            int[] arr = GetNumber();
            QuickSort(arr, 0, arr.Length - 1);
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.Read();
        }
        #region Random a new number

        public static int[] GetNumber()
        {
            int[] arr = new int[10];
            Random random = new Random();
            for (int i = 0; i < 10; i++)
            {
                arr[i] = random.Next(DateTime.Now.Millisecond);
            }
            return arr;
        }
        #endregion Random a new number
        #region 冒泡排序

        public static void BubbleSort(int[] arr)
        {
            //int[] arr = new int[] { 13, 4, 6, 3, 8 };
            int temp;
            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])
                    {
                        temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
            }
            for (int i = 0; i < arr.Length; i++)
            {
                Console.Write(arr[i] + " ");
            }
        }
        #endregion 冒泡排序
        #region 插入排序
        public static void InsertSort(int[] arr)
        {
            //int[] arr = new int[] { 13, 4, 6, 3, 8 };
            int i, j, temp;
            for (i = 1; i < arr.Length; i++)
            {
                temp = arr[i];
                for (j = i; j > 0; j--)
                {
                    if (temp < arr[j - 1] && j > 0)
                    {
                        arr[j] = arr[j - 1];
                    }
                    else
                        break;
                }
                arr[j] = temp;
            }
        }
        #endregion 插入排序
        #region 选择排序

        public static void SelectSort(int[] arr)
        {
            //int[] arr = new int[] { 13, 4, 6, 3, 8 };
            int min, temp;
            for (int i = 0; i < arr.Length - 1; i++)
            {
                min = i;
                arr[min] = arr[i];
                for (int j = i + 1; j < arr.Length; j++)
                {
                    if (arr[min] > arr[j])
                    {
                        min = j;
                    }
                }
                temp = arr[i];
                arr[i] = arr[min];
                arr[min] = temp;
            }
        }
        #endregion 选择排序
        #region 快速排序

        public static void QuickSort(int[] arr, int left, int right)
        {
            int position;
            if (left < right)
            {
                position = KeyValuePosition(arr, left, right);
                QuickSort(arr, left, position - 1);
                QuickSort(arr, position + 1, right);
            }
        }
        //Get keyValuePosition
        public static int KeyValuePosition(int[] arr, int left, int right)
        {
            //int[] arr = new int[] {  49, 38, 65, 97, 76, 13,27 };
            int low = left, high = right, key = arr[left];
            while (low < high)
            {
                while (arr[high] > key)
                {
                    high--;
                }
                if (low < high)
                {
                    arr[low++] = arr[high];
                }
                while (arr[low] < key && low < high)
                {
                    low++;
                }
                if (low < high)
                {
                    arr[high--] = arr[low];
                }

            }
            arr[low] = key;
            return low;
        }
        #endregion 快速排序
    }
posted @ 2012-10-25 13:11  威少  阅读(340)  评论(0编辑  收藏  举报