Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Other Programming > Other Programming Languages
|
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
  #1 (permalink)  
Old January 31st, 2006, 12:30 PM
Authorized User
 
Join Date: Feb 2005
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to ashokparchuri Send a message via Yahoo to ashokparchuri
Default 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
Reply With Quote
  #2 (permalink)  
Old June 24th, 2006, 09:25 AM
Authorized User
 
Join Date: Jun 2006
Posts: 73
Thanks: 1
Thanked 1 Time in 1 Post
Default

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.

Reply With Quote
  #3 (permalink)  
Old December 1st, 2006, 03:17 PM
Registered User
 
Join Date: Dec 2006
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Reply With Quote
  #4 (permalink)  
Old December 5th, 2006, 09:25 AM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Position of sorted nodes tall rog XSLT 5 July 9th, 2007 05:59 PM
Grouped and sorted table row numbers ADenny XSLT 1 April 24th, 2006 10:06 AM
fillin array with letters from a to Z and numbers sajid C# 10 May 3rd, 2005 03:38 PM
sorted table crmpicco Javascript How-To 0 March 17th, 2005 10:29 AM
Dataset does not get sorted correctly Imar ADO.NET 2 December 29th, 2003 04:49 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.