
求C语言高手相助,急啊。。
采用选择法对15个整数排序,排序完成后各整数按照从大到小的顺序排列。排序完成后输入一个数,要求用折半查找法找出该数是数组中第几个元素的值。Input输入共有两行:第一行输...
采用选择法对15个整数排序,排序完成后各整数按照从大到小的顺序排列。排序完成后输入一个数,要求用折半查找法找出该数是数组中第几个元素的值。Input输入共有两行: 第一行输入15个整数. 第二行输入你要查找的整数.Output输出你要查找的整数在排好序的数组中是第几个元素.如果该数不在数组中,则输出“wucishu!”。如果输入的数在数组中有多个,则输出此数在排好序后的数组中第一次出现的位置.具体请见Sample Output .
Sample Input
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
15
Sample Output
1
Hint选择法排序算法思想: 先在原数组n个元素中选择最小的一个元素,把它和位于第一个位置的元素互换位置;然后,在剩下的n-1个元素中选择最小的一个元素,并把它和n-1个数中第一个位置的元素交换;不断重复这些过程,直到最后两个元素。 如:设有10个元素a[0]~a[9], 第一轮:将a[0]与a[1]~a[9]比较,若a[0]比a[1]~a[9]都小,则不进行交换操作;若a[1]~a[9]中有一个以上比a[0]小,则将其中最小的一个(假设为a[i])与a[0]交换,此时a[0]中存放了10个中的最小数。 第二轮将a[1]与a[2]~a[9]比较,将剩下的8个数中最小者a[i]与a[1]对换,此时a[1]中存放的是10个中第二小的数。 依此类推,共进行9轮比较,a[0]~a[9]中已按由大到小的顺序存放。 2、 排序完成后,这15个成绩按由大到小的顺序排列,此时用折半查找法查找某一个成绩效率较高。 折半查找法算法思想:已有按由小到大排好序的9个数,a[0]~a[8],其值分别为:1 3 5 7 9 11 13 15 17 若想查3是否在此数组中,可以先找出表列中居中的数,即a[4],将要找的数3与a[4]比较,a[4]值是9,发现a[4]>3,显然3应当在a[0]~a[4]范围内,而不会在a[5]~a[8]范围内。这样就可以缩小查找范围,甩掉a[5]~a[8]这一部分,即将查找范围缩小为一半。再找a[0]~a[4] 中居中的数,即a[2],将要找的数与a[2] 比较,a[2]的值是5,发现a[2]>3,显然3应当在a[0]~a[2]范围内。这样又将查找范围缩小了一半。再将3与a[0]~a[2]范围内居中的数a[1]比较,发现要找的数3等于a[1],查找结束。一共比较了3次。
帮我写个程序嘛。。 展开
Sample Input
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
15
Sample Output
1
Hint选择法排序算法思想: 先在原数组n个元素中选择最小的一个元素,把它和位于第一个位置的元素互换位置;然后,在剩下的n-1个元素中选择最小的一个元素,并把它和n-1个数中第一个位置的元素交换;不断重复这些过程,直到最后两个元素。 如:设有10个元素a[0]~a[9], 第一轮:将a[0]与a[1]~a[9]比较,若a[0]比a[1]~a[9]都小,则不进行交换操作;若a[1]~a[9]中有一个以上比a[0]小,则将其中最小的一个(假设为a[i])与a[0]交换,此时a[0]中存放了10个中的最小数。 第二轮将a[1]与a[2]~a[9]比较,将剩下的8个数中最小者a[i]与a[1]对换,此时a[1]中存放的是10个中第二小的数。 依此类推,共进行9轮比较,a[0]~a[9]中已按由大到小的顺序存放。 2、 排序完成后,这15个成绩按由大到小的顺序排列,此时用折半查找法查找某一个成绩效率较高。 折半查找法算法思想:已有按由小到大排好序的9个数,a[0]~a[8],其值分别为:1 3 5 7 9 11 13 15 17 若想查3是否在此数组中,可以先找出表列中居中的数,即a[4],将要找的数3与a[4]比较,a[4]值是9,发现a[4]>3,显然3应当在a[0]~a[4]范围内,而不会在a[5]~a[8]范围内。这样就可以缩小查找范围,甩掉a[5]~a[8]这一部分,即将查找范围缩小为一半。再找a[0]~a[4] 中居中的数,即a[2],将要找的数与a[2] 比较,a[2]的值是5,发现a[2]>3,显然3应当在a[0]~a[2]范围内。这样又将查找范围缩小了一半。再将3与a[0]~a[2]范围内居中的数a[1]比较,发现要找的数3等于a[1],查找结束。一共比较了3次。
帮我写个程序嘛。。 展开
展开全部
HINT法不太熟悉 我一般用双循环比较法
程序的话我可能需要写一会 等一下发
程序的话我可能需要写一会 等一下发
更多追问追答
追问
嗯,等待ing,,,
追答
楼主你好刚才有些事没看到追问目前正在编
展开全部
我的程序只要改一下输入就行了:(希望能帮到你)
#include<stdio.h>
#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<n;i++)
{
scanf("%d",&stu[i].mark);
}
scoreSort(stu,n);
int j = 0;
i=0;
while(j<n&&i<n)
{
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<n;i++)
{
for(int j =i;j<n;j++)
{
if(stu[i].mark<stu[j].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<a[middle])
front=middle+1;
else
return (middle);
}
return -1;
}
#include<stdio.h>
#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<n;i++)
{
scanf("%d",&stu[i].mark);
}
scoreSort(stu,n);
int j = 0;
i=0;
while(j<n&&i<n)
{
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<n;i++)
{
for(int j =i;j<n;j++)
{
if(stu[i].mark<stu[j].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<a[middle])
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<stdio.h>
/** 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<length-1;i++)
{
for(j=i+1;j<length;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]<num)
{
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;
}
}
}
/**
* 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<stdio.h>
/** 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<length-1;i++)
{
for(j=i+1;j<length;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]<num)
{
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;
}
}
}
更多追问追答
追问
真心不懂。。。
追答
我要不把注释去掉或者换成中文~~~~
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询