zoukankan      html  css  js  c++  java
  • 冒泡排序

    其基本思想是:

    将被排序的记录数组R[1..n]垂直排列,每个记录R看作是重量为R.key的气泡。根据轻气泡不能在重气泡之下的原则,从下往上扫描数组R:凡扫描到违反本原则的轻气泡,就使其向上"飘浮"。如此反复进行,直到最后任何两个气泡都是轻者在上,重者在下为止。

    测试:

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

    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] iArray ={ 10, 5, 2, 4, 3, 6, 7, 9, 1, 8 };
                BubbleSort(iArray);
            }

            public static void BubbleSort(int[] arr)
            {
                int temp=0;
                int exchange = 0;

                for (int i = 0; i < arr.Length; i++)
                {
                    for (int j = i + 1; j < arr.Length; j++)
                    {
                        if (arr[j] < arr[i])
                        {
                            temp = arr[i];

                            arr[i] = arr[j];
                            arr[j] = temp;

                            exchange++;
                        }
                    }
                }

                Console.WriteLine("Exchange time is " + exchange + ".");

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

  • 相关阅读:
    bzoj4105: [Thu Summer Camp 2015]平方运算
    bzoj4035: [HAOI2015]数组游戏
    bzoj1022: [SHOI2008]小约翰的游戏John
    bzoj4665: 小w的喜糖
    CodeChef:Little Elephant and Colored Coins
    bzoj4664: Count
    bzoj4498: 魔法的碰撞
    bzoj4230: 倒计时
    bzoj4532: [BeiJing2014 WinterCamp] 珠链
    python 画正态曲线
  • 原文地址:https://www.cnblogs.com/aspsmile/p/1260588.html
Copyright © 2011-2022 走看看