C#数组二分查找法

看了一下的二分查找法!就用C#写了一下代码。

None.gifusing System;
None.gif
using System.Collections.Generic;
None.gif
using System.Text;
None.gif
None.gif
namespace ConsoleApplication1
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif    
class array
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public static void Main(string [] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Data data 
= new Data(10);
InBlock.gif            data.add(
1);
InBlock.gif            data.add(
32);
InBlock.gif            data.add(
12);
InBlock.gif            data.add(
45);
InBlock.gif            data.add(
24);
InBlock.gif            data.cancel(
12);
InBlock.gif            data.size();
InBlock.gif            data.display();
InBlock.gif
InBlock.gif           
// data.size();
ExpandedSubBlockEnd.gif
        }

ExpandedSubBlockEnd.gif    }

InBlock.gif
InBlock.gif    
class Data
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 用二分法查找数组
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        int number;//数组大小;
InBlock.gif
        int[] list;//数组;
InBlock.gif
        int lower;
InBlock.gif        
int upper;
InBlock.gif        
int current;
InBlock.gif
InBlock.gif        
public Data(int max)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            list 
=new int [max];
InBlock.gif            number 
= 0;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public void add(int o)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int i;
InBlock.gif            
for ( i = 0; i < number; i++)
InBlock.gif            
if ((int)list[i] > (int)o) break;
InBlock.gif            
for (int k = number; k>i ; k--)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif             list[k] 
= list[k-1];
ExpandedSubBlockEnd.gif            }

InBlock.gif            list[i] 
= o;
InBlock.gif            number
++;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public Boolean cancel(int o)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
int i = find(o);
InBlock.gif            
if (i.Equals("0")) return false;
InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
for (int k = i; k < number; k++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    list[k] 
= list[k + 1];
ExpandedSubBlockEnd.gif                }

InBlock.gif                number
--;
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public int find(int o)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            lower 
= 0;
InBlock.gif            upper 
= number-1;
InBlock.gif            
while (true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                current 
= (lower + upper) / 2;
InBlock.gif                
if (list[current] == o)
InBlock.gif                    
return current;
InBlock.gif                
else if (lower > upper)
InBlock.gif                    
return 0;
InBlock.gif                
else if (list[current] > (int)o)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    upper 
= current - 1;
InBlock.gif
ExpandedSubBlockEnd.gif                }

InBlock.gif                
else if (list[current] < (int)o)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    lower 
= current + 1;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//return ;
ExpandedSubBlockEnd.gif
        }

InBlock.gif        
public void size()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Console.WriteLine( number);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void display()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for (int i = 0; i < number; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"值:"+list[i]);
InBlock.gif                
ExpandedSubBlockEnd.gif            }

InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

InBlock.gif
ExpandedBlockEnd.gif}

None.gif

转载于:https://www.cnblogs.com/heyaowen163/articles/1206960.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值