/**
**/
package com.test.sort;
/**
* @author Administrator
*
*/
/**
*冒泡排序
*
*/
public class Bubbling
{
public static int[] Bubbling(int[] arr)
{
//int[] arr = {12,45,11,22,2,5,27,3,99,0};
for(int i = 0;i<arr.length;i++)
{
for(int j=0;j<arr.length-i-1;j++)
{
int temp;
if(arr[j]>arr[j+1])
{
temp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
}
--------------------------------------------------------------分隔符----------------------------------------------------------
二分查找以中间的数字大小儿子比较标准,假设是从小到大,中间数字大于所求数字,那肯定在左边,相同的方法定位左边字表的中间值在比较,相反则亦然
/**
*
*/
package com.test.sort;
/**
* @author Administrator
*二分查找
*/
public class BinarySearch
{
public static int binarySearch(int[] array, int value)
{
int low = 0;
int high = array.length-1;
int middle = 0;
while(low <= high)
{
middle = (low+high)/2;
if(array[middle] == value)
{
return middle;
}
if(value < array[middle])
{
high = middle - 1;
}
if(value > array[middle])
{
low = middle + 1;
}
}
return -1;
}
//递归
public static int binarySearch1(int[] array, int value,int low,int right)
{
int center = (low+right)/2;
if(array[center] == value)
{
System.out.println("找到了");
return center;
}
if(array[center] > value)
{
return binarySearch1(array,value,low,right -1);
}
if(array[center] < value)
{
return binarySearch1(array,value,center+1,right);
}
return -1;
}
public static void main(String[] args)
{
int[] array = {7,3,9,5,6,8,1};
int[] array1 = Bubbling.Bubbling(array);
//1356789
int index = binarySearch(array1,5);
System.out.println("所在的位置:"+index);
}
}