它重复地走访过要排序的元素列,依次比较两个相邻的元素,如果他们的顺序(如从大到小、首字母从A到Z)错误就把他们交换过来。走访元素的工作是重复地进行直到没有相邻元素需要交换,也就是说该元素列已经排序完成。
这个算法的名字由来是因为越大的元素会经由交换慢慢“浮”到数列的顶端(升序或降序排列),就如同碳酸饮料中二氧化碳的气泡最终会上浮到顶端一样,故名“冒泡排序”。
第一种实现方法:
Default2.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TextBox ID="sa" runat="server"></asp:TextBox> <asp:Button ID="sa1" Text="冒泡排序正序" runat="server" OnClick="sa1_Click" /> <asp:Button ID="sa2" Text="冒泡排序倒序" runat="server" OnClick="sa2_Click" /> </div> </form> </body> </html>
Default2.aspx.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class Default2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void sa1_Click(object sender, EventArgs e) { string[] iNums = Convert.ToString(sa.Text.Trim()).Split(','); int[] split = Array.ConvertAll<string, int>(iNums, int.Parse); //正序排列: for (int i = 0; i < split.Length - 1; i++)//外层循环控制循环次数 { for (int j = 0; j < split.Length - i - 1; j++)//内层循环用于交换相邻要素 { int temp; if (split[j] >split[j + 1]) { temp =split[j + 1]; split[j + 1] = split[j]; split[j] =temp; } } } foreach (int outint in split) { Response.Write(+outint + "\t"); } } protected void sa2_Click(object sender, EventArgs e) { string[] iNums = Convert.ToString(sa.Text.Trim()).Split(','); int[] split = Array.ConvertAll<string, int>(iNums, int.Parse); //倒序排列: for (int i = 0; i < split.Length - 1; i++)//外层循环控制循环次数 { for (int j = 0; j < split.Length - i - 1; j++)//内层循环用于交换相邻要素 { int temp; if (split[j] < split[j + 1]) { temp = split[j]; split[j] = split[j + 1]; split[j + 1] = temp; } } } foreach (int outint in split) { Response.Write(+outint + "\t"); } } }
结果如下:
正序
倒序
第二种实现方法(控制台方法):
using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; namespace ConsoleApp1 { class Program { static void Main(string[] args) { //接收用户输入 Console.WriteLine("请输入5个正整数:"); int[] myArray = new int[5]; for (int i = 0; i < myArray.Length; i++) { myArray[i] = int.Parse(Console.ReadLine());//循环输入5个正整数,enter切换 } //正序排列: for (int i = 0; i < myArray.Length - 1; i++)//外层循环控制循环次数 { for (int j = 0; j < myArray.Length - i - 1; j++)//内层循环用于交换相邻要素 { int temp; if (myArray[j] > myArray[j + 1]) { temp = myArray[j + 1]; myArray[j + 1] = myArray[j]; myArray[j] = temp; } } } Console.WriteLine("正序序排列为:"); foreach (int outint in myArray) { Console.Write(+outint + "\t"); } Console.WriteLine(); //倒序排列为: for (int i = 0; i < myArray.Length - 1; i++)//外层循环控制循环次数 { for (int j = 0; j < myArray.Length - i - 1; j++)//内层循环用于交换相邻要素 { int temp; if (myArray[j] < myArray[j + 1]) { temp = myArray[j]; myArray[j] = myArray[j + 1]; myArray[j + 1] = temp; } } } Console.WriteLine("倒序排列为:"); foreach (int outint in myArray) { Console.Write(outint + "\t"); } Console.ReadKey(); } } }
结果如下:
来源:https://www.cnblogs.com/tianranbai/p/10750419.html