码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-05-31 04:18:19      阅读:289      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

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

1.合拼排序的代码

bubuko.com,布布扣
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++;
            }

        }
    }

}
bubuko.com,布布扣

快速排序的代码:

bubuko.com,布布扣
    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;
        }
    }
bubuko.com,布布扣

使用合并排序和快速排序对字符串按长度排序,布布扣,bubuko.com

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

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/goxmpx/p/3761676.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!