一个o(n)的排序算法,类位图排序

基本思想:

定义一个带排序数中的最大数为DataForStore数组长度,一遍扫描带排序数组,将其值作为DataForStore数组中对应下标的数加1,随后在DataForStore数组中即是一排序号的数,顺序输出即可。

当然该算法有条件限制,如排序数中的最大数不能太大,至于DataForStore数组可以采用位存储方案,这里为了便于实验,即忽略空间要素。。。。。。

只是扫描一遍数组即完成排序,真正的O(N)复杂度

 

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

namespace  ConsoleApplication2
{
    
class  Program
    {
        
static   void  Main( string [] args)
        {
            BitMap bm 
=   new  BitMap( 1000000 10000 );
            bm.CreateRandomData();
// 产生随机数
            bm.Sort(); // 排序
            bm.PrintDataAfterSort(); // 输出
            Console.ReadLine();
        }
    }
    
class  BitMap
    {
        
public   int  DateLenth;
        
public   int  MaxNumber;
        
public   int [] DataForStore;
        
public   int [] DataForSort;
        
///   <summary>
        
///  
        
///   </summary>
        
///   <param name="datelenth"> 带排序个数 </param>
        
///   <param name="maxnumber"> 带排序最大数 </param>
         public  BitMap( int  datelenth,  int  maxnumber) 
        {
            DateLenth 
=  datelenth;
            MaxNumber 
=  maxnumber;
            DataForStore 
=   new   int [maxnumber];
        }
        
///   <summary>
        
///  产生随机数,便于测试
        
///   </summary>
         public   void   CreateRandomData()
        {
            Random r 
=   new  Random();
            DataForSort 
=   new   int [DateLenth];             
            
for  ( int  i  =   0 ; i  <  DateLenth; i ++ )
            {
                DataForSort[i] 
=  r.Next(MaxNumber); 
            }
        }
        
///   <summary>
        
///  排序
        
///   </summary>
         public   void  Sort()
        {
            
for  ( int  i  =   0 ; i  <  DateLenth; i ++ )
            {
                DataForStore[DataForSort[i]]
++
            }
        }
        
///   <summary>
        
///  输出排序后的数据
        
///   </summary>
         public   void  PrintDataAfterSort()
        {
            
for  ( int  i  =   0 ; i  <  MaxNumber; i ++ )
            {
                
for  ( int  j  =   0 ; j  <  DataForStore[i]; j ++ )
                {
                    Console.Write(i
+ " , " );
                }
            }
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值