zoukankan      html  css  js  c++  java
  • 冒泡排序的几种实现方式

    代码
    /// <summary>
    /// 把最大的依次换到最下方
    /// </summary>
    protected void BobbleSortUpToDown()
    {

    int[] s = { 10,20,30,40,50,60 };
    for (int i = 0; i < s.Length - 1; i++)
    {

    bool exchange = false;
    Response.Write(i.ToString()
    + ":");
    for (int j = 0; j < s.Length - i - 1; j++)
    {
    if (s[j] > s[j + 1])
    {
    int temp = s[j];
    s[j]
    = s[j + 1];
    s[j
    + 1] = temp;

    exchange
    = true;
    }
    Response.Write(j.ToString());
    }
    Response.Write(
    "<br/>");
    if (!exchange)
    return;
    }
    for (int i = 0; i < s.Length; i++)
    {
    Response.Write(s[i].ToString());
    }


    }


    /// <summary>
    /// 把最小的依次换到最上方
    /// j向上走
    /// </summary>
    protected void BobbleSortDownToUp()
    {

    int[] s = { 50,60 , 40, 30, 20, 10 };
    for (int i = 0; i < s.Length - 1; i++)
    {
    Response.Write(
    "<br/>"+i.ToString() + ":");
    for (int j = s.Length - 2; j >=i; j--)
    {
    if (s[j] > s[j + 1])
    {
    int temp = s[j];
    s[j]
    = s[j + 1];
    s[j
    + 1] = temp;
    }
    Response.Write(j.ToString());
    }
    Response.Write(
    "<br/>");
    }
    for (int i = 0; i < s.Length; i++)
    {
    Response.Write(s[i].ToString());
    }


    }

    /// <summary>
    /// 把最小的放在上面
    /// j向下走
    /// </summary>
    protected void BobbleSortDownToUp3()
    {


    int[] list = { 19, 11, 15, 12, 55, 90, 60 };
    for (int i = 0; i < list.Length; i++)
    {
    for (int j = i + 1; j < list.Length; j++)
    {
    if (list[i] > list[j])
    {
    int temp = list[i];
    list[i]
    = list[j];
    list[j]
    = temp;
    }
    }
    }
    for (int i = 0; i < list.Length; i++)
    {
    Response.Write(list[i]);
    }
    }
  • 相关阅读:
    light oj 1105 规律
    light oj 1071 dp(吃金币升级版)
    light oj 1084 线性dp
    light oj 1079 01背包
    light oj 1068 数位dp
    light oj 1219 树上贪心
    light oj 1057 状压dp TSP
    light oj 1037 状压dp
    矩阵快速幂3 k*n铺方格
    矩阵快速幂2 3*n铺方格
  • 原文地址:https://www.cnblogs.com/wz327/p/1739495.html
Copyright © 2011-2022 走看看