用伪代码(或任何其他伪代码)编写程序以提供优化的解决方案,以最大程度地减少执行时间和使用的资源? [英] Write a programe in pseudo code(or any) to provide an optimized solution that minimizes the excution time and the resources to be used?

查看:111
本文介绍了用伪代码(或任何其他伪代码)编写程序以提供优化的解决方案,以最大程度地减少执行时间和使用的资源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果给定一个球阵列,则该阵列包含三种颜色的球:红色,绿色和蓝色,这些球随机分布在阵列中,不进行排序.
每种颜色的球数都是未知的,并且不等于任何一种不同颜色的球数.

您需要对数组进行排序,以便将一种颜色的所有球都聚集在一起.
顺序排序无所谓.
例如,排序后,所有蓝色球都可以放在数组的开头,然后是所有红色球,最后是所有绿色球.

用伪代码(或其他任何一种)编写程序.您正在提供最小化优化的解决方案
执行时间和使用的资源.

If you are given an array of balls,the array contains ball in three colors:Red,green and blue,the balls are randomly distributed in the array and are not sorted.
The number of balls of each color is unknown and is not equal to the number of balls of any of the different colors.

you are asked to sort the array so as all the balls of one color are lumped together .
the order sorting is indifferent.
For Example,after sorting you could have all the blue balls at the beginning of the array and then followed by all the red balls and finally followed by all the green balls.

Write a programe in pseudo code(or any) .you are to provide an optimized solution that minimizes
the excution time and the resources to be used.

推荐答案

我们不做您的作业:它的设置是有原因的.在这里,您可以考虑自己被告知的内容,并尝试理解它.也可以在那里帮助您的导师识别您的弱点,并将更多的注意力放在补救措施上.

自己尝试,您可能会发现它并不像您想的那么困难!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!


我想:
蓝色= 1,绿色= 2,红色= 3
冒泡排序的伪代码:

procedure bubbleSort(A:可排序项目的列表)
重复
swapped =假
对于i = 1到length(A)-1(含1),请执行:
/*(如果这对故障)*/
如果A [i-1]> A [i]然后
/*交换它们,并记住更改的内容*/
swap(A [i-1],A [i])
swapped = true
如果
结束 结尾
直到不交换
结束过程
I suppose that:
Blue=1 , Green=2, Red=3
pseudo code by Bubble sort:

procedure bubbleSort( A : list of sortable items )
repeat
swapped = false
for i = 1 to length(A) - 1 inclusive do:
/* if this pair is out of order */
if A[i-1] > A[i] then
/* swap them and remember something changed */
swap( A[i-1], A[i] )
swapped = true
end if
end for
until not swapped
end procedure


这篇关于用伪代码(或任何其他伪代码)编写程序以提供优化的解决方案,以最大程度地减少执行时间和使用的资源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