排序算法(快速排序、选择排序、冒泡排序、2分搜索)

本文通过C#实现多种排序算法,包括快速排序、选择排序、冒泡排序,并演示了如何使用二分查找法。每种排序算法都附带了运行次数统计,帮助理解不同算法的效率。

// 其实 System.Collections.ArrayList 和 List<int>都有各自的sort()方法。

 

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

namespace ConsoleTest
{
    class Sort
    {

        //static void Main()
        //{
        //    int[] a = { -20, 9, 7, 37, 38, 69, 89, -1, 59, 29, 0, -25, 39, 900, 22, 13, 55 };
        //    int[] b = new int[10];

        //    int iTmp = a[0], iMax;

        //    for (int i = 0; i < a.Length; i++)
        //    {
        //        iTmp = a[i] > iTmp ? a[i] : iTmp;
        //    }
        //    iMax = iTmp;
 
        //    for (int j = 0; j < b.Length; j++)
        //    {
        //        for (int i = 0; i < a.Length; i++)
        //        {
        //            if (j == 0)
        //                iTmp = a[i] < iTmp ? a[i] : iTmp;
        //            else
        //            {
        //                if (a[i] > b[j - 1])
        //                    iTmp = a[i] < iTmp ? a[i] : iTmp;
        //            }
        //        }
        //        b[j] = iTmp;
        //        iTmp = iMax;
        //    }

        //    foreach (int bb in b)
        //    {
        //        Console.WriteLine(bb);
        //    }
        //    Console.ReadLine();
        //}

        static void Main()
        {
            List<int> list = new List<int>() { 1,-93,9,723,4,92,79,13,34,29,64,3,81,3,-69};
            int iRunTimes = 0;
            QuickSort(list,ref iRunTimes); // 快速排序
            Print(list,iRunTimes);

            list = new List<int>() { 1, -93, 9, 723, 4, 92, 79, 13, 34, 29, 64, 3, 81, 3, -69 };
            iRunTimes = 0;
            SelectSort(list,ref iRunTimes); //简单排序
            Print(list, iRunTimes);

            list = new List<int>() { 1, -93, 9, 723, 4, 92, 79, 13, 34, 29, 64, 3, 81, 3, -69 };
            iRunTimes = 0;
            BubbleSort(list, ref iRunTimes); //冒泡排序
            Print(list, iRunTimes);

            list = new List<int>() { 1, -93, 9, 723, 4, 92, 79, 13, 34, 29, 64, 3, 81, 3, -69 };
            iRunTimes = 0;
            int iFoundIndex = -1;
            BinarySearch(list,3,ref iFoundIndex,ref iRunTimes); //2分法
            Print(list, iRunTimes);

            Console.WriteLine("输入任意键结束---");
            Console.ReadLine();
        }
 
        /// <summary>
        ///  打印最终结果
        /// </summary>
        /// <param name="intList"></param>
        /// <param name="iRunTime">运行次数</param>
        /// <param name="iOrderType"> iOrderType:0 升序,1降序</param>
        static void Print(List<int> intList,int iRunTime,int iOrderType=0)
        {
            StringBuilder s = new StringBuilder();
            for (int i = 0; i < intList.Count; i++)
            {
                s.Append(intList[i] + ",");
            }

            if (s.ToString().Length > 1)
                s.Remove(s.ToString().Length - 1, 1);

            Console.WriteLine(s.ToString());
            Console.WriteLine("运行次数: " + iRunTime);
            //Console.WriteLine("输入任意键结束---");
            //Console.ReadLine();
        }

        static void QuickSort(List<int> intList,ref int RunTimes)
        {
            if (intList.Count <= 1) return;

            List<int> less = new List<int>();
            List<int> more = new List<int>();
            int iIndex0 = intList[0];

            for (int i = 1; i < intList.Count; i++)
            {
                RunTimes++;

                if (intList[i] > iIndex0)
                    more.Add(intList[i]);
                else
                    less.Add(intList[i]);
            }

            intList.Clear();
            QuickSort(less, ref RunTimes);
            intList.AddRange(less);
            intList.Add(iIndex0);
            QuickSort(more, ref RunTimes);
            intList.AddRange(more);
        }

        static void SelectSort(List<int> intList,ref int iRunTimes)
        {
            int iMin=0;
            for (int i = 0; i < intList.Count - 1; i++)
            {
                iMin = intList[i];
                for (int j = i + 1; j < intList.Count; j++)
                {
                    if (iMin > intList[j])
                    {
                        iMin = intList[j];
                        intList[j] = intList[i];
                        intList[i] = iMin;

                        iRunTimes++;
                    }
                }
            }
        }

        static void BubbleSort(List<int> intList,ref int iRunTimes)
        {
            int iTmp = 0;
            bool bDone = false;

            for (int i = 0; i < intList.Count - 1; i++)
            {
                bDone = false;
                for (int j = 0; j < intList.Count-i-1; j++)
                {
                    if (intList[j] > intList[j+1])
                    {
                        iTmp = intList[j];
                        intList[j] = intList[j+1];
                        intList[j + 1] = iTmp;

                        iRunTimes++;
                        bDone = true;
                    }
                }
                if (!bDone) break;
            }
        }

        static bool BinarySearch(List<int> intList,int iTarget,ref int iFoundIndex,ref int intRunTimes)
        {
            int iRunTimes1 = 0;
            SelectSort(intList,ref iRunTimes1);

            int low = 0;
            int high = intList.Count - 1;
            int mid = 0;

            while (low <= high)
            {
                intRunTimes++;

                mid = (low + high) / 2;
                if (iTarget == intList[mid])
                {
                    //mid++;
                    Console.WriteLine("目标数据:{0} 的位置索引是:{1}", iTarget, mid);
                    iFoundIndex = mid;
                    return true;
                }
                else if (iTarget < intList[mid])
                {
                    high = mid - 1;
                }
                else
                {
                    low = mid + 1;
                }
            }

            Console.WriteLine("没有找到数据!");
            return false;
        }

    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值