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]);               
                }
            }
        }
    }

  • 相关阅读:
    Go语言实现:【剑指offer】剪绳子
    delphi10.3安装使用mySQL
    uniGUI学习之把窗口分成左,右边(上下)三部分,并且在运行中可以动态调节其相对大小(36)
    uniGUI学习之UniStringGrid(35)
    uniGUI之主窗口折叠UI之UniTreeMenu(32-2)
    好网站
    ios图片
    ios启动图的相关问题
    自学php
    Parse error: syntax error, unexpected $end in diguoclassfunctions.php on line 1246
  • 原文地址:https://www.cnblogs.com/aspsmile/p/1260588.html
Copyright © 2011-2022 走看看