幾種排序算法的c#實現

幾種排序算法的實現

冒泡排序

  • 在要排序的一組數中,對當前還未排好序的範圍內的全部數,自上而下對相鄰的兩個數依次進行比較和調整
  • 讓較大的數往下沉,較小的往上冒。即:每當兩相鄰的數比較後發現它們的排序與排序要求相反時,就將它們互換.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Sort
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Person> persons=new List<Person>()
            {
                new Person(4),
                new Person(3),
                new Person(2),
                new Person(5)
            };
            BubbleSort(persons,Person.Compare);
            foreach (Person person in persons)
            {
                Console.WriteLine(person);
            }
            Console.ReadKey();
        }

        static void BubbleSort<T>(List<T> list,Func<T,T,bool> Compare)
        {
            for (int i = 0; i < list.Count; i++)
            {
                for (int j = 0; j < list.Count - i - 1; j++)
                {
                    if (Compare(list[j],list[j+1]))
                    {
                        T temp = list[j+1];
                        list[j + 1] = list[j];
                        list[j] = temp;
                    }
                }
            }
        }
        class Person
        {
            private int num;

            public Person(int Num)
            {
                num = Num;
            }

            public static bool Compare(Person p1, Person p2)
            {
                if (p1.num > p2.num)
                    return true;
                else
                {
                    return false;
                }
            }

            public override string ToString()
            {
                return num.ToString();
            }
        }

    }
}

選擇排序

  • 在要排序的一組數中,選出最小的一個數與第一個位置的數交換;
  • 然後在剩下的數當中再找最小的與第二個位置的數交換,如此循環到倒數第二個數和最後一個數比較爲止。
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SelectSort
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array=new int[4]{2,3,4,1};
            Sort(array);
            foreach (int temp in array)
            {
                Console.WriteLine(temp);
            }
            Console.ReadKey();
        }

        static void Sort(int[] array)
        {
            for (int i = 0; i < array.Length-1; i++)
            {
                int iTemp = array[i];
                int iPos = i;
                for (int j = i+1; j < array.Length; j++)
                {
                    if (array[j] < iTemp)
                    {
                        iTemp = array[j];
                        iPos = j;
                    }
                }
                array[iPos] = array[i];
                array[i] = iTemp;
            }
        }
    }
}

直接插入排序

  • 直接插入排序:在要排序的一組數中,假設前面(n-1) [n>=2] 個數已經是排好順序的
  • 現在要把第n個數插到前面的有序數中,使得這n個數也是排好順序的。如此反覆循環,直到全部排好順序。
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace InsertSort
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[4] { 2, 3, 4, 1 };
            //不能再靜態方法裏調用非靜態成員
            Sort(array);
            foreach (int temp in array)
            {
                Console.WriteLine(temp);
            }
            Console.ReadKey();
        }

        static void Sort(int[] array)
        {
            //外層循環標示並決定待比較的數值(array[i])
            for (int i = 1; i < array.Length; i++)
            {
                int j = i - 1;
                int temp = array[i];
                //內層循環爲待比較的數值確定其最終位置,當前一數字(array[j])比待比較的數值大的情況下繼續循環,
                //直到找到比待比較數值小的並將待比較數值置入其後一位置,結束該次循環
                for (; j >= 0 && array[j]>temp; j--)
                {
                    array[j + 1] = array[j];
                }
                array[j + 1] = temp;
            }
        }
    }
}

快速排序

  • 快速排序:快速排序是對冒泡排序的一種改進,它的基本思想是通過一趟掃描後,使得排序序列的長度能大幅減小
  • 快速排序通過一遍掃描,就能確保某個數(以它爲基準點)的左邊各數都比他小,右邊各數都比它大,
  • 然後又用同樣的方法處理它左右兩邊的數,直到基準點左右只有一個元素爲止。
using System.Collections.Generic;
using System.Diagnostics.Eventing.Reader;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuickSort
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[4] { 2, 3, 4, 1 };
            //Sort(array,0,array.Length-1);
            Stack<int> stack=new Stack<int>();
            stack.Push(0);
            stack.Push(array.Length-1);
            while (stack.Count>0)
            {
                int low = stack.Pop();
                int high = stack.Pop();
                if (low > high)
                {
                    int temp = low;
                    low = high;
                    high = temp;
                }
                Sort(array,low,high,stack);
            }
            foreach (int temp in array)
            {
                Console.WriteLine(temp);
            }
            Console.ReadKey();
        }

        static int Partition(int[] array,int left,int right)
        {
            int pivot = array[left];//將首元素作爲樞紐
            while (left<right)
            {
                //從右到左,尋找首個小於pivot的元素
                while (array[right] >= pivot && left < right)
                    right--;
                //執行到此,right已指向從右端其首個小於pivot的元素
                //執行替換
                array[left] = array[right];
                //從左向右,尋找首個大於pivot的元素
                while (array[left] <= pivot && left < right)
                    left++;
                //執行到此,left已指向從左端起首個大於pivot的元素
                //執行替換
                array[right] = array[left];
            }
            //退出while循環,執行至此,必定是left=right的情況
            //left(或right)值向的即是樞紐位置,定位該趟排序的樞紐並將位置返回
            array[left] = pivot;
            return left;
        }

        //快速排序的遞歸實現
        //static void Sort(int[] array, int left, int right)
        //{
        //    if (left<right)
        //    {
        //        int middle = Partition(array, left, right);
        //        //對樞紐左端排序
        //        Sort(array,left,middle-1);
        //        //對樞紐右端排序
        //        Sort(array,middle+1,right);
        //    }
        //}

        //快速排序的非遞歸實現
        static void Sort(int[] array, int left, int right, Stack<int> stack)
        {
             int low = left; 
            int high = right; 
            int temp = array[low];
            while (high > low)
            {
                while (low < high && temp <= array[high])
                {
                    high--;
                }
                if (high > low)
                {
                    array[low] = array[high];
                    array[high] = temp;
                }
                while (low < high && temp >= array[low])
                {
                    low++;
                }
                if (high > low)
                {
                    array[high] = array[low];
                    array[low] = temp;
                }
                if (low == high)
                {
                    if (left < low - 1)
                    {
                        stack.Push(left);
                        stack.Push(low - 1);
                    }
                    if (right > low + 1)
                    {
                        stack.Push(low + 1);
                        stack.Push(right);
                    }
                }
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章