zoukankan      html  css  js  c++  java
  • c#冒泡排序正解!

    今天,一个朋友问我冒泡排序的例子,因为正忙着,就告诉他网上有很多的例子,让他自己搜下,后来他又发消息给我,这可把我恨的啊。
    这不是误人前程么
    下面我们来看看网上的冒泡排序例子:
     1public void Sort(int[] list)
     2   {
     3      while(j<list.Length)
     4      {
     5       for(i=0;i<list.Length-1;i++)
     6       {
     7         if(list[i]>list[i+1])
     8        {
     9          temp=list[i];
    10         list[i]=list[i+1];
    11          list[i+1]=temp;
    12         }

    13      }

    14     j++;
    15    }

    16
    这个例子中,我想问,这个j干啥用了?我上网看了下,和这个一摸一样的例子还特别多。
    真是害人不浅啊 !
    无奈,只能动手写个了。
    下面是正解。简单的例子:
    protected int[] bubbleUp(int[] Array)
        
    {
            
    for (int i = 0; i < Array.Length; i++)
            
    {
                
    for (int j = i+1; j < Array.Length; j++)
                
    {
                    
    if (Array[i] > Array[j])
                    
    {
                        
    int temp=Array[i];
                        Array[i]
    =Array[j];
                        Array[j] 
    = temp;
                    }

                }

            }

            
    return Array;
        }
    希望对不了解冒泡排序的朋友有所帮助!
    详细参考:http://www.cnblogs.com/emanlee/archive/2008/04/28/1174071.html
  • 相关阅读:
    php configure –help说明
    HTML5(目前)无法帮你实现的五件事多媒体
    Centos搭建PHP5.3.8+Nginx1.0.9+Mysql5.5.17
    lighttpdmod_secdownload 防盗链
    中文环境下PostgreSQL的使用
    一步一步教你安装Nginx+PHP+Mysql
    20+ 个免费和高级的 Web 视频播放器
    二十个你必须知道的SEO概念
    IO流
    sofaBoot
  • 原文地址:https://www.cnblogs.com/tobin/p/1174687.html
Copyright © 2011-2022 走看看