博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#中的各种排序算法
阅读量:5169 次
发布时间:2019-06-13

本文共 2891 字,大约阅读时间需要 9 分钟。

原文发布时间为:2008-11-26 —— 来源于本人的百度文章 [由搬家工具导入]

C#算法(一)选择排序

using System;
public class SelectionSorter
{
    // public enum comp {COMP_LESS,COMP_EQUAL,COMP_GRTR};
    private int min;
    // private int m=0;
    public void Sort(int[] list)
    {
        for (int i = 0; i < list.Length - 1; ++i)
        {
            min = i;
            for (int j = i + 1; j < list.Length; ++j)
            {
                if (list[j] < list[min])
                    min = j;
            }
            int t = list[min];
            list[min] = list[i];
            list[i] = t;
            // Console.WriteLine("{0}",list[i]);
        }

    }

}
public class MainClass
{
    public static void Main()
    {
        int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
        SelectionSorter ss = new SelectionSorter();
        ss.Sort(iArrary);
        for (int m = 0; m <= 13; m++)
            Console.WriteLine("{0}", iArrary[m]);

    }

}

C#算法(二)插入排序

using System;
public class InsertionSorter
{
    public void Sort(int[] list)
    {
        for (int i = 1; i < list.Length; ++i)
        {
            int t = list[i];
            int j = i;
            while ((j > 0) && (list[j - 1] > t))
            {
                list[j] = list[j - 1];
                --j;
            }
            list[j] = t;
        }

    }

}
public class MainClass
{
    public static void Main()
    {
        int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
        InsertionSorter ii = new InsertionSorter();
        ii.Sort(iArrary);
        for (int m = 0; m <= 13; m++)
            Console.WriteLine("{0}", iArrary[m]);
    }
}

C#算法(三)希尔排序

public class ShellSorter
{
    public void Sort(int[] list)
    {
        int inc;
        for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
        for (; inc > 0; inc /= 3)
        {
            for (int i = inc + 1; i <= list.Length; i += inc)
            {
                int t = list[i - 1];
                int j = i;
                while ((j > inc) && (list[j - inc - 1] > t))
                {
                    list[j - 1] = list[j - inc - 1];
                    j -= inc;
                }
                list[j - 1] = t;
            }
        }
    }
}
public class MainClass
{
    public static void Main()
    {
        int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
        ShellSorter sh = new ShellSorter();
        sh.Sort(iArrary);
        for (int m = 0; m <= 13; m++)
            Console.WriteLine("{0}", iArrary[m]);
    }
}
C#算法(四)快速排序
using System;

namespace QuickSorter

{
    public class QuickSorter
    {
        private void Swap(ref int l, ref int r)
        {
            int s;
            s = l;
            l = r;
            r = s;
        }
        public void Sort(int[] list, int low, int high)
        {
            int pivot;
            int l, r;
            int mid;
            if (high <= low)
                return;
            else if (high == low + 1)
            {
                if (list[low] > list[high])
                    Swap(ref list[low], ref list[high]);
                return;
            }
            mid = (low + high) >> 1;
            pivot = list[mid];
            Swap(ref list[low], ref list[mid]);
            l = low + 1;
            r = high;
            do
            {
                while (l <= r && list[l] < pivot)
                    l++;
                while (list[r] >= pivot)
                    r--;
                if (l < r)
                    Swap(ref list[l], ref list[r]);
            } while (l < r);
            list[low] = list[r];
            list[r] = pivot;
            if (low + 1 < r)
                Sort(list, low, r - 1);
            if (r + 1 < high)
                Sort(list, r + 1, high);
        }
    }
    public class MainClass
    {
        public static void Main()
        {
            int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
            QuickSorter q = new QuickSorter();
            q.Sort(iArrary, 0, 13);
            for (int m = 0; m <= 13; m++)
                Console.WriteLine("{0}", iArrary[m]);
        }
    }

}

转载于:https://www.cnblogs.com/handboy/p/7148473.html

你可能感兴趣的文章
tableView
查看>>
Happy Great BG-卡精度
查看>>
Xamarin Visual Studio不识别JDK路径
查看>>
菜鸟“抄程序”之道
查看>>
Ubuntu下关闭防火墙
查看>>
TCP/IP 邮件的原理
查看>>
原型设计工具
查看>>
windows下的C++ socket服务器(4)
查看>>
css3 2d转换3d转换以及动画的知识点汇总
查看>>
【Java】使用Eclipse进行远程调试,Linux下开启远程调试
查看>>
对Vue为什么不支持IE8的解释之一
查看>>
计算机改名导致数据库链接的诡异问题
查看>>
Java8内存模型—永久代(PermGen)和元空间(Metaspace)(转)
查看>>
ObjectiveC基础教程(第2版)
查看>>
centos 引导盘
查看>>
Notes of Daily Scrum Meeting(12.8)
查看>>
Apriori算法
查看>>
onlevelwasloaded的调用时机
查看>>
lr_start_transaction/lr_end_transaction事物组合
查看>>
CodeIgniter学习笔记(四)——CI超级对象中的load装载器
查看>>