HINT法不太熟悉 我一般用双循环比较法
程序的话我可能需要写一会 等一下发
我的程序只要改一下输入就行了:(希望能帮到你)
#include
#define NUM 100
struct stuInfo
{
int mark;
}stu[NUM];
int BinarySearch(int a[],int x,int n);
void scoreSort(stuInfo stu[],int n);
int main(int argc, char* argv[])
{
int n,int a[100],int index;
printf("你准备输入数的个数:\n");
scanf("%d",&n);
printf("输入具体的数:\n");
for(int i = 0;i
scanf("%d",&stu[i].mark);
}
scoreSort(stu,n);
int j = 0;
i=0;
while(j
printf("%d ",stu[j].mark);
a[i]=stu[j].mark;
j++,i++;
}
printf("请输入另外一个数:\n");
int k=n;
scanf("%d",&stu[k].mark);
index=BinarySearch(a,stu[k].mark,n);
if(BinarySearch(a,stu[k].mark,n+1)!=-1){
printf("%d",index+1);
printf("\n");
}
else
printf("wucishu\n");
return 0;
}
void scoreSort(stuInfo *stu,int n)//n为学生数
{
for(int i= 0;i
for(int j =i;j
if(stu[i].mark
stuInfo temp;
temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
}
}
int BinarySearch(int a[],int x,int n)
{
int front=0,back=n-1,middle;
while(front<=back){
middle=(front+back)/2;
if(x>a[middle])
back=middle-1;
else if(x front=middle+1;
else
return (middle);
}
return -1;
}
不知道
这不是冒泡排序吗~~~~~~
/**
* file: sort.c
* description: sorting numbers with bubble sorting
* and find a given number by printing its position
* created with : Code::Blocks 10 .05
* Compiler : MinGW
* @Author: ZhouHaibing
*/
#include
/** ProtoType
* @Param: the pointer to the array , the length of the array
* return null
*/
void BubbleSort(int *nums,int length);
/** ProtoType
* @Param: the array , the element which is to be found, the length of the array
* return : the position of the number
*/
int FindNumber(int *nums,int num,int length);
int main()
{
/* declare a random array */
int nums[10] = {10,59,34,98,43,21,76,69,83,17};
/* the number to be found */
int SearchNum;
/* the position */
int Position;
/* sort the array */
BubbleSort(nums,10);
printf("Input the number you want to search:\n");
scanf("%d",&SearchNum);
Position = FindNumber(nums,SearchNum,10);
if(Position==-1)
{
printf("The number doesn't exist int the destination array.\n");
}
else
{
printf("The position is %d .\n",Position);
}
return 0;
}
/** Implementation
* @Param: the pointer to the array , the length of the array
* return null
*/
void BubbleSort(int *nums,int length)
{
int i,j;
for(i=0;i
for(j=i+1;j
if(nums[i]>nums[j])
{
int temp;
temp=nums[j];
nums[j]=nums[i];
nums[i]=temp;
}
}
}
}
/** Implementation
* @Param: the array , the element which is to be found , the length of the array
* return : the position of the number
*/
int FindNumber(int *nums,int num,int length)
{
/** Algorithm: BinarySearch
* startPosition=1 , endPosition=length
*/
int Start=1;
int End=length;
int Middle;
if(nums[0]>num||nums[length-1]
return -1;
}
Search :
{
Middle=(Start+End)/2;
if(nums[Middle-1]==num)
{
return Middle;
}
else if(Start==Middle||End==Middle)
{
return -1;
}
else if(nums[Middle-1]>num)
{
End=Middle;
goto Search;
}
else
{
Start=Middle;
goto Search;
}
}
}