冒泡算法

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] array = { 1, 2, 3, 4, 5, 6 };

            bubble_sort(array, SortType.rise);

            for (int i = 0; i < array.Length; i++)
            {
                Console.WriteLine(array[i]);
            }


            Console.ReadKey();

        }


        //冒泡算法
        static void bubble_sort(int[] unsorted, SortType type) 
        {
            if (type == SortType.rise)
            {
                for (int i = 0; i < unsorted.Length; i++)
                {
                    for (int j = i; j < unsorted.Length; j++)
                    {
                        if (unsorted[i] > unsorted[j])
                        {
                            int temp = unsorted[i];
                            unsorted[i] = unsorted[j];
                            unsorted[j] = temp;
                        }
                    }
                }
            }
            else if(type == SortType.drop) 
            {
                for (int i = 0; i < unsorted.Length; i++)
                {
                    for (int j = i; j < unsorted.Length; j++)
                    {
                        if (unsorted[i] < unsorted[j])
                        {
                            int temp = unsorted[i];
                            unsorted[i] = unsorted[j];
                            unsorted[j] = temp;
                        }
                    }
                }
            }


        }

    }

    public enum SortType 
    {
        drop,
        rise
    }
    

}

 

原文地址:http://www.cnblogs.com/kkun/archive/2011/11/23/2260280.html

 

posted @ 2014-12-06 16:35  盘子脸  阅读(283)  评论(0编辑  收藏  举报