番茄的梦想

那一抹夕阳

  博客园  :: 首页  ::  :: 联系 :: 订阅 订阅  :: 管理

class Program
    {
        static void Main(string[] args)
        {
            int[] arr ={ 23,3, 2, 5, 6, 1, 234, 4, 345, 567, 7 };

            print(arr);

            //QuickSort(arr,0,arr.Length-1);
            selectSort(arr);

            print(arr);           
        }

        private static void selectSort(int []arr)
        {
            for (int i = 0; i < arr.Length - 1;i++ )
            {
                for (int j = i + 1; j <=arr.Length - 1;j++ )
                {
                    if(arr[i]>arr[j])
                    {
                        int temp = arr[i];
                        arr[i] = arr[j];
                        arr[j] = temp;
                    }
                }
            }
        }

        private static void QuickSort(int[] arr,int low,int high)
        {
            if(low<high)
            {
                int pivotLoc = partialQuickSort(arr,low,high);
                QuickSort(arr,low,pivotLoc-1);
                QuickSort(arr,pivotLoc+1,high);
            }
        }

        private static int partialQuickSort(int[] arr, int low, int high)
        {           
            int pivot = arr[low];
            int pivotIndex = low;
            while (low < high)
            {
                while (low < high && arr[high] >= pivot)
                {
                    high--;
                }

                int temp = arr[pivotIndex];
                arr[pivotIndex] = arr[high];
                arr[high] = temp;

                pivotIndex = high;

                while (low < high && arr[low] <= pivot)
                {
                    low++;
                }

                temp = arr[pivotIndex];
                arr[pivotIndex] = arr[low];
                arr[low] = temp;

                pivotIndex = low;
            }

            return pivotIndex;
        }       

        private static void BubbleSort(int []arr)
        {
            for (int i = 0; i < arr.Length-1; i++)
            {
                bool isChange = false;
                for (int j = 0; j < arr.Length - 1-i; j++)
                {
                    if (arr[j]<arr[j+1])
                    {
                        isChange = true;
                        int temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
                if(!isChange)
                {
                    return;
                }
            }
        }

        private static void print(int []arr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                if (i != arr.Length - 1)
                {
                    Console.Write("{0,3},", arr[i].ToString().PadLeft());
                }
                else
                {
                    Console.Write("{0,3}", arr[i]);
                }
            }
            Console.WriteLine();
        }
    }

posted on 2007-06-27 16:26  番茄的梦想  阅读(334)  评论(1编辑  收藏  举报