用C# 实现的几种排序算法

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

namespace Sort
{
    class Program
    {
        int n;
       static int count;
        int[] a = new int[50];
        public void Accept()
        {
            Console.WriteLine("Please input the number that you want to sort!");
            n = Convert.ToInt32 (Console.ReadLine());
            Console.WriteLine("input the elements one by one");
            for (int i = 0; i < n; i++)
            {
                a[i] = Convert.ToInt32(Console.ReadLine());
            }
        }
        public void Display()
        {
            for (int i = 0; i < n; i++)
            {
                Console.Write(a[i] + " ");
            }
            Console.Write("/n");
        }
        public void BubbleSort()
        {
            for (int i = 1; i < n; i++)
            {
                int swap = 0,temp;
                for (int j = 0; j < n - i; j++)
                {
                    if (a[j] > a[j + 1])
                    {
                        temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp;
                        swap = 1;
                    }
                }
                  Display();
                if (swap == 0) break;
              
            }
        }
        public void SelectionSort()
        {
            int min,temp;
            for (int i = 1; i < n; i++)
            {
                min = i - 1;
                for (int j = i; j < n ; j++)
                {
                    if(a[min]>a[j]) min=j;
                }
                if (min != i - 1)
                {
                    temp = a[i - 1];
                    a[i - 1] = a[min];
                    a[min] = temp;
                }
                Display();
            }

         }
        public void InsertionSort()
        {
            int temp,j;
            for (int i = 1; i < n; i++)
            {
                temp = a[i];
                for (j = i - 1; j >= 0 && temp < a[j]; j--)
                {
                    a[j + 1] = a[j];
                }
                a[j + 1] = temp;
                Display();
            }
        }
        public void ShellSort()
        {
            int i,j,temp;
            for (int incr = 3; incr >0; incr--)
            {
                for (int length = 0; length < incr; length++)
                {
                  
                   for ( i = length + incr;i < n ;i = i + incr)
                    {
                        temp = a[i];
                      for (j = i - incr;j >= 0 && a[j] > temp; j = j - incr)
                        {
                            a[j + incr] = a[j];
                          
                        }
                        a[j + incr] = temp;
                      
                    }
                 }
                Display();
            }
        }
        public void MergeSort(int low,int high)
        {
            int mid; mid = (low + high) / 2;
            if (low < high)
            {
                MergeSort(low, mid);
                MergeSort(mid + 1, high);
                Merge(low, mid, high);
                if (high == n - 1||high==n-2)
                {
                    Display();
                }
             }
          
        }
        public void Merge(int low, int mid, int high)
        {
            int[] arrTans = new int[high - low + 1];
            int k = 0,first=low,second=mid+1;
            for( ;first<=mid&&second<=high;)
            {
                if (a[first] < a[second])
                {
                    arrTans[k++] = a[first];
                    first++;
                }
                else
                {
                    arrTans[k++] = a[second];
                    second++;
                }
            }
            while (first <= mid)
            {
                arrTans[k++] = a[first];
                first++;
            }
            while (second <= high)
            {
                arrTans[k++] = a[second];
                second++;
            }
            for (int i = 0, m = low; i < arrTans.Length; i++, m++)
            {
                a[m] = arrTans[i];
            }
        }
        public void QuickSort(int low, int high,int n)
        {
            count++;
            int pivot,temp;
            if (low <=high)
            {  
                int i=low+1,j=high;   pivot = a[low];
               while(i <=j)
                {
                    while ((a[i] <= pivot) && (i <= high)) i++;
                    while ((a[j] > pivot) && (j >= low)) j--;
                    if (i < j) { temp = a[i]; a[i] = a[j]; a[j] = temp; }
                }
                if (low < j)
                {
                    temp = a[low]; a[low] = a[j]; a[j] = temp;
                 }
                if(n==2)
                   Display();
                QuickSort(low, j - 1,1); QuickSort(j + 1, high,2);
               
               
            }
        }
        static void Main(String[] args)
        {
            string choice = "";
            Program obj = new Program();
            obj.Accept();
            Console.Clear();
            do
            {
                Console.WriteLine("1- - Bubble Sort/n" +
                             "2- - Selection Sort/n" +
                             "3- - Insertion Sort/n" +
                             "4- -Hell Sort:Increments-3,2,1/n" +
                             "5- -Merge Sort/n" +
                             "6- -Quick Sort/n" +
                             "0- - Exit");
                choice = Console.ReadLine();
                if (choice == "1")
                {
                    Console.Clear();
                    obj.BubbleSort();
                }
                else if (choice == "2")
                {
                    Console.Clear();
                    obj.SelectionSort();
                  
                }
                else if (choice == "3")
                {
                    Console.Clear();
                    obj.InsertionSort();
                }
                else if (choice == "4")
                {
                    Console.Clear ();
                    obj.ShellSort();
                }
                else if (choice == "5")
                {
                    Console.Clear();
                    obj.MergeSort(0, obj.n - 1);
                }
                else if (choice == "6")
                {
                    Console.Clear();
                    obj.QuickSort(0, obj.n - 1, 2);
                }
               
            } while (choice != "0");
            Console.ReadLine();
        }
    }
}
     

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值