c#实现冒泡、快速、选择和插入排序算法

整理一下常用的排序算法,用c#实现,以备日后再用。

1.冒泡排序

将被排序的记录数组R[1..n]垂直排列,每个记录R[i]看作是重量为R[i].key的气泡。根据轻气泡不能在重气泡之下的原则,从下往上扫描数组R:凡扫描到违反本原则的轻气泡,就使其向上"飘浮"(冒泡因此得名)。如此反复进行,直到最后任何两个气泡都是轻者在上,重者在下为止。

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

namespace BubbleSort
{
    public class BubbleSorter
    {
        /// <summary>
        /// 冒泡排序 
        /// </summary>
        /// <param name="numArr"></param>
        public void Sort(int[] numArr)
        {
            int tmpNum;
            bool flag = false; //交换标志
            for (int i = 1; i < numArr.Length; i++) //最多做numArr.Length-1趟排序
            {
                flag = false;
                for (int j = numArr.Length - 1; j >= i; j--) //对当前无序区自下向上扫描
                {
                    if (numArr[j] < numArr[j - 1]) //当前无序区: 轻的在下面,“冒泡”到上面
                    {
                        tmpNum = numArr[j];
                        numArr[j] = numArr[j - 1];
                        numArr[j - 1] = tmpNum;
                        flag = true;
                    }
                }
                if (!flag) //如果没有发生交换,终止算法
                    return;
            }
        }

        public class Program
        {
            public static void Main()
            {
                int[] testArr = new int[] { 1, 5, 11, 6, 4, 21, 99, 2, 15, 11, 34, 0, 33, 47 };
                BubbleSorter sh = new BubbleSorter();
                sh.Sort(testArr);
                for (int m = 0; m < testArr.Length; m++)
                    Console.Write("{0} ", testArr[m]);
                Console.Read();
            }
        }
    }
}

2 快速排序

快速排序是C.R.A.Hoare于1962年提出的一种划分交换排序。它采用了一种分治的策略,通常称其为分治法(Divide-and-ConquerMethod)。分治法的基本思想是:将原问题分解为若干个规模更小但结构与原问题相似的子问题。递归地解这些子问题,然后将这些子问题的解组合为原问题的解。
二、快速排序的基本思想
     设当前待排序的无序区为R[low..high],利用分治法可将快速排序的基本思想描述为:
(1)分解: 
     在R[low..high]中任选一个记录作为基准(Pivot),以此基准将当前无序区划分为左、右两个较小的子区间R[low..pivotpos-1)和R[pivotpos+1..high],并使左边子区间中所有记录的关键字均小于等于基准记录(不妨记为pivot)的关键字pivot.key,右边的子区间中所有记录的关键字均大于等于pivot.key,而基准记录pivot则位于正确的位置(pivotpos)上,它无须参加后续的排序。注意:划分的关键是要求出基准记录所在的位置pivotpos。划分的结果可以简单地表示为(注意 pivot=R[pivotpos] ):  R[low..pivotpos-1].keys≤R[pivotpos].key≤R[pivotpos+1..high].keys,
其中low≤pivotpos≤high。(边界条件)
(2)求解: 
     通过递归调用快速排序对左、右子区间R[low..pivotpos-1]和R[pivotpos+1..high]快速排序。
(3)组合: 
     因为当"求解"步骤中的两个递归调用结束时,其左、右两个子区间已有序。对快速排序而言,"组合"步骤无须做什么,可看作是空操作。

using System;

namespace QuickSorter
{
    public class QuickSort
    {
        public static void Sort(int[] numArr)
        {
            Sort(numArr, 0, numArr.Length - 1);
        }

        private static void Sort(int[] numArr, int left, int right)
        {
            if (left < right)
            {
                int middle = numArr[(left + right) / 2];
                int i = left - 1;
                int j = right + 1;
                while (true)
                {
                    while (numArr[++i] < middle) ;

                    while (numArr[--j] > middle) ;

                    if (i >= j)
                        break;

                    Swap(numArr, i, j);
                }

                Sort(numArr, left, i - 1);
                Sort(numArr, j + 1, right);
            }
        }

        private static void Swap(int[] numArr, int i, int j)
        {
            int number = numArr[i];
            numArr[i] = numArr[j];
            numArr[j] = number;
        }

    }

    public class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[] { 20, 41, 27, 14, 16, 1, 8, 55, 9, 35, 22, 14 };
            QuickSort.Sort(arr);
            Console.WriteLine("Numbers after quicksort:");
            foreach (int i in arr)
            {
                Console.WriteLine(i);
            }
            Console.Read();
        }
    }
}

3.选择排序

using System;

namespace SelectionSorter
{
    /// <summary>
    ///  选择排序(Selection Sort)的基本思想是:每一趟从待排序的记录中选出关键字最小的记录,顺序放在已排好序的子文件的最后,直到全部记录排序完毕。
    /// </summary>
    public class SelectionSort
    {
        public static void Sort(int[] numArray)
        {
            int min, tmp;
            for (int i = 0; i < numArray.Length - 1; i++)
            {
                min = i;
                for (int j = i + 1; j < numArray.Length; j++)
                {
                    if (numArray[j] < numArray[min])
                    {
                        min = j;
                    }
                }
                tmp = numArray[i];
                numArray[i] = numArray[min];
                numArray[min] = tmp;
            }

        }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[] { 20, 41, 27, 14, 16, 1, 8, 55, 9, 35, 22, 14 };
            SelectionSort.Sort(arr);
            Console.WriteLine("Numbers after selectionsort:");
            foreach (int i in arr)
            {
                Console.WriteLine(i);
            }
            Console.Read();
        }
    }
}

4.插入排序
using System;

namespace InsertSorter
{

    /// <summary>
    /// 插入排序(Insertion Sort)的算法描述是一种简单直观的排序算法。它的工作原理是通过构建有序序列,对于未排序数据,
    /// 在已排序序列中从后向前扫描,找到相应位置并插入。插入排序在实现上,通常采用in-place排序(即只需用到O(1)的额外空间的排序),
    /// 因而在从后向前扫描过程中,需要反复把已排序元素逐步向后挪位, 为最新元素提供插入空间。
    /// </summary>
    public class InsertSort
    {
        public static void Sort(int[] numArr)
        {
            for (int i = 1; i < numArr.Length; i++) //i从1开始
            {
                int t = numArr[i]; //标志当前未排序数据
                int j = i;
                while ((j > 0) && (numArr[j - 1] > t))
                {
                    numArr[j] = numArr[j - 1];
                    j--;
                }
                numArr[j] = t; //在已排序序列中插入当前值
            }
        }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[] { 20, 41, 27, 14, 16, 1, 8, 55, 9, 35, 22, 14 };
            InsertSort.Sort(arr);
            Console.WriteLine("Numbers after insertsort:");
            foreach (int i in arr)
            {
                Console.WriteLine(i);
            }
            Console.Read();
        }
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值