/    Sign up×
Community /Pin to ProfileBookmark

Javascript sorting error…

Can’t seem to get this to work : I know there is an easier way to do this… but the error is bothering me.

[code]
function sort(nums) {

var rangeStart = 0;
var rangeEnd = nums.length – 1;
var i;
var minPosition = rangeStart;
while(rangeStart < rangeEnd) {

// find minumum
for(i = rangeStart; i < rangeEnd; i++) {
if(nums[i] <= nums[minPosition+1]) {
minPosition = i;
}
}

// swap
var temp = nums[rangeStart];
nums[rangeStart] = nums[minPosition];
nums[minPosition] = temp;

// change range
rangeStart++;
}
}

document.write(“<h3>Examples</h3>”);

first10 = [2,3,5,7,9,4,8,0,6,1];
document.write(“<div>Sorting <tt>[“+first10+”]</tt> with current code gives “);
sort(first10);
document.write(“<tt>[“+first10+”]</tt></div>”);

ages = [19,34,20,66,82,53,88,74,39,13];
document.write(“<div>Sorting <tt>[“+ages+”]</tt> with current code gives “);
sort(ages);
document.write(“<tt>[“+ages+”]</tt></div>”);

</script>
[/code]

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@KorNov 06.2009 — First: javascript has a [B]sort()[/B] native method for sorting the arrays.

[I]But[/I] the languages sorting methods are not decimal, as humans expect, but purely semantic, as they do not imply a bitwise comparison. See:
<i>
</i>&lt;script type="text/javascript"&gt;
array=[2,3,5,7,9,20,66,82,53,88,74];
sortedArray=array.sort();
alert (sortedArray);
&lt;/script&gt;

What to do? A bitwise comparison. Here's a "human" sorting approach:
<i>
</i>&lt;script type="text/javascript"&gt;
array=[2,3,5,7,9,20,66,82,53,88,74];
sortedArray = array.sort(function(array,sortedArray){return array==sortedArray?0:(array&lt;sortedArray?-1:1)});
alert (sortedArray);
&lt;/script&gt;
Copy linkTweet thisAlerts:
@JavaHTMLStudentNov 06.2009 — Thanks for the input Kor, I actually posted the same question here, I think the op and I are in the same class. We were tasked to figure out how to get the posted code to sort correctly by solving a problem by not re writing the code, but we have learned little in the realm of sorting so it is kind of a daunting task.
Copy linkTweet thisAlerts:
@KorNov 06.2009 — Thanks for the input Kor, I actually posted the same question here, I think the op and I are in the same class. We were tasked to figure out how to get the posted code to sort correctly by solving a problem by not re writing the code, but we have learned little in the realm of sorting so it is kind of a daunting task.[/QUOTE]
That's crazy:rolleyes: Do you mean your teacher invents silly codes to test your logic? ?

Are you sure that is your genuine code task?
Copy linkTweet thisAlerts:
@JavaHTMLStudentNov 06.2009 — That's crazy:rolleyes: Do you mean your teacher invents silly codes to test your logic? ?

Think about this:
<i>
</i>var temp = nums[rangeStart];
nums[rangeStart] = nums[minPosition];
nums[minPosition] = temp;

Simplify it like:
<i>
</i>a=b;
b=c;
c=a;

I don't sense what those statements supposes to do ? It's like a snake-eating-his-tail useless bunch of assignments. Are you sure that is your genuine code task?[/QUOTE]


Well we have a lecture and then lab, which this is based on and they always use code much different than our lecture. I know it is suppose to be an example of selection sorting and we are suppose to make it work somehow. And this is the only hint they were willing to give us, "There are two errors - a moderately obvious one and a subtle one - solvable by a very simple change each. Fixing the relatively easier one will allow the first example to be sorted properly. Fixing the second more subtle error would allow you to sort the second example."
Copy linkTweet thisAlerts:
@AshiKittyauthorNov 06.2009 — I admit I was too ashamed to admit that I needed help on homework... but now I see I'm not the only one!
Copy linkTweet thisAlerts:
@JavaHTMLStudentNov 06.2009 — I admit I was too ashamed to admit that I needed help on homework... but now I see I'm not the only one![/QUOTE]

