Sort of exchange

1: Bubble Sort

Bubble Sort is a comparison between adjacent elements, if it is relatively front to back, you can put a maximum of the last element into the array, the next cycle in addition to the maximum number of one, the maximum number of remaining find out, followed by cycle, to sort the array

the using the System;
 the using the System.Collections.Generic;
 the using the System.Diagnostics; // namespace class StopWatch 
the using the System.Linq;
 the using the System.Text;
 the using System.Threading.Tasks; 

namespace direct insertion sort 
{ 
    class Program 
    { 
        static  void the Main ( String [] args) 
        { 
            int [] Array = new new  int [] { 42 is , 20 is , 99 , 12 is , 44 is , . 4 , 66, 88 , 24 , 93 , 1 , 22 }; // needs sorted array 
            The Stopwatch Watch = new new The Stopwatch (); // create an object class StopWatch 
            watch.Start (); // call the class start function to start timing       
            Bubbing (array); // sort the array using bubble sort 
            watch.Stop (); // call StopWatch class stop function, its termination timing 
            ; Console.WriteLine (watch.Elapsed) // output by the direct insertion sort 
            the foreach ( var Item in array) // output nearly the sorted array.
             {
                Console.WriteLine (Item); 
            } 
            the Console.ReadKey (); 
        } 
                            ///  <Summary> 
        /// bubble sort, each good first maximum number of rows in the array
         ///  </ Summary> 
        ///  < name = param "Array"> </ param> 
        public  static  void Bubbing ( int [] Array) 
        { 
            for ( int I = 0 ; I <array.Length- . 1 ; I ++) // record the number of the number of sorting almost good the 
            {
                 for ( int J = 0 ; J <array.length-I- . 1 ; J ++)// logarithm of remaining sort 
                {
                     IF (Array [J]> Array [J + . 1 ]) // adjacent comparison between the two elements, the larger elements put back 
                    {
                         int TEMP = Array [J] ; 
                        Array [J] = Array [J + . 1 ]; 
                        Array [J + . 1 ] = TEMP; 
                    } 
                } 
            } 
        } 
    } 
}

 

2: Quick Sort

 

Providing two array pointers high low, a pointer to the first number of the array and the last array, low number of points as a reference value, smaller than the first reference value to find the number in front of the high position into the low number of the position, in place from the original low

 Find a number larger than the reference value, put in place a number of exchange. And so on until the middle position vacated array, the reference value into that position. In the array is divided into two arrays, this recursive function to implement quicksort

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

namespace 快速排序
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = new int[] { 42, 20, 99, 12, 44, 4, 66, 88, 24, 93, 1, 22 };//需要排序的数组
            Stopwatch watch = new Stopwatch();//创建StopWatch类的对象
            watch.Start();//调用类中的start函数开始计时      
            QuickSort(0,array.Length-1,array);//用快速排序算法对数组进行排序
            watch.Stop();//调用StopWatch类中的stop函数,终止计时
            Console.WriteLine(watch.Elapsed);//输出直接插入排序的用时
            foreach (var item in array)//输出已近排序好的数组
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
        /// <summary>
        /// 快速排序,确定数组的基准位,大于他的放在他的右边,小于他的放在他的左边
        /// </summary>
        /// <param name="left"></param>
        /// <param name="right"></param>
        /// <param name="array"></param>

        private static void QuickSort(int left,int right,int[] array)
        {
            if(left>=right)//设置递归函数终止条件,当数组只有一个数时,终止回调
            {
                return;
            }
            int i = left;
            int j = right;
            int temp = array[left];
            if(left<right)
            {
                while(i<j)
                {
                    while(i<j)//从数组尾部找比基准值小的数,放在数组首部空出的位置
                    {
                        if(temp>array[j])//查找到就终止循环,把数放在数组首部空出的位置
                        {
                            array[i] = array[j];
                            break;
                        }
                        else//这个数不小于基准数就左移数组一位,用来判断
                        {
                            j--;
                        }
                    }
                    while(i<j)//找比数组大的数
                    {
                        if(temp<array[i])//找到放在数组右边空的位置上,结束查找
                        {
                            array[j] = array[i];
                            break;
                        }
                        else//没找到就继续找
                        {
                            i++;
                        }
                    }
                }
            }
            array[i] = temp;//把大于基准数的放在基准数的右边,小于基准数的放在左边,基准数的位置根据他左右边数的个数确定
            QuickSort(left,i-1,array);//这次执行确定了基准数在数组中排序的位置,在递归函数确定基准数左边的数位置
            QuickSort(i+1 , right, array);//递归函数确定基准数右边数的位置

        }
    }
}

 

Guess you like

Origin www.cnblogs.com/zhangyang4674/p/11333093.html