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

  • 相关阅读:
    Codeforces 777B Game of Credit Cards
    Codeforces 777A Shell Game
    零基础学贪心算法
    1283 最小周长
    容斥原理
    Humble Numbers(丑数) 超详解!
    1284 2 3 5 7的倍数
    1305 Pairwise Sum and Divide
    1347 旋转字符串
    HDU 2549 壮志难酬
  • 原文地址:https://www.cnblogs.com/aspsmile/p/1260588.html
Copyright © 2011-2022 走看看