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]);
    }
    }
  • 相关阅读:
    钢镚儿冲刺一周期第七天
    代码大全(第二版)阅读笔记01
    组队开发——地铁路线查询
    学习进度——第五周
    学习进度——第四周
    返回一个整数数组中最大子数组的和(文件)
    学习进度——第三周
    返回一个整数数组中最大子数组的和
    自我介绍
    学习进度——第二周
  • 原文地址:https://www.cnblogs.com/wz327/p/1739495.html
Copyright © 2011-2022 走看看