问题描述
让我们认为我需要开发一个程序,该程序读取3个值,并仅使用if-else结构以升序打印这些值。
Let us consider that I need to develop a program that reads 3 values and that prints these values in ascending order, using only if-else structures.
注意我知道经典的排序算法。但是这里的重点是如何使用简单的条件结构为3个值开发排序算法。
Notice that I know the classical sorting algorithms. But the point here is how to develop a sorting algorithm for 3 values using simple conditional structures.
我实现了2个版本。我需要确定哪一个是最有效的,为什么。让我们考虑效率与程序花费的时间成反比。
I have implemented 2 versions. I need to identify which one is the most officient and why. Let us consider efficiency inversely proportional to the amount of time taken by the program.
我认为衡量此效率的一种方法是计算最小和最大数量。比较是必要的。即,评估最佳和最坏的情况。但是两种算法中ifs中条件的数量是不同的。
I think that one way to measure this would be to count the minimum and the maximum amount of comparisons that are necessary. That is, to evaluate the best and the worst cases. But the number of conditions in the ifs are different in the two algorithms.
让我们忽略printf花费的时间。
Let us ignore the time taken by printf.
版本1:
#include <stdio.h>
int main()
{
int v1,v2,v3;
printf("Provide 3 values:\n");
scanf("%d%d%d",&v1,&v2,&v3);
if ( v1 <= v2 && v1 <= v3){
if( v2 <= v3 ){
printf("%d, %d, %d\n", v1, v2, v3);
}
else{
printf("%d, %d, %d\n", v1, v3, v2);
}
}
else{
if(v2 <= v1 && v2 <= v3){
if(v1 <= v3){
printf("%d, %d, %d\n", v2, v1, v3);
}
else{
printf("%d, %d, %d\n", v2, v3, v1);
}
}
else{
if(v2 <= v1){
printf("%d, %d, %d\n", v3, v2, v1);
}
else{
printf("%d, %d, %d\n", v3, v1, v2);
}
}
}
return 0;
}
版本2
#include <stdio.h>
int main()
{
int v1,v2,v3;
printf("Provide 3 values:\n");
scanf("%d%d%d",&v1,&v2,&v3);
if ( v1 <= v2){
if( v1 <= v3 ){
if(v2 <= v3){
printf("%d, %d, %d\n", v1, v2, v3);
}
else{
printf("%d, %d, %d\n", v1, v3, v2);
}
}
else{
printf("%d, %d, %d\n", v3, v1, v2);
}
}
else{
if(v2 <= v3){
if(v1 <= v3){
printf("%d, %d, %d\n", v2, v1, v3);
}
else{
printf("%d, %d, %d\n", v2, v3, v1);
}
}
else{
printf("%d, %d, %d\n", v3, v2, v1);
}
}
return 0;
}
还有其他一些程序(仅使用if-else)吗?比这两个效率更高?
Is there some other program (that uses only if-else) that is more efficient than these two?
版本3
int main()
{
int v1,v2,v3;
printf("Provide 3 values:\n");
scanf("%d%d%d",&v1,&v2,&v3);
if(v1 <= v2){
if(v2 <= v3){
printf("%d, %d, %d\n", v1, v2, v3);
}else if(v1 <= v3 ){
printf("%d, %d, %d\n", v1, v3, v2);
}else{
printf("%d, %d, %d\n", v3, v1, v2);
}
}
else{
if(v1 <= v3){
printf("%d, %d, %d\n", v2, v1, v3);
}else if(v2 <= v3){
printf("%d, %d, %d\n", v2, v3, v1);
}else{
printf("%d, %d, %d\n", v3, v2, v1);
}
}
return 0;
}
推荐答案
判断效率很困难,因为它
Judging efficiency is difficult because it relies deeply on your CPU and your compiler's optimizer.
但是,从理论上讲,对3个元素进行排序至少需要3个比较。 具有用于更多元素的值。
However, from a theoretic perspective, sorting 3 elements requires at least 3 comparisons. Sequence A036604 has values for larger numbers of elements.
用于排序5个元素的代码显示在-您可能会看到为什么不经常使用这种排序方式。
Code for sorting 5 elements is shown here - you'll probably see why this kind of sorting isn't used often.
这篇关于使用if-else对3个值排序的最有效的c程序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!