冒泡排序——算法

计算机经典算法 ——冒泡排序(降序排序)

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace SortTest
 7 {
 8     class Program
 9     {
10         /*
11          * 降序排列
12          */
13         static void Sort(int[] iNum)
14         {
15             for (int i = 0; i < iNum.Length;i++ )
16             {
17                 for (int j=i+1;j<iNum.Length;j++)
18                 {
19                     int temp =0;
20                     if (iNum[i]<iNum[j])
21                     {
22                         temp = iNum[i];
23                         iNum[i] = iNum[j];
24                         iNum[j] = temp;
25                     }
26                 }
27             }
28         }
29         static void PrintNumber(int[] iNums)
30         {
31             Console.Write("数组的顺序: ");
32             foreach (var iNum in iNums)
33             {
34                 Console.Write(iNum+ " ");
35             }
36             Console.WriteLine();
37         }
38         static void Main(string[] args)
39         {
40             int[] iNumber = { 9, 6, 7, 8};
41             PrintNumber(iNumber);
42             Sort(iNumber);
43             PrintNumber(iNumber);
44             Console.ReadKey();
45         }
46     }
47 }

 2.冒泡排序(升序排序)

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

namespace AscendingSort
{
    class Program
    {
        /************************************************************************/
        /* 升序排列                                                             */
        /************************************************************************/
        static void AscSort(int[] iNum)
        {
            for (int i = 0; i < iNum.Length;i++ )
            {
                for (int j = iNum.Length - 1; j > i; j--)
                {
                    if (iNum[i]>iNum[j])
                    {
                        int temp = 0;
                        temp = iNum[i];
                        iNum[i] = iNum[j];
                        iNum[j] = temp;
                    }
                }
            }
        }
        static void PrintNumber(int[] iNums)
        {
            Console.Write("数组的顺序: ");
            foreach (var iNum in iNums)
            {
                Console.Write(iNum + " ");
            }
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            int[] iNumber = { 9, 6, 7, 8 };
            PrintNumber(iNumber);
            AscSort(iNumber);
            PrintNumber(iNumber);
            Console.ReadKey();
        }
    }
}

 另一种升序排序写法

/// <summary>
        /// 升序排列
        /// </summary>
        /// <param name="iNum"></param>
        static void Bubble(int[] iNum)
        {
            int temp = 0;
            for (int i = iNum.Length; i > 0; i--)
            {
                for (int j = 0; j < i - 1; j++)
                {
                    if (iNum[j] > iNum[j + 1])
                    {
                        temp = iNum[j];
                        iNum[j] = iNum[j + 1];
                        iNum[j + 1] = temp;
                    }
                }
            }
        }

 

posted @ 2016-07-05 19:39  流星小子  阅读(203)  评论(0编辑  收藏  举报