zoukankan      html  css  js  c++  java
  • 冒泡排序(C#数据结构学习九)

    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace SoloDataStructure
    {
        
    class MyBubbleSort
        
    {
            
    //冒泡排序
            static void BubbleSort(int[] arr)
            
    {
                
    int n = arr.Length;
                
    for (int i = 0; i < n - 1; i++)  //做n-1趟排序
                {
                    
    bool noswap = true;    //未交换标志
                    for (int j = n - 1; j > i; j--)
                    
    {
                        
    if (arr[j] < arr[j-1])
                        
    {
                            
    //swap记录
                            int temp = arr[j];
                            arr[j] 
    = arr[j-1];
                            arr[j
    -1= temp;
                            noswap 
    = false;
                        }

                    }

                    
    if (noswap)
                        
    return;
                }

            }

            
    static void Main(string[] args)
            
    {
                
    int[] arr = new int[] {99,884,9,2,77,32,11,46};
                BubbleSort(arr);
                Console.Write(
    "Data After BubbleSort:");
                
    foreach (int i in arr)
                
    {
                    Console.Write(i
    +",");
                }

                Console.ReadLine();
            }

        }

    }

  • 相关阅读:
    第八周上机
    第七周作业
    第七周上机练习
    第六周作业
    第六次上机
    第五次上机
    第四周作业
    第四周上机练习
    第三次作业
    第二次作业
  • 原文地址:https://www.cnblogs.com/solo/p/610138.html
Copyright © 2011-2022 走看看