首页 > 代码库 > 使用合并排序和快速排序对字符串按长度排序

使用合并排序和快速排序对字符串按长度排序

前段时间要对字符串集合进行按长度排序,字符串长度长的排在队列前面,最短的排在最后,可以使用两种排序方法进行排序,其中快速排序的效能会好些,但快速排序在字符串的集合非常大的时候,有时会得不到正确的结果,具体原因还不清楚。

1.合拼排序的代码

using System;
using System.Collections.Generic;
using System.Text;

namespace FtpproxyDownRule.DBUtility
{

    public class sortbyStringLength
    {
        /// <summary>
        /// 使用合并排序
        /// </summary>
        /// <param name="orderstrings">待排序的集合</param>
        public static void Mergesort(string[] orderstrings)
        {
            long arraylength = orderstrings.LongLength;
            if (arraylength > 1)
            {
                //把集合分成两半,分别进行排序
                long length = long.Parse(Convert.ToString(Math.Ceiling(orderstrings.LongLength / 2D)));

                string[] firstArray = new string[length];
                string[] secondArray = new string[arraylength - length];
                //把orderstrings前半部分复制到firstArray中
                copyArray(orderstrings, firstArray, true);
                //把orderstrings后半部分复制到secondArray中
                copyArray(orderstrings, secondArray, false);
                //递归调用,对firstArray分成两部分
                Mergesort(firstArray);
                Mergesort(secondArray);
                //合并排序后的结果
                merge(firstArray, secondArray, orderstrings);
            }
        }
        /// <summary>
        /// 对两个集合进行排序
        /// </summary>
        /// <param name="firstArray">待排序的集合</param>
        /// <param name="secondArray">待排序的集合</param>
        /// <param name="OriginalArray">firstArray,secondArray进行排序后得到的集合</param>
        private static void merge(string[] firstArray, string[] secondArray, string[] OriginalArray)
        {
            long firstArraylength = firstArray.LongLength;
            long secondArraylength = secondArray.LongLength;

            long i = 0, j = 0, k = 0;
            while (i < firstArraylength && j < secondArraylength)
            {
                if (firstArray[i].Length > secondArray[j].Length)
                {
                    OriginalArray[k] = firstArray[i];
                    i++;
                }
                else
                {
                    OriginalArray[k] = secondArray[j];
                    j++;
                }
                k++;
            }
            if (i == firstArraylength)
            {
                copyArray(secondArray, OriginalArray, j, k);
            }
            else
            {
                copyArray(firstArray, OriginalArray, i, k);
            }

        }
        private static void copyArray(string[] OriginalArray, string[] firstArray, bool flag)
        {
            long OriginalLength = OriginalArray.LongLength;
            long count = long.Parse(Convert.ToString(Math.Ceiling(OriginalLength / 2D)));
            if (flag)
            {
                for (int i = 0; i < count; i++)
                {

                    firstArray[i] = OriginalArray[i];

                }
            }
            else
            {
                long j = 0;
                for (long i = count; i < OriginalLength; i++)
                {
                    firstArray[j] = OriginalArray[i];
                    j++;
                }
            }

        }
        private static void copyArray(string[] OriginalArray, string[] firstArray, long OriginalArraybenginIndex, long FirstArraybenginIndex)
        {

            long OriginalArrayLenth = OriginalArray.LongLength;
            for (long i = OriginalArraybenginIndex; i < OriginalArrayLenth; i++)
            {
                firstArray[FirstArraybenginIndex] = OriginalArray[i];
                FirstArraybenginIndex++;
            }

        }
    }

}

快速排序的代码:

    class quickSortbyStringLenth
    {
        public static void QuickSort(string[] orderstring)
        {
            Qsort(orderstring, 0, orderstring.LongLength - 1);
        }
        private static void Qsort(string[] orderstrings, long lowindex, long highindex)
        {
            if (lowindex < highindex)
            {
                long pivotloc = Partition(orderstrings, lowindex, highindex);
                Qsort(orderstrings, lowindex, pivotloc - 1);
                Qsort(orderstrings, pivotloc + 1, highindex);
            }
        }
        private static long Partition(string[] OriginalArray, long lowIndex, long highIndex)
        {
            //OriginalArray[0] = OriginalArray[lowIndex];
            string p = OriginalArray[lowIndex];
            while (lowIndex < highIndex)
            {
                while (lowIndex < highIndex && OriginalArray[highIndex].Length <= p.Length) --highIndex;
                OriginalArray[lowIndex] = OriginalArray[highIndex];
                while (lowIndex < highIndex && OriginalArray[lowIndex].Length >= p.Length) ++lowIndex;
                OriginalArray[highIndex] = OriginalArray[lowIndex];
            }
            OriginalArray[lowIndex] = p;
            return lowIndex;
        }
    }