数据结构(C#)--冒泡、插入、快速、堆、归并、希尔、选择各种排序排序过程比较以及各种排序的所用时间的对比

19 篇文章 0 订阅
16 篇文章 0 订阅

// 学习小结 吴新强于2013年3月18日22:38:47 桂电2507

//冒泡、插入、快速、堆、归并、希尔、选择各种排序排序过程比较以及各种排序的所用时间的对比
using System.Text;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.IO;

namespace Chapter14
{
    class Program
    {
        private static int mid;
        private static int value;
        static void Main(string[] args)
        {

            CArray ca = new CArray(100);
            Timing tObj = new Timing();
            Random random = new Random(1000);//随机数

            for (int i = 0; i < 100; i++)
            {
                ca.Insert(random.Next(0, 1000));

            }
            Console.WriteLine("BubbleSort Before Sorting:");//排序前
            Console.WriteLine();
            ca.DisplayElements();//显示数据
            Console.WriteLine(); // 换行
            tObj.StartTime();//  测试排序时间的开始时间处
            Console.WriteLine();
            //  Console.WriteLine("BubbleSort During Sorting:");//排序中
            Console.WriteLine();
            //  ca.BubbleSort();//  调用冒泡排序法进行排序
            Console.WriteLine();
            tObj.StopTime();//  测试排序时间的截止时间
            Console.WriteLine("BubbleSort After Sorting:");//排序后      
            Console.WriteLine();
            ca.DisplayElements();//显示数据
            Console.WriteLine();
            Console.WriteLine("BubbleSort time( .net):" + tObj.Result().TotalMilliseconds + " MS");//  显示冒泡排序法共使用了多少时间
            Console.WriteLine();
            ca.Clear();
            for (int i = 0; i < 100; i++)
            {
                ca.Insert(random.Next(0, 1000));

            }
            Console.WriteLine("SelectionSort Before Sorting:");//排序前
            Console.WriteLine();
            ca.DisplayElements();//显示数据
            Console.WriteLine(); // 换行
            tObj.StartTime();//  测试排序时间的开始时间处
            Console.WriteLine();
            // Console.WriteLine("SelectionSort During Sorting:");//排序中
            Console.WriteLine();
            // ca.SelectionSort();//  调用选择排序法进行排序
            Console.WriteLine();
            tObj.StopTime();//  测试排序时间的截止时间
            Console.WriteLine("SelectionSort After Sorting:");//排序后      
            Console.WriteLine();
            ca.DisplayElements();//显示数据
            Console.WriteLine();
            Console.WriteLine("SelectionSort time( .net):" + tObj.Result().TotalMilliseconds + " MS");//  显示选择排序法共使用了多少时间
            Console.WriteLine();
            ca.Clear();
            for (int i = 0; i < 100; i++)
            {
                ca.Insert(random.Next(0, 1000));

            }
            Console.WriteLine("InsertionSort Before Sorting:");//排序前
            Console.WriteLine();
            ca.DisplayElements();//显示数据
            Console.WriteLine(); // 换行
            tObj.StartTime();//  测试排序时间的开始时间处
            Console.WriteLine();
            // Console.WriteLine("InsertionSort During Sorting:");//排序中
            Console.WriteLine();
            //  ca.InsertionSort();//  调用选择排序法进行排序
            Console.WriteLine();
            tObj.StopTime();//  测试排序时间的截止时间
            Console.WriteLine("InsertionSort After Sorting:");//排序后      
            Console.WriteLine();
            ca.DisplayElements();//显示数据
            Console.WriteLine();
            Console.WriteLine("InsertionSort time( .net):" + tObj.Result().TotalMilliseconds + " MS");//  显示选择排序法共使用了多少时间
            Console.WriteLine();
            ca.Clear();
            for (int i = 0; i < 100; i++)
            {
                ca.Insert(random.Next(0, 1000));

            }
            //  Console.WriteLine("请输入查找的值:");
            Console.WriteLine();
            ca.DisplayElements();//显示数据
            Console.WriteLine(); // 换行
            tObj.StartTime();//  测试排序时间的开始时间处
            Console.WriteLine();
            Console.WriteLine("请输入查找的值:");
            ca.RbinSearch(5, 1, 10);//  调用折半查找法进行查找
            int v = Convert.ToInt32(Console.ReadLine());
            if (value == mid)
                Console.WriteLine("你查找的值存在!");
            else
                Console.WriteLine("你查找的值不存在!");
            Console.WriteLine();
            tObj.StopTime();//  测试查找时间的截止时间     
            Console.WriteLine();
            ca.DisplayElements();//显示数据
            Console.WriteLine();
            Console.WriteLine("RbinSearch time( .net):" + tObj.Result().TotalMilliseconds + " MS");//  显示选择排序法共使用了多少时间
            ca.Clear();
           
            const int SIZE = 99;
            Heap aHeap = new Heap(SIZE);
            Random RandomClass = new Random();
          
            for (int i = 0; i < SIZE; i++)
            {

               int rn = RandomClass.Next(1, 1000);
               aHeap.Insert(rn);
            }
            Console.WriteLine();
            Console.WriteLine("排序前:");//排序前
           // Console.WriteLine();
            aHeap.ShowArray();//显示数据
           // Console.WriteLine(); // 换行
            tObj.StartTime();//  测试排序时间的开始时间处
          //  Console.WriteLine();
            // Console.WriteLine("InsertionSort During Sorting:");//排序中
          //  Console.WriteLine();
            //  ca.InsertionSort();//  调用选择排序法进行排序
          //  Console.WriteLine();
            tObj.StopTime();//  测试排序时间的截止时间
                  
            Console.WriteLine();
          
            Console.Write("堆: ");
            Console.WriteLine();
            for (int i = (int)SIZE / 2 - 1; i >= 0; i--)
                aHeap.ShiftDown(i);
            aHeap.ShowArray();
            for (int i = SIZE - 1; i >= 0; i--)
            {
              //  Node bigNode = aHeap.Remove();
               // aHeap.InsertAt(i, bigNode);
            }
            Console.WriteLine();
            Console.WriteLine("排序后:");//排序后
            aHeap.ShowArray();
            Console.WriteLine();
            Console.WriteLine("HeapSort time( .net):" + tObj.Result().TotalMilliseconds + " MS");//  显示选择排序法共使用了多少时间
            Console.WriteLine();
            ca.Clear();
            for (int i = 0; i < 100; i++)
            {
                ca.Insert(random.Next(0, 1000));

            }
            Console.WriteLine("QSort Before Sorting:");//排序前
           // Console.WriteLine();
            ca.DisplayElements();//显示数据
            Console.WriteLine(); // 换行
            tObj.StartTime();//  测试排序时间的开始时间处
           // Console.WriteLine();
            // Console.WriteLine("QSort During Sorting:");//排序中
           // Console.WriteLine();
            // ca.QSort();//  调用选择排序法进行排序
           // Console.WriteLine();
            tObj.StopTime();//  测试排序时间的截止时间
            Console.WriteLine("QSort After Sorting:");//排序后      
           // Console.WriteLine();
            ca.DisplayElements();//显示数据
            Console.WriteLine();
            Console.WriteLine("QSort time( .net):" + tObj.Result().TotalMilliseconds + " MS");//  显示选择排序法共使用了多少时间
            Console.WriteLine();
        }
    }

    class CArray
    {
        private int[] arr;
        private int upper;
        private int numElements;
        public CArray(int size)
        {
            arr = new int[size];
            upper = size - 1;
            numElements = 0;
        }
        public void Insert(int item)
        {
            arr[numElements] = item;
            numElements++;
        }
        public void DisplayElements()
        {
            for (int i = 0; i <= upper; i++)
                Console.Write(arr[i] + " ");
        }
        public void Clear()
        {
            for (int i = 0; i <= upper; i++)
                arr[i] = 0;
            numElements = 0;
        }
        /*    static void Main()
            {
                CArray nums = new CArray(50);
                for (int i = 0; i <= 49; i++)
                    nums.Insert(i);
                nums.DisplayElements();
                Console.ReadKey();
            }
         */
        public int RbinSearch(int value, int lower, int upper)
        {
            int mid;
            if (lower > upper)
                return -1;
            else
            {
                //  int mid;
                mid = (int)(lower + upper) / 2;
                if (value < arr[mid])
                    return RbinSearch(value, lower, mid - 1);
                else if (value == mid)
                    return mid;
                else
                    return RbinSearch(value, mid + 1, upper);
            }

            if (value == mid)
                Console.WriteLine("你查找的值存在!");
            else
                Console.WriteLine("你查找的值不存在!");

        }

 


        public void BubbleSort()
        {
            int temp;
            for (int outer = upper; outer >= 1; outer--)
            {
                for (int inner = 0; inner <= outer - 1; inner++)
                {
                    if ((int)arr[inner] > arr[inner + 1])
                    {
                        temp = arr[inner];
                        arr[inner] = arr[inner + 1];
                        arr[inner + 1] = temp;
                    }
                }
                this.DisplayElements();
            }
        }


        public void SelectionSort()
        {
            int min, temp;
            for (int outer = 0; outer <= upper; outer++)
            {
                min = outer;
                for (int inner = outer + 1; inner <= upper; inner++)
                {
                    if (arr[inner] < arr[min]) min = inner;
                }
                temp = arr[outer];
                arr[outer] = arr[min];
                arr[min] = temp;
                this.DisplayElements();
            }
        }

        public void InsertionSort()
        {
            int inner, temp;
            for (int outer = 1; outer <= upper; outer++)
            {
                temp = arr[outer];
                inner = outer;
                while (inner > 0 && arr[inner - 1] >= temp)
                {
                    arr[inner] = arr[inner - 1];
                    inner -= 1;
                }
                arr[inner] = temp;
                this.DisplayElements();
            }
        }
        public void ShellSort()
        {
            int inner, temp;
            int h = 3;
            while (h > 0)
            {
                for (int outer = h; outer <= numElements - 1; outer++)
                {
                    temp = arr[outer];
                    inner = outer;
                    while ((inner > h - 1) && arr[inner - h] >= temp)
                    {
                        arr[inner] = arr[inner - h];
                        inner -= h;
                    }
                    arr[inner] = temp;
                }
                h = (h - 1) % 3;
            }
        }    //  希尔排序
        public void Merge(int[] tempArray, int lowp, int highp, int ubound)  // 归并排序
        {
            int lbound = lowp;
            int mid = highp - 1;
            int n = (ubound - lbound) + 1;
            int j = 0;
            while ((lowp <= mid) && (highp <= ubound))
            {
                if (arr[lowp] < arr[highp])
                {
                    tempArray[j] = arr[lowp];
                    j++;
                    lowp++;
                }
                else
                {
                    tempArray[j] = arr[highp];
                    j++;
                    highp++;
                }
            }
            while (lowp <= mid)
            {
                tempArray[j] = arr[lowp];
                j++;
                lowp++;
            }
            while (highp <= ubound)
            {
                tempArray[j] = arr[highp];
                j++;
                highp++;
            }
            for (j = 0; j <= n - 1; j++)
                arr[lbound + j] = tempArray[j];
        }
        public void QSort()
        {
            RecQSort(0, numElements - 1);
        }
        public void RecQSort(int first, int last)
        {
            if ((last - first) <= 0)
                return;
            else
            {
                int part = this.Partition(first, last);
                RecQSort(first, part - 1);
                RecQSort(part + 1, last);
            }
        }
        public int Partition(int first, int last)
        {
            int pivotVal = arr[first];
            int theFirst = first;
            bool okSide;
            first++;
            do
            {
                okSide = true;
                while (okSide)
                    if (arr[first] > pivotVal)
                        okSide = false;
                    else
                    {
                        first++;
                        okSide = (first <= last);
                    }
                okSide = true;
                while (okSide)
                    if (arr[last] <= pivotVal)
                        okSide = false;
                    else
                    {
                        last--;
                        okSide = (first <= last);
                    }
                if (first < last)
                {
                    Swap(first, last);
                    this.DisplayElements();
                    first++;
                    last--;
                }
            } while (first <= last);
            Swap(theFirst, last);
            this.DisplayElements();
            return last;
        }
        public void Swap(int item1, int item2)
        {
            int temp = arr[item1];
            arr[item1] = arr[item2];
            arr[item2] = temp;
        }

    }
    public class Timing   // 时间测试类
    {
        TimeSpan duration;
        public Timing()
        {
            duration = new TimeSpan(0);
        }
        public void StopTime()
        {
            duration = Process.GetCurrentProcess().TotalProcessorTime;
        }
        public void StartTime()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
        }
        public TimeSpan Result()
        {
            return duration;
        }
    }
    public class Heap
    {
        Node[] heapArray = null;
        private int maxSize = 0;
        private int currSize = 0;
        public Heap(int maxSize)
        {
            this.maxSize = maxSize;
            heapArray = new Node[maxSize];
        }
        public bool InsertAt(int pos, Node nd)
        {
            heapArray[pos] = nd;
            return true;
        }
        public void ShowArray()
        {
            for (int i = 0; i < maxSize; i++)
            {
                if (heapArray[i] != null)
                    System.Console.Write(heapArray[i].data + "  ");
            }
        }
        public Node Remove()
        {
            Node root = heapArray[0];
            currSize--;
            heapArray[0] = heapArray[currSize];
            ShiftDown(0);
            return root;
        }
        public void ShiftDown(int index)
        {
            int largerChild;
            Node top = heapArray[index];
            while (index < (int)(currSize / 2))
            {
                int leftChild = 2 * index + 1;
                int rightChild = leftChild + 1;
                if ((rightChild < currSize) && heapArray[leftChild].data < heapArray[rightChild].data)
                    largerChild = rightChild;
                else
                    largerChild = leftChild;
                if (top.data >= heapArray[largerChild].data)
                    break;
                heapArray[index] = heapArray[largerChild];
                index = largerChild;
            }
            heapArray[index] = top;
        }
    public class Node
{
    public int data;
    public Node(int key)
    {
        data = key;
    }
}


public void ShiftUp(int index)
{
    int parent = (index - 1) / 2;
    Node bottom = heapArray[index];
    while ((index > 0) && (heapArray[parent].data < bottom.data))
    {
        heapArray[index] = heapArray[parent];
        index = parent;
        parent = (parent - 1) / 2;
    }
    heapArray[index] = bottom;
}

public bool Insert(int key)
{
    if (currSize == maxSize)
        return false;
    heapArray[currSize] = new Node(key);
    currSize++;
    return true;
}
    }   // 堆排序
}

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值