C# 冒泡排序

黄舟
Libérer: 2017-02-09 16:24:05
original
1417 Les gens l'ont consulté

 C# 冒泡排序

using System;  
using System.Collections.Generic;  
using System.Linq;  
using System.Text;  
namespace Sort  
{  
    class BubbleSorter  
    {  
        public static int[] Sort(int[] a)  
        {  
            BubbleSort(a);  
            return a;  
        }  
        private static void BubbleSort(int[] myArray)  
        {  
            for (int i = 0; i < myArray.Length; i++)//循环的趟数  
            {  
                for (int j = 0; j < myArray.Length - 1- i; j++)//每次循环的次数  
                {  
        //比较相邻元素,将值大的后移==》每一趟循环结束==》最后一个数是最大的。  
        //所以每次循环都比上一次循环的个数少1,即j < myArray.Length - 1- i  
                    if (myArray[j] > myArray[j + 1])  
                    {  
                        Swap(ref myArray[j], ref myArray[j + 1]);  
                    }  
                }  
            }  
        }  
    //引用参数与值参数  
        private static void Swap(ref int left, ref int right)  
        {  
            int temp;  
            temp = left;  
            left = right;  
            right = temp;  
        }  
    }  
}  
冒泡排序算法的运作如下:
Copier après la connexion

冒泡排序算法的运作如下:

1、比较相邻的元素。如果第一个比第二个大,就交换他们两个。

2、对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。==》最后一个数就不需要比较了,因为它就是最大的。

3、针对所有的元素重复以上的步骤,除了最后一个。

4、持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。

以上就是 C# 冒泡排序的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!