473,387 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

sorting/searching arrays

3
does anyone know how to:

Write the code to determine and display the smallest and largest values contained in a 99 element floating point array named w
May 24 '07 #1
14 2067
RedSon
5,000 Expert 4TB
Yes, I could figure it out.
May 24 '07 #2
jma
3
can you post an example?
May 24 '07 #3
RedSon
5,000 Expert 4TB
can you post an example?
Is this a homework question?
May 24 '07 #4
jma
3
yes.. i need it asap
May 24 '07 #5
RedSon
5,000 Expert 4TB
yes.. i need it asap
I can't do your homework for you, that would be cheating. Why don't you tell me what you have done so far and maybe I can help guide you in the right direction.

EDIT: Changed thread name to better describe what the question was that was asked.
May 24 '07 #6
Silent1Mezzo
208 100+
Well, basically without giving away the answer (because it's a hw question) you would just go through each element of the array and check to see if its smaller than the minimum or larger than the maximum....Us telling you the answer won't help you learn it.
May 24 '07 #7
ahammad
79
There are many ways to do this. I think the hint that was posted above me is the simplest way to do it.
May 24 '07 #8
RedSon
5,000 Expert 4TB
There are many ways to do this. I think the hint that was posted above me is the simplest way to do it.
Another good hint would be to sort the array, then you know easily which is the lowest and which is the highest.

IMHO it is easier to write efficient sorting algorithms then searching algorithms.
May 24 '07 #9
ahammad
79
Another good hint would be to sort the array, then you know easily which is the lowest and which is the highest.

IMHO it is easier to write efficient sorting algorithms then searching algorithms.
That's what I was thinking. But if he's asking help for this then he's probably still a beginner. Even though there are some sorting algorithms all over the place, they may be hard to implement.

But yes, if it's a numerical array, make a habit of sorting it before you start manipulating. Most of the time it'll make your life much easier, but in this case, it's not extremely crucial.
May 24 '07 #10
RedSon
5,000 Expert 4TB
That's what I was thinking. But if he's asking help for this then he's probably still a beginner. Even though there are some sorting algorithms all over the place, they may be hard to implement.

But yes, if it's a numerical array, make a habit of sorting it before you start manipulating. Most of the time it'll make your life much easier, but in this case, it's not extremely crucial.
Or even better, sort while you insert then you are guaranteed O(n) sort algorithm.
May 24 '07 #11
Ganon11
3,652 Expert 2GB
Or even better, sort while you insert then you are guaranteed O(n) sort algorithm.
Insertion sort? Isn't that O(n**2)? There are n operations (one for each number being inserted), and for each insertion you will traverse approximately n / 2 of the numbers to find the correct insertion place, giving you n * (n / 2) = n**2 / 2 = O(n**2).

Not to mention the extra time necessary to push back the numbers into a new array when the insertion is in the middle.

Assuming the array is in random, unsorted order, the fastest way would be to traverse the array once looking for the highest and lowest values - this will be O(n) time. Any sorting will be greater than O(n).
May 24 '07 #12
Silent1Mezzo
208 100+
Insertion sort? Isn't that O(n**2)?
If O(n**2) means O(n^2) then yes...(not sure what **2 means). Insertion sort, bubble sort and selection sort are all O(n^2).

The best sorts would be Heap sort or Merge sort (both O(n log n))

Like Ganon11 said, the best way would be to loop through it once and just check it against a max / min value (this would produce O(n) which is the best you can get with this sort of algorithm )
May 25 '07 #13
RedSon
5,000 Expert 4TB
Insertion sort? Isn't that O(n**2)? There are n operations (one for each number being inserted), and for each insertion you will traverse approximately n / 2 of the numbers to find the correct insertion place, giving you n * (n / 2) = n**2 / 2 = O(n**2).

Not to mention the extra time necessary to push back the numbers into a new array when the insertion is in the middle.

Assuming the array is in random, unsorted order, the fastest way would be to traverse the array once looking for the highest and lowest values - this will be O(n) time. Any sorting will be greater than O(n).
Yes insertion sort is O(n**2). But sorting while you insert gives you an almost sorted array all the time, so at most you would need to traverse all the elements in it once to find the right spot to insert your new element.
May 25 '07 #14
nemisis
64
does anyone know how to:

Write the code to determine and display the smallest and largest values contained in a 99 element floating point array named w

Getting back to this, I would suggest sorting the array elements which was suggested by RedSon and ahammad earlier and is explained beautifully by Ganon11 here

After you have your elements sorted in this order
Index: 0 1 2 3 4
Array: 1 2 3 5 8

then the very first number (the 0th element of the array) is the smallest value and ofcourse the last number(the nth element, in your case 99th) is the largest value
May 27 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Paul Kirby | last post by:
Hello All I am trying to update me code to use arrays to store a group of information and I have come up with a problem sorting the multiple array :( Array trying to sort: (7 arrays put into...
2
by: Kakarot | last post by:
I'm gona be very honest here, I suck at programming, *especially* at C++. It's funny because I actually like the idea of programming ... normally what I like I'm atleast decent at. But C++ is a...
7
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) ...
7
by: Karin Jensen | last post by:
Hi I am running a PHP program that connects to an Access 2000 database via ODBC: $results = odbc_exec($connection_id, $sql_select); Is it possible to sort the contents of $results? I wish to...
23
by: yatindran | last post by:
hai this is my 2d array. int a = { {5,2,20,1,30,10}, {23,15,7,9,11,3}, {40,50,34,24,14,4}, {9,10,11,12,13,14}, {31,4,18,8,27,17}, {44,32,13,19,41,19}, {1,2,3,4,5,6},
10
by: Roy Gourgi | last post by:
Hi, How would I sort an array? TIA Roy
4
by: Kuku | last post by:
Hi, Can anyone please tell me good books/sites for sorting and searching. Finding it a little hard to understand
3
by: SneakyElf | last post by:
i am very green with c++ so i get stuck on very simple things anyway, i need to write a program that would read data from file (containing names of tv shows and their networks) one line at a time...
3
KevinADC
by: KevinADC | last post by:
If you are entirely unfamiliar with using Perl to sort data, read the "Sorting Data with Perl - Part One and Two" articles before reading this article. Beginning Perl coders may find this article...
5
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.