I am too, this lab doesn't compare to anything else we've had.....hopefully someone here can help us....it would be very much appreciated!!
Copy linkTweet thisAlerts:
@KorNov 06.2009 —  And this is the only hint they were willing to give us, "There are two errors - a moderately obvious one [/QUOTE]
Ok... I'll help you with the (I guess) moderate obvious one:
<i>
</i>var rangeEnd = nums.length[COLOR="Red"] - 1[/COLOR];
...
...
for(i = rangeStart; i [COLOR="Red"]&lt;[/COLOR] rangeEnd; i++) {


When looping trough an array, the upper limit is the [B]array's length-1[/B], that is true, [I]but[/I] either you reach it (<=) or you set the upper limit straight as the [B]array's length[/B] and put the strict condition (<)
Copy linkTweet thisAlerts:
@AshiKittyauthorNov 06.2009 — http://codingforums.com/showthread.php?t=181542

Here is another person in our class that posted on a forum asking for help... I guess he was too ashamed to admit he needed help on a school assignment too.
Copy linkTweet thisAlerts:
@KorNov 06.2009 — That is not fair. From your teacher, I mean. It reminds me of the difference between "solving a problem" and "finding a solution". When they noticed that the astronauts ball pens do not work properly within the 0 gravity, the Americans spent quite a lot of money and time to invent a special ball pen for the situation. They thought they have "solved the problem"

Well, the Russians have used a simple coal pencil, from the beginning. ?. They "found a solution".

Do you study in US? ?
Copy linkTweet thisAlerts:
@AshiKittyauthorNov 06.2009 — Thank you for your help! Yeah, we are in the US.
Copy linkTweet thisAlerts:
@astupidnameNov 07.2009 — When they noticed that the astronauts ball pens do not work properly within the 0 gravity, the Americans spent quite a lot of money and time to invent a special ball pen for the situation. They thought they have "solved the problem"

Well, the Russians have used a simple coal pencil, from the beginning. . They "found a solution".
[/quote]

Well, just to be fair here, the anecdote at it's heart is valid, but you are promoting an old (false -total marketing ploy by the Fisher company) 'urban legend' of sorts. Perhaps you don't know the truth about that, see:

[URL]http://www.snopes.com/business/genius/spacepen.asp[/URL]

Also, interesting note from a soviet:

[URL]http://www.esa.int/esaCP/SEM9YN7O0MD_index_0.html[/URL]

Also, an interview with the inventor himself:

[URL]http://www.allwrite.nl/?incl=article-fisher-space-pen-history[/URL]

Fisher was somewhat fanatic, as all inventors are, and if we have the time and money to do something which has not been done before, or do it better (especially in this case, were also safety concerns), well maybe that is the american spirit... as well as Fisher's capitalizing off the notion of the 'space pen'. Seems like it was a good investment for him, big company, been around ever since...

Sorry for the somewhat off-topic note, but falsehoods are always questionable ?
Copy linkTweet thisAlerts:
@KorNov 07.2009 — Well, just to be fair here, the anecdote at it's heart is valid, but you are promoting an old (false -total marketing ploy by the Fisher company) 'urban legend' of sorts. Perhaps you don't know the truth about that, see:

[URL]http://www.snopes.com/business/genius/spacepen.asp[/URL]

Also, interesting note from a soviet:

[URL]http://www.esa.int/esaCP/SEM9YN7O0MD_index_0.html[/URL]

Also, an interview with the inventor himself:

[URL]http://www.allwrite.nl/?incl=article-fisher-space-pen-history[/URL]

Fisher was somewhat fanatic, as all inventors are, and if we have the time and money to do something which has not been done before, or do it better (especially in this case, were also safety concerns), well maybe that is the american spirit... as well as Fisher's capitalizing off the notion of the 'space pen'. Seems like it was a good investment for him, big company, been around ever since...

Sorry for the somewhat off-topic note, but falsehoods are always questionable ?[/QUOTE]


Sure. Everybody knows that urban anecdote is questionable. I was only to emphasize a principle, not the anecdote itself. ?

PS. I don't love Russians too much. We, Romanians, have serious historical reasons for mistrust them ?
×

Success!

Help @AshiKitty spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...