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

  • 相关阅读:
    hdu1561--树形dp<依赖背包>
    hdu--1520--树形dp<写起来就是深搜啊>-<滚动数组优化>
    hdu--1595-另类最短路
    hdu--1599--最小环<会加深你对floyd的理解>
    hdu--1851--尼姆博弈&&巴什博弈<也有人用了sg可惜我还不懂>
    hdu--4920--原来一直写了速度慢的矩阵乘法
    hdu--4912--终于解脱了
    hdu--4947--我太天真了
    hdu--2576--高中数学..
    hdu--2579--第二次与女孩的约会
  • 原文地址:https://www.cnblogs.com/aspsmile/p/1260588.html
Copyright © 2011-2022 走看看