c# 各种排序算法+找第二大的数+句子单词反转

冒泡排序


// 冒泡排序 bubble sort
        public static int[] BubbleSort(int []array)
        {
            bool isContinue = true;
            for (int i = 0; i < array.Length && isContinue; i++)
            {
                isContinue = false;
                for (int j = 0; j < array.Length - i - 1; j++)
                {
                    if (array[j] > array[j + 1])
                    {
                        int temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                        isContinue = true;
                    }
                }
            }
            return array;
        }
View Code
双向冒泡

// 双向冒泡 Double sided bubble sort
        public static int[] TowWayBubbleSort(int[] array)
        {
            bool isContinue = true;
            for (int i = 0; i < array.Length && isContinue; i++)
            {
                isContinue = false;
                for (int j = 0; j < array.Length - i - 1; j++)
                {
                    if (array[j] > array[j + 1])
                    {
                        int temp = array[j];
                        array[j] = array[j + 1];
                        array[j + 1] = temp;
                        isContinue = true;
                    }
                    if (array[array.Length - j - 1] < array[array.Length - j - 2])
                    {
                        int temp = array[array.Length - j - 1];
                        array[array.Length - j - 1] = array[array.Length - j - 2];
                        array[array.Length - j - 2] = temp;
                        isContinue = true;
                    }
                }
            }
            return array;
        }
View Code
插入排序

// 插入排序 Insertion sort
        public static int[] InsertionSort(int[] array)
        {
            for (int i = 1; i < array.Length; i++)
            {
                int current = array[i];
                int j = i;
                while (j > 0 && current < array[j - 1])
                {
                    array[j] = array[j - 1];
                    j--;
                }
                array[j] = current;
            }

            return array;
        }
View Code
折半插入

// 折半插入排序 Half insertion sort
        public static int[] HalfInsertionSort(int[] array)
        {
            for (int i = 1; i < array.Length; i++)
            {
                int current = array[i];
                int low = 0;
                int high = i - 1;
                int mid;
                while (low <= high)
                {
                    mid = (low + high) / 2;
                    if (array[mid] < current)
                    {
                        low = mid + 1;
                    }
                    else
                    {
                        high = mid - 1;
                    }
                }
                for (int j = i; j > low; j--)
                {
                    array[j] = array[j - 1];
                }
                array[low] = current;
            }
            return array;
        }
View Code
选择排序

// 选择排序 Selection sort
        public static int[] SelectionSort(int[] array)
        {
            for (int i = 0; i < array.Length; i++)
            {
                int min = i;
                for (int j = i + 1; j < array.Length; j++)
                {
                    if (array[j] < array[min])
                    {
                        min = j;
                    }
                }
                int temp = array[i];
                array[i] = array[min];
                array[min] = temp;
            }

            return array;
        }
View Code
快速排序

// 快速排序 Quick sort
        public static void QuickSort(int[] array, int low, int high)
        {
            if (low < high)
            {
                int midValue = array[low];
                int left = low;
                int right = high;
                while (left < right)
                {
                    while (left < right && array[right] >= midValue)
                    {
                        right--;
                    }
                    if (left < right)
                    {
                        array[left++] = array[right];
                    }
                    while (left < right && array[left] < midValue)
                    {
                        left++;
                    }
                    if (left < right)
                    {
                        array[right--] = array[left];
                    }
                }
                array[left] = midValue;
                QuickSort(array, low, left - 1);
                QuickSort(array, left + 1, high);
            }
        }
View Code
找第二大的数

public static int GetSecondNum(int[] array)
        {
            int first = 0;
            int second = -1;
            for (int i = 1; i < array.Length; i++)
            {
                if (array[first] < array[i])
                {
                    second = first;
                    first = i;
                }
                else if (second == -1 || (array[i] < array[first] && array[second] < array[i]))
                {
                    second = i;
                }
            }

            return array[second];
        }
View Code
反转句子单词不反转

public static string WordReverse(string str)
        {
            char[] array = str.ToArray();
            CharArrayReverse(array, 0, array.Length - 1);
            int start = -1;
            int end = -1;
            for (int i = 0; i < array.Length; i++)
            {
                if (!((array[i] >= 'a' && array[i] <= 'z') || (array[i] >= 'A' && array[i] <= 'Z')))
                {
                    if (start < end)
                    {
                        CharArrayReverse(array, start + 1, end);
                    }
                    start = i;
                }
                else
                {
                    end = i;
                }
            }
            return new string(array);
        }

        public static void CharArrayReverse(char[] array, int start, int end)
        {
            if (array != null && start < array.Length && end < array.Length)
                while (start < end)
                {
                    char temp = array[start];
                    array[start] = array[end];
                    array[end] = temp;
                    start++;
                    end--;
                }
        }
View Code

 

posted @ 2013-08-14 08:35  While蹒跚学步...  阅读(543)  评论(0编辑  收藏  举报