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
  • 相关阅读:
    前端规范标准(一)
    node之旅(3) Express之我的第一个应用
    node之旅(2) hello wrold!
    node之旅(1) 安装NodeJS
    CSS3 基础知识
    博客转移公告
    博客主题更换留念
    网络流-费用流zkw算法
    网络流-最大流ISAP
    字符串总结-三大“自动机”
  • 原文地址:https://www.cnblogs.com/tobin/p/1174687.html
Copyright © 2011-2022 走看看