归并排序

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

namespace ConsoleApplication20
{
    class Program
    {
        static void Main(string[] args)
        {
            SortingAlgorithms MyArray = new SortingAlgorithms(10);
            Random rnd = new Random(100);

            long Ticks = DateTime.Now.Ticks;
            for (int i = 0; i < 10; i++)
            {
                MyArray.Insert((int)(rnd.NextDouble() * 100));
            }

            Console.WriteLine("Before Sorting:");
            MyArray.DisplayElement();

            //归并排序
            MyArray.MergeSort();

            Console.WriteLine("After sorting");
            //打印排序后的元素
            MyArray.DisplayElement();

            Console.ReadKey();
        }
    }
    //}
    //namespace Sorting
    //{
    class SortingAlgorithms
    {
        private int[] arr;
        private int upper;
        private int numElement;

        //初始化数组
        public SortingAlgorithms(int size)
        {
            arr = new int[size];
            upper = size - 1;
            numElement = 0;
        }

        //给数组插入元素
        public void Insert(int item)
        {
            arr[numElement] = item;
            numElement++;
        }

        //打印数组元素
        public void DisplayElement()
        {
            for (int i = 0; i <= upper; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.ReadLine();
        }

        public void CleaArry()
        {
            for (int i = 0; i <= upper; i++)
            {
                arr[i] = 0;
                numElement = 0;
            }
        }


        /*归并排序,以最坏的运行时间运行,该算法的基本的操作是合并两个
    已经排序的表,是一个稳定的排序算法,但是不经常使用。
    */
        public void MergeSort()
        {
            //临时数组,存放排序好的数据
            int[] TempArray = new int[numElement];
            RecMergeSort(TempArray, 0, numElement - 1);

        }

        public void RecMergeSort(int[] tempArray, int lbount, int ubound)
        {
            if (lbount == ubound)
            {
                return;
            }
            else
            {
                int Mid = (lbount + ubound) / 2;
                //递归调用
                //数组的前半段
                RecMergeSort(tempArray, lbount, Mid);
                //数组的后半段
                RecMergeSort(tempArray, Mid + 1, ubound);
                Merge(tempArray, lbount, Mid + 1, ubound);
            }

        }

        public void Merge(int[] tempArray, int lowp, int highp, int ubound)
        {
            int lbound = lowp;
            int mid = highp - 1;
            int n = (ubound - lbound) + 1;
            //原书中没有定义该变量,但是在while语句中用到了
            int j = 0;
            //将数组中元素由小到大复制到临时数组
            while ((lowp <= mid) && (highp <= ubound))//保证输入的准确性
            {
                if (arr[lowp] < arr[highp])//
                {
                    tempArray[j] = arr[lowp];
                    j++;
                    lowp++;
                }
                else
                {
                    tempArray[j] = arr[highp];//用在只有0,1索引的时候
                    j++;
                    highp++;
                }
            }

            //拷贝上半部的数据到临时数组
            while (lowp <= mid)
            {
                tempArray[j] = arr[lowp];
                j++;
                lowp++;
            }

            //拷贝下半部的剩余数据到临时数组
            while (highp <= ubound)
            {
                tempArray[j] = arr[highp];
                j++;
                highp++;
            }

            //原书中还是定义的为j,这样容易造成歧义,修改为k
            //将临时数组中有序的表拷贝至正式数组中
            for (int k = 0; k <= n - 1; k++)
            {
                arr[lbound + k] = tempArray[k];
            }
        }

    }

}

posted on 2010-04-07 17:14  ATAK  阅读(183)  评论(0编辑  收藏  举报

导航