 |
| Other Programming Languages If you have a coding issue to discuss about another language that really isn't provided for in any other forum here (not ASP.NET C#, C++, VB, PHP, JavaScript, Python, Java, Perl, Applescript, XML or any of the other forum topics we have), post it here. Enough discussion on a language we don't have covered could prompt a new forum. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Other Programming Languages section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

January 31st, 2006, 12:30 PM
|
|
Authorized User
|
|
Join Date: Feb 2005
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
How to get least value numbers in a sorted array?
Hi,
I had a doubt that if we have an sorted array with some numbers like 45,54,65,34,34,23,78. How can we get the least umbers in this arrray?
Please reply me as soon as possible.
Thanks-Ashok
|

June 24th, 2006, 09:25 AM
|
|
Authorized User
|
|
Join Date: Jun 2006
Posts: 73
Thanks: 1
Thanked 1 Time in 1 Post
|
|
Ashok, it will depend on your language very much, but in principle, you would code for a 'bubble sort', which means you look at each elememt in the array in turn and compare it to the other elements. Storing the lower number in a variable on each comparison, until eventually the smallest number is stored in the variable. Post again with more detail, I write in Visual Basic.liamfitz.
|

December 1st, 2006, 03:17 PM
|
|
Registered User
|
|
Join Date: Dec 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
in javascript/perl/php slight difference in declaration of variables but i will do it in js. Per suggested previously.
var lowest_num=0;
for(var i=0;i<array_name.length;i++){
if(array_name.length != i){ // no need to look at last
var temp_holder = array_name[i];
var temp_holder2 = arrau_name[i+1] // look one ahead
if(temp_holder < temp_holder2){
lowest_num=temp_holder;
}
else{
lowest_num=temp_holder2;
}
}
}
should be fairly close, if not working
now if you wanna build a numerically sorted array, i suggest you look at "push / pop" method
|

December 5th, 2006, 09:25 AM
|
|
Friend of Wrox
|
|
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
Most languages have built in sorting, which works well in the majority of cases - you should use these. If keeping the information in a sorted order is important you may want to consider using a data structure other than an array.
Anyhow, here's a couple of snippets that find the lowest number in an array.
Here's a perl version:
Code:
my @unsorted = (34, 69, 12, 67, 21, 9, 78, 478327);
my @sorted = sort {$a <=> $b} @unsorted;
print "$sorted[0]\n";
Here's a ruby version:
Code:
arr=[34, 69, 12, 67, 21, 9, 78, 478327]
p arr.sort.first
Cheers
--
Don't Stand on your head - you'll get footprints in your hair
http://charlieharvey.org.uk
http://charlieharvey.com
|
|
 |