C#冒泡排序法

http://hi.baidu.com/cherryli2008/blog/item/023fba76f8d27e1db151b91f.html 

现在的许多IT公司在面试的时候,都会有类似于手写代码题一类的考试,其中有两道题是出现几率非常大的,几乎成了所有IT企业的必考题.所以,在这里我自己写了一下这两个题的C#代码具体实现,希望对有些人的面试有所帮助(其实许多场合这两道题起着决定性作用).

        当然,许多公司在机试的时候,允许应聘者上网搜集资料,如果你正好遇到这样的公司而且遇到这样的题目了,哈哈~~那就来我的博客吧,直接复制以下代码,把类文件里的代码全部替换,将是100分答案.

        嘿嘿,当然我这里也有很多代码都是为了搔情,不是核心代码,只是为了让人体验更好而已,没什么大怪了.

        我的<刚刚冒泡排序法>的具体代码如下:

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

namespace GanggangApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            SortedNumbers();
        }

        /// <summary>
        /// 该方法获得需要排序的数组,表调用排序方法进行排序
        /// </summary>
        public static void SortedNumbers()
        {
            int numberCount;
            int[] numbers;
            Console.WriteLine("----------刚刚冒泡排序法----------");
            Console.WriteLine("请问您要对多少为数字进行排序?");
            numberCount = Convert.ToInt32(Console.ReadLine());
            numbers = new int[numberCount];
            Console.WriteLine("请输入您要进行排序的这{0}个数字:",numberCount);
            for (int i = 0; i < numberCount; i++)
            {
                numbers[i] = Convert.ToInt32(Console.ReadLine());
            }

            Console.WriteLine("\n您要进行排序的{0}个数字分别为:",numberCount);
            for (int i = 0; i < numberCount; i++)
            {
                Console.Write(numbers[i].ToString() + "\t");
            }

            Console.WriteLine("\n您要对这{0}个数字进行什么排序?(1表示升序,2表示降序)",numberCount);
            int method = Convert.ToInt32(Console.ReadLine());

            while (method != 1 && method != 2)
            {
                Console.WriteLine("只能输入1或者2,请您重新输入!");
                method = Convert.ToInt32(Console.ReadLine());
            }

            //调用排序方法
            ExecuteSortedMethod(numbers, method);

            Console.WriteLine("排序后的结果为:");
            for (int i = 0; i < numberCount; i++)
            {
                Console.Write(numbers[i].ToString() + "\t");
            }
            Console.WriteLine("\n----------刚刚冒泡排序法----------");
            Console.ReadKey();
        }

        /// <summary>
        /// 接受数字参数和排序方法参数,进行冒泡排序
        /// </summary>
        /// <param name="num">要进行排序的整数数组</param>
        /// <param name="sortedMethod">排序方法标识:1为升序,2为降序</param>
        public static void ExecuteSortedMethod(int[] num, int sortedMethod)
        {
            if (sortedMethod == 1)      //升序排列
            {
                for (int i = 0; i < num.Length -1; i++)
                {
                    for (int j = 0; j < num.Length - 1 - i; j++)
                    {
                        if (num[j] > num[j + 1])
                        {
                            int temp = num[j];
                            num[j] = num[j + 1];
                            num[j + 1] = temp;
                        }
                    }
                }
            }
            if (sortedMethod == 2)      //降序排列
            {
                for (int i = 0; i < num.Length - 1; i++)
                {
                    for (int j = 0; j < num.Length - 1 - i; j++)
                    {
                        if (num[j] < num[j + 1])
                        {
                            int temp = num[j];
                            num[j] = num[j + 1];
                            num[j + 1] = temp;
                        }
                    }
                }
            }
        }

    }
}

运行结果如下图:

其实这里面的核心代码只有几行,就是如下:

                for (int i = 0; i < num.Length - 1; i++)
                {
                    for (int j = 0; j < num.Length - 1 - i; j++)
                    {
                        if (num[j] > num[j + 1])
                        {
                            int temp = num[j];
                            num[j] = num[j + 1];
                            num[j + 1] = temp;
                        }
                    }
                }

posted @ 2010-05-11 15:20  TC-MrLee  阅读(551)  评论(0编辑  收藏  举报