您的位置:首页 > 职场人生

面试准备几个基础的算法

2018-03-01 16:38 387 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sort1
{
    class Program
    {
        //选择排序
        public static int[] SelectSort(int[] a)
        {
            int smallest;
            int i;
            int j;
            for (i = 0; i < a.Length - 1; i++) 
            {
                smallest = i;
                for (j = i + 1; j < a.Length; j++) 
                {
                    if (a[j] < a[smallest]) 
                    {
                        smallest = j;
                    }
                }
                Swap(ref a[smallest], ref a[i]);
            }
                return a;
        }

        //冒泡排序
        public static int[] BubbleSort(int[] a) 
        {
            bool flag;
            int m = 0;
            int n = 0;
            for (int i = 0; i < a.Length - 1; i++) 
            {
                flag = true;
                if (i % 2 == 0)
                {
                    for (int j = n; j < a.Length - 1 - m; j++)
                    {
                        if (a[j] < a[j + 1])
                        {
                            Swap(ref a[j], ref a[j + 1]);
                            flag = false;
                        }
                    }
                    if (flag)
                        break;
                    m++;
                }
                else 
                {
                    for (int k = a.Length - 1 - m; k >n; k--)
                    {
                        if (a[k] > a[k - 1])
                        {
                            Swap(ref a[k], ref a[k - 1]);
                            flag = false;
                        }
                    }
                    if (flag)
                        break;
                    n++;
                }
             
            }
                return a;
        }

        //插入排序
        public static void InsertSort(int[] a) 
        {
            int temp;
            for (int i = 1; i < a.Length; i++) 
            {
                temp = a[i];
                for (int j = i - 1; j >= 0;j-- )
                {
                    if (a[j] > temp)
                    {
                        a[j + 1] = a[j];
                        if (j == 0)
                        {
                            a[0] = temp;
                            break;
                        }
                    }
                    else 
                    {
                        a[j + 1] = temp;
                        break;
                    }
                }

            }
        }

        //快速排序
        public static void QuickSort(int[] a,int low,int high)
        {
            if (low >= high) return;
            int temp = a[low];
            int i=low+1;
            int j=high;

            while (true)
            {
                while (a[j] > temp) j--;
                while (a[i] < temp && i < j) i++;
                if (i >= j) break;
                Swap(ref a[j], ref a[i]);
                i++;
                j--; 
                
            }

            if (j != low) 
            {
                Swap(ref a[j], ref a[low]);
            }

            QuickSort(a,j+1,high
4000
);
            QuickSort(a, low, j - 1);        
        }

        public static void Swap(ref int p1, ref int p2)
        {
            int temp;
            temp = p1;
            p1 = p2;
            p2 = temp;
        }

        static void Main(string[] args)
        {
            int[] a = { 10, 2, 14, 4, 6,12,24,56,33,46,98,21,26,13,17,88 };
            //QuickSort(a,0,a.Length-1);
            InsertSort(a);
            for (int i = 0; i < a.Length; i++) 
            {
                Console.WriteLine(a[i]);
            }
            Console.Read();
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: