排序

直接插入排序

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

namespace 直接插入排序
{
    class Program
    {
        static void InsertSort(int[] dataArray)
        {
            for (int i = 1; i < dataArray.Length; i++)
            {
                int iValue = dataArray[i];
                bool isInsert = false;
                //拿到i位置的元素, 跟前面所有的元素比较
                //如果发现比i大的,就让它向后移动
                for (int j = i - 1; j >= 0; j--)
                {
                    if (dataArray[j] > iValue)
                    {
                        dataArray[j + 1] = dataArray[j];
                    }
                    else
                    {
                        dataArray[j+1] = iValue;
                        isInsert = true;
                        break;
                    }
                }
                if (isInsert == false)//没插入时
                {
                    dataArray[0] = iValue;
                }
            }
        }

        static void Main(string[] args)
        {
            int[] data = new int[] { 42, 20, 17, 27, 13, 8, 17, 48 };
            InsertSort(data);
            foreach (var item in data)
            {
                Console.Write(item + " ");
            }
        }
    }
}

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

namespace 简单选择排序
{
    class Program
    {
        static void SelectSort(int[] dataArray)
        {
            for (int i = 0; i < dataArray.Length-1; i++)
            {
                int min = dataArray[i];//最小值
                int minIndex = i;//最小值所在索引
                for (int j = i+1; j < dataArray.Length; j++)//找到最小的
                {
                    if (dataArray[j] < min)
                    {
                        min = dataArray[j];
                        minIndex = j;
                    }
                }
                if(minIndex!=i)
                {
                    int temp = dataArray[i];
                    dataArray[i] = dataArray[minIndex];
                    dataArray[minIndex] = temp;
                }
            }
        }
        static void Main(string[] args)
        {
            int[] data = new int[] { 42, 20, 17, 27, 13, 8, 17, 48 };
            SelectSort(data);
            foreach (var item in data)
            {
                Console.Write(item + " ");
            }
        }
    }
}

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

namespace 快速排序
{
    class Program
    {
        static void QuickSort(int[] dataArray,int left,int right)
        {
            if(left<right)
            {
                int x = dataArray[left]; //基准数  把比它小或者等于它的 放在它的左边,然后把比它大的放在它的右边
                int i = left;
                int j = right;//循环的标志位

                while (true && i<j)//当i==j说明我们找到中间位置,这个中间位置就是基准数应该所在的位置
                {
                    //从后往前比较(从右向左比较) 找一个比x小或者等于的数字,放在坑里,坑位于i的位置
                    while (true && i < j)
                    {
                        if (dataArray[j] <= x)
                        {
                            dataArray[i] = dataArray[j];
                            break;
                        }
                        else
                        {
                            j--; //向左移动 到下一个数字,然后做比较
                        }
                    }
                    //从前往后(从左向右)找一个比x大的数字,放在我们的坑里面 现在的坑位于j的位置
                    while (true && i < j)
                    {
                        if (dataArray[i] > x)
                        {
                            dataArray[j] = dataArray[i];
                            break;
                        }
                        else
                        {
                            i++; //向右移动 到下一个数字,然后做比较
                        }
                    }
                }
                //跳出循环 I==J  现在i==j i是中间位置
                dataArray[i] = x; // left--i--right
                QuickSort(dataArray, left, i - 1);
                QuickSort(dataArray, i+1, right);

            }
        }
        static void Main(string[] args)
        {
            int[] data = new int[] { 42, 20, 17, 27, 13, 8, 17, 48 };
            QuickSort(data, 0, data.Length-1);
            foreach (var item in data)
            {
                Console.Write(item + " ");
            }
        }
    }
}
原文地址:https://www.cnblogs.com/rongweijun/p/6260570.html