using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 排序法
{classProgram{staticvoidMain(string[] args){int[] arry ={5,2,9,4,6,3,7,8,1};for(int i =0; i < arry.Length-1; i++){for(int j =0; j < arry.Length-1-i; j++)//一定能把最大的数 放到最后一位{if(arry[j]> arry[j +1])//只要前一位大于后一位 就调换位置{int temp = arry[j];
arry[j]= arry[j +1];
arry[j +1]= temp;}}}foreach(int s in arry){
Console.WriteLine(s);}
Console.ReadLine();}}}
二分查找法
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace 二分法
{classProgram{staticvoidMain(string[] args){int[] arr ={1,2,3,4,5,6,7,8,9,10};//测试的数组intvalue=BinarySearCh(arr,8);//调用方法
Console.WriteLine(value);
Console.ReadLine();}publicstaticintBinarySearCh(int[] arr,intvalue)//传入数组和需要查找的值{int fir =0;//数组下标第一位int end = arr.Length -1;//数组下标最后一位while(fir <= end)//下标第一位不能大于最后一位{int num =(fir + end)/2;//最中间的索引值if(value== arr[num])//传入的值是否跟最中间的相等,相等就返回这个最中间索引值{return num;}elseif(value> arr[num])//传入的值大于中间的,相当于需要查找的值在比较大的那一半,所以下一次最小的索引从最中间的数加一开始再次循环{
fir = num +1;//}else//传入的值小于中间的,相当于需要查找的值在比较小的那一半,所以下一次最大的索引从最中间的数减一开始再次循环{
end = num -1;}}return-1;//如果没找到就返回-1}}}