zoukankan      html  css  js  c++  java
  • 冒泡排序法

    • using System;
    • using System.Collections.Generic;
    • using System.Text;
    • namespace 程序算法
    • {
    •     /// <summary>
    •     /// 就是将一组数中最大的挑出来,像气泡一样向上升
    •     /// 
    •     /// 过程:
    •     /// 首先将一组数组中的第1个数与第2个数进行比较,若为逆序,则将连个记录交换,然后第2个与第3个数,以此类推,直至第n-1个数与第n个数比较完,将
    •     /// </summary>
    •     publicclass 冒泡算法
    •     {
    •         publicvoid Sort(int[] arr)
    •         {
    •             int temp;           
    •             for (int i = 0; i < arr.Length; i++) //
    •             {
    •                 for (int j = 0; j < arr.Length - i-1; j++)
    •                 {
    •                     //if (arr[j] > arr[j + 1]) //和自己后面的一个元素进行比较
    •                     //{
    •                     //    temp = arr[j];
    •                     //    arr[j] = arr[j + 1];
    •                     //    arr[j + 1] = temp;
    •                     //}
    •                     /*倒序排列*/
    •                     if (arr[j] < arr[j + 1])
    •                     {
    •                         temp = arr[j + 1];
    •                         arr[j + 1] = arr[j];
    •                         arr[j] = temp;
    •                     }
    •                 }
    •             }
    •         }
    •         publicstaticvoid Main(string[] args)
    •         {
    •             冒泡算法 maoPao = new 冒泡算法();
    •             int[] array = newint[] { 1, 3, 2, 10, 13, 16, 42, 36, 77, 37, 99, 100 };
    •             maoPao.Sort(array);
    •             foreach (int k in array)
    •             {
    •                 Console.Write(k);
    •                 Console.Write(" ");              
    •             }
    •             Console.WriteLine();
    •         }       
    •     }
    • }

  • 相关阅读:
    linux中iptables的用法
    Git介绍及安装配置
    第一个shell脚本
    Nginx配置优化解读
    Python中print格式化输出
    python 程序构架浅析
    Python 常用字符串操作
    Python入门学习:网络刷博器爬虫
    vSphere SDK for Java
    vROPS中获取虚拟机在VC中的UUID
  • 原文地址:https://www.cnblogs.com/cyjch/p/2420886.html
Copyright © 2011-2022 走看看