码迷,mamicode.com
首页 > 编程语言 > 详细

冒泡排序及折半查找

时间:2016-05-14 06:37:16      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:

冒泡排序:

            两层循环            

1.外层循环:循环趟数      n-1;            

2.内层循环:两两比较的次数      n-2;   

            int[] shuzu = new int[10]{1,9,5,6,2,3,4,7,8,10 };            

            for (int i = 0; i < shuzu.length; i++)            

            {                

                   for (int j = i; j < shuzu.length - 1; j++)                

                   {                    

                        if (shuzu[i] > shuzu[j + 1])                    

                       {                        

                             int zhong = shuzu[i];                        

                             shuzu[i] = shuzu[j + 1];                        

                             shuzu[j + 1] = zhong;                    

                        }

               

                  }            

              }            

//遍历数组            

foreach (int a in shuzu)            

{                

     console.writeline(a);            

}

折半查找:

            int[] a = new int[8] { 5,9,10,12,26,35,62,85};            

            int find = Convert.ToInt32(Console.ReadLine());

            int kaiShi=0, jieShu=a.Length-1, zhongJian;

            zhongJian = (kaiShi + jieShu) / 2;

冒泡排序及折半查找

标签:

原文地址:http://www.cnblogs.com/UC0079/p/5491755.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!