• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
张继东
看铁蹄铮铮,踏遍万里河山。磨不破,打不烂。
   首页    新随笔    联系   管理    订阅  订阅

所谓的面向对象实现的冒泡排序

今天朋友问我用面向对象实现冒泡排序,说是面试用,写了一个,不知道是否能符合要求,同时希望这个小子能面试成功。
using System;

namespace BubbleSortTest
{
 /// <summary>
 /// BubbleSorter 的摘要说明。
 /// </summary>
 public class BubbleSorter
 {
  private int [] _myArray=null;
  public int [] MyArray
  {
   get
   {
    return _myArray;
   }
   set
   {
    _myArray=value;
   }
  }
  public BubbleSorter(int [] myArray)
  {
   MyArray=myArray;
  }
  /// <summary>
  /// 排序
  /// </summary>
  public void DoSort()
  {
   for(int i=0;i<_myArray.Length-1;i++)
   {
    int bFlog=0;
    for (int j=0;j<_myArray.Length-i-1;j++)
    {
     
     if(_myArray[j]<_myArray[j+1])
     {
      Swap(ref _myArray[j],ref _myArray[j+1]);
      bFlog=1;
     }

    }
    //如果一遍搜索后没有交换说明已经有序,结束排序。
    if(bFlog==0)
    {
     break;
    }
   }
  }
  /// <summary>
  /// 传引用交换
  /// </summary>
  /// <param name="i"></param>
  /// <param name="j"></param>
  public void Swap(ref int i,ref int j)
  {
   int temp=i;
   i=j;
   j=temp;
  }
 }
}

posted @ 2007-05-20 23:08  简单生活  阅读(711)  评论(4)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3