二分查找排序,二分排序


static final int N=15;
	static void quickSort(int[] arr,int left,int right)			//快速排序算法
	{
	    int f,t;
		int rtemp,ltemp;

	    ltemp=left;
	    rtemp=right;
	    f=arr[(left+right)/2];						//确定分界值
		while(ltemp<rtemp){
	        while(arr[ltemp]<f) ++ltemp;
	        while(arr[rtemp]>f) --rtemp;
	        if(ltemp<=rtemp){                      //冒泡置换
				t=arr[ltemp];
		        arr[ltemp]=arr[rtemp];
	    	    arr[rtemp]=t;
				--rtemp;
	        	++ltemp;
			}
	    }
		
	    if(ltemp==rtemp) ltemp++;
	    if(left<rtemp)  quickSort(arr,left,ltemp-1);				//递归调用
	    if(ltemp<right) quickSort(arr,rtemp+1,right);				//递归调用
	}
	
	static int searchFun(int a[],int n,int x){                     //折半查找
	    int mid,low,high;
		low=0;
		high=n-1;
	    while(low<=high){
	   		mid=(low+high)/2;
			if(a[mid]==x)
	            return mid;						//找到
			else if(a[mid]>x)
			    high=mid-1;
	        else
				low=mid+1;
	    }
		return -1;								//未找到
	}
	
	public static void main(String[] args) {
		int[] shuzu=new int[N];
		int x,n,i;

		for(i=0;i<N;i++){
			shuzu[i]=(int)(100+Math.random()*(100+1));;				//产生数组
		}
		
		System.out.print("折半查找算法演示!\n");
	    System.out.print("排序前数据序列:\n");
	    for(i=0;i<N;i++)
		{
	        System.out.print(" "+shuzu[i]);					//输出序列
		}
		System.out.print("\n\n");
		quickSort(shuzu,0,N-1);				//排序
	    System.out.print("排序后数据序列:\n");
	    for(i=0;i<N;i++)
		{
	        System.out.print(" "+shuzu[i]);					//输出序列
		}
		System.out.print("\n\n");
	    System.out.print("输入要查找的数:");
	    Scanner input=new Scanner(System.in);
	    x=input.nextInt();							//输入要查找的数

	    n=searchFun(shuzu,N,x);						//查找

	    if(n<0)								//输出查找结果
           System.out.println("没找到数据:"+x);
		else
		   System.out.println("数据:"+x+" 位于数组的第"+(n+1)+" 个元素处。");
	}


二分查找与快速排序

二分
package stack;

public class HalfSearch {
static int a[]={1,3,5,98,8,9,4,38,12};
public static int halfSeacrh(int[] a,int number){//二分查找
HalfSearch hs=new HalfSearch();
hs.bubbleSort(a);
int startPostion=0;
int endPostion=a.length-1;
int postion=(startPostion+endPostion)/2;
while(startPostion<endPostion){
if(a[postion]==number)
return postion;
else if(a[postion]<number){
startPostion=postion+1;
}
else if(a[postion]>number){
endPostion=postion-1;
}
postion=startPostion+endPostion;
}
return -1;

}
public static void bubbleSort(int a[]){
int temp=0;
for(int i=0;i<a.length;i++){
for(int j=i+1;j<a.length;j++){
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;

}
}
}

}

public static void main(String[] args) {

System.out.println(halfSeacrh(a,8));

}

}

快速:template<class elemtype>
void array<elemtype>::sort(int low,int high)
{
if(low>=high) return;
int lo=high+1;
elemtype elem=_ia[low];
for( ; ; ) {
while(_ia[++lo]<elem && lo<high);
while(_ia[--hi]>elem && hi>lo);
if(lo<hi) swap(lo,hi);
else break;
}
swap(low,hi);
sort(low,hi-1);
sort(hi+1,high);
}
 

一个C语言折半查找排序的程序,顺便再写一下怎验证?

#include "stdio.h"
#define N 15
int find(int b[N],int x)
{ int i,j,m;
i=0;j=N-1;
while(i<j)
{ m=(i+j)/2;
if(b[m]==x) return m;
else if(b[m]>x)
j=m-1;
else i=m+1;
}
return -1;
}

void main()
{ int a[N]={1,3,5,7,9,11,13,15,17,19,21,23,25,27,29},y,w;
printf("Please input the number you want to find:");
scanf("%d",&w);
y=find(a,w);
if(y==-1)
printf("No find!");
else
printf("Find susess! pos:%d\n",y);

}
 

相关内容

    暂无相关文章