By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,513 Members | 1,693 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,513 IT Pros & Developers. It's quick & easy.

sorting/searching arrays

P: 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
Share this Question
Share on Google+
14 Replies


RedSon
Expert 5K+
P: 5,000
Yes, I could figure it out.
May 24 '07 #2

P: 3
jma
can you post an example?
May 24 '07 #3

RedSon
Expert 5K+
P: 5,000
can you post an example?
Is this a homework question?
May 24 '07 #4

P: 3
jma
yes.. i need it asap
May 24 '07 #5

RedSon
Expert 5K+
P: 5,000
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

100+
P: 208
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

P: 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
Expert 5K+
P: 5,000
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

P: 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
Expert 5K+
P: 5,000
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
Expert 2.5K+
P: 3,652
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

100+
P: 208
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
Expert 5K+
P: 5,000
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
P: 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

Post your reply

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