快速排序,冒泡排序,插入排序 完整示例

 快速排序

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

using System.Text;

namespace ConsoleApplication12 {     class Program     {         static void Main(string[] args)         {             ArrayList at = new ArrayList();             int[] number = new int[] { 5679, 4, 36, 67976, 124, 235, 453, 436, 6589, 0, 5, 6, 346, 457, 34, 3642 };             foreach (int i in number)             {                 at.Add(i);             }             ArrayList al = QuickSort(at);             foreach (int j in al)             {                 Console.WriteLine(j);             }             Console.ReadKey();         }         static ArrayList QuickSort(ArrayList arrayList)         {             if (arrayList.Count <= 1) return arrayList;             int pivotIndex = arrayList.Count / 2;             int pivot = (int)arrayList[pivotIndex];             arrayList.Remove(pivot);             ArrayList left = new ArrayList();             ArrayList right = new ArrayList();             for (int i = 0; i < arrayList.Count; i++)             {                 // 改變排序方式, 只要改變這裏的比較方式                          if ((int)arrayList[i] < pivot)                 {                     left.Add(arrayList[i]);                 }                 else                 {                     right.Add(arrayList[i]);                 }             }             ArrayList newArrayList = QuickSort(left);             newArrayList.Add(pivot);             newArrayList.AddRange(QuickSort(right));             return newArrayList;         }

    } }

 

冒泡排序

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

using System.Text;

namespace ConsoleApplication12
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] number=new int[]{5679,4,36,67976,124,235,453,436,6589,0,5,6,346,457,34,3642};
            QuickSort(ref number);
            foreach (int j in number)
            {
                Console.WriteLine(j);
            }
            Console.ReadKey();
        }
        static void QuickSort(ref int[] number)
        {       
            for (int i = 0; i < number.Length; i++)
            {         
                // 改變排序方式, 只要改變這裏的比較方式         
                for (int j = 0; j < number.Length - i-1; j++)
                {
                    if (number[j]>number[j+1])
                    {
                        int temp = number[j];
                        number[j] = number[j + 1];
                        number[j + 1] = temp;
                    }
                }
            }     
        }

    }
}

 

插入排序

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

using System.Text;

namespace ConsoleApplication12
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] number=new int[]{5679,4,36,67976,124,235,453,436,6589,0,5,6,346,457,34,3642};
            QuickSort(ref number);
            foreach (int j in number)
            {
                Console.WriteLine(j);
            }
            Console.ReadKey();
        }
        static void QuickSort(ref int[] number)
        {       
            for (int i = 1; i < number.Length; i++)
            {
                int temp = number[i];
                int j=i;
                while (j>0 &&number[j-1]>temp)
                {
                    number[j] = number[j-1];
                    --j;
                }
                number[j] = temp;  
            }     
        }

    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章