C#实现冒泡排序

感谢 morewindows,这里用C# 实现3种冒泡排序方法。

运算复杂度,很显然是O(n^2)

***********************转载******************************

设数组长度为N。

1.比较相邻的前后二个数据,如果前面数据大于后面的数据,就将二个数据交换。

2.这样对数组的第0个数据到N-1个数据进行一次遍历后,最大的一个数据就“沉”到数组第N-1个位置。

3.N=N-1,如果N不为0就重复前面二步,否则排序完成。

***********************转载******************************


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

namespace 排序
{
    /// <summary>
    /// 冒泡排序基本解法,未优化( 这好像不是冒泡排序?那这个应该是什么啊,我也不懂。。。)
    /// </summary>
    class BubbleSort1
    { 
        public void BSmain1(int[] a,int n)
        {
            print(a, n);
            BSort1(a, n);
            print(a, n);
            
        }

        //C# 交换两个数的值, 没有指针,注意要使用ref 关键字,否则改变传递不出去
        private void swap(ref int a,ref int b)
        {
            int t;
            t = a; a = b; b = t;
        }
        private void BSort1 (int[]a,int n)
        {
            for(int i=0;i<n;i++)
            {
                for(int j=i;j<n;j++)
                {
                    if (a[i] > a[j])
                        swap(ref a[i],ref a[j]);
                }
            }
        }
        private void print(int[] a, int n)
        {
            for (int i = 0; i < n; i++)
            {
                Console.Write("{0}  ", a[i]);
            }
            Console.WriteLine("\t");
        }
       
    }
}


另外主程序中需要注意:静态方法不能直接调用非静态方法, 静态方法只能用来调用静态函数和变量的

***********************转载******************************

http://www.cnblogs.com/tobyforever/archive/2009/05/08/1452331.html

static方法调用non-static方法必须通过传对象参数的方式,因为non-static方法是与对象实例对应的。

我们都知道,静态static方法中不能调用非静态non-static方法,准确地说是不能直接调用non-static方法。但是可以通过将一个对象的引用传入static方法中,再去调用该对象的non-static方法。 其实这个事实的应用很经常,以至于我们不去重视:在主函数(static方法)中我们经常创建某个类的实例,再利用其饮用变量调用它的非静态方法。

*********************转载********************************

//StaticMethodTest.java
 //A ststic method cannot call a non-static method, but we can transfer a object reference, which include a non-static metho to the static method, thus, wo can call that non-static method in a indirect way.


 public class StaticMethodTest{
     void NonStaticMethod(){
         System.out.println("This is a non-sataic method.");

    }
     
    static void StaticMethod(StaticMethodTest s){
        System.out.println("This is a static method.");
        s.NonStaticMethod();
     }


    public static void main(String[] args) {

         StaticMethodTest sObj=new StaticMethodTest();
         StaticMethod(sObj); //在主函数中可以直接调用静态方法
    }
 }


class Program
    {
        static void Main(string[] args)
        {
            int n = 10;
            int[] a = new int[n];
            Random rm = new Random();

            rm.Next(10);
            for (int i = 0; i < n; )
            {
                int j = rm.Next(20);
                if (!a.Contains(j))
                {
                    a[i] = j;
                    i++;
                }
            }
            
            BubbleSort1 bs1 = new BubbleSort1();
            bs1.BSmain1(a,a.Length);
            //这里是不对的,静态方法不能“直接”调用非静态方法,只能调用静态方法和静态变量
            //print(a, a.Length);
            Console.ReadKey();
        }
        private void print(int[] a, int n)
        {
            for (int i = 0; i <n; i++)
            {
                Console.Write("{0}  ", a[i]);
            }
            Console.WriteLine("\t");
        }
    }

这貌似才是冒泡排序?

    /// <summary>
    /// 冒泡排序法2
    /// </summary>
    
        private void BSort2(int[] a, int n)
        {
            for (int i = 0; i < n; i++)
            {
                for (int j = 1; j < n-i; j++)
                {
                    if (a[j-1] > a[j])
                        swap(ref a[j-1], ref a[j]);
                }
            }
        }

***********************转载******************************

优化算法:设置一个标志,如果这一趟发生了交换,则为true,否则为false。明显如果有一趟没有发生交换,说明排序已经完成。

***********************转载******************************

 private void BSort2(int[] a, int n)
        {
           // bool tag = true;
            for (int i = 0; i < n; i++)
            {
                bool tag = true;
                for (int j = 1; j < n-i; j++)
                {
                    if (a[j - 1] > a[j])
                    {
                        swap(ref a[j - 1], ref a[j]);
                        tag = false;
                    }
                }
                if (tag) break;
            }
        }





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值