Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Improve my sort!

43 views
Skip to first unread message

jonas.t...@gmail.com

unread,
Nov 4, 2017, 8:50:47 AM11/4/17
to
<HTML>
<HEAD><TITLE>SORT</TITLE></HEAD>
<BODY>
<TABLE BGCOLOR="WHITE" BORDER=1><TH>GENERATE RANDOM NUMBERS</TH> <TR><TD>
<FORM NAME=random onSubmit="newrandom(); return false;">
Smallest number <input type="text" name="startrange" value="0"><br>
Biggest number <input type="text" name="endrange" value="1000"><br>
Values to generate <input type="text" name="xvalues" value="10000000"><P>
<input type="submit" value="GENERATE"><P>
VALUES TO SORT "PRINTOUT ONLY FOR ARRAY WITH LESS THEN 100000 ELEMENTS"<BR>
<TEXTAREA NAME=sort COLS=120 ROWS=8>
</TEXTAREA><BR>
</FORM>
</TD></TR></TABLE>
<TABLE BGCOLOR="WHITE" BORDER=!>
<TH>COMPARE SORT TIMINGS</TH>
<TR><TD>CHOOSE SORT ALGORITHM
<select id="mySelect" onchange="chosesort();">
<option value="index">JT_INDEXSORT</option>
<option value="javascript">javascript_sort</option>
<option value="quick">QUICKSORT</option>
</select>
<FORM NAME=sort onSubmit="chosesort(); return false;">
<input type=submit value="SORT"> TIME MS <input type="text" name="functime" value=""><br>
SORTED VALUES PRINTOUT "ONLY IF ARRAY HAS LESS THEN 100000 ELEMENTS":<BR>
<TEXTAREA TYPE=TEXT NAME=distrib COLS=120 ROWS=23>
</TEXTAREA><BR>
</FORM>
</TD></TR></TABLE>
</BODY>
<SCRIPT LANGUAGE="Javascript">
newrandom();
chosesort();
function newrandom(){
dval=new Array();
start=document.random.startrange.value;
end=document.random.endrange.value;
start=Number(start);
end=Number(end);
numberofvalues=document.random.xvalues.value;
//Generate random values
for (var i=0;i<numberofvalues;i++){
number=Math.floor(Math.random() * end) + start;
dval[i]=number;
}
if (dval.length<100000) {document.random.sort.value=dval;} else {document.random.sort.value=dval.length+" elements in array"};
}
function chosesort(){
selectedsort = document.getElementById("mySelect").value;
if (selectedsort=="index"){jtindexsort();}
else if (selectedsort=="javascript"){javascriptNativesort();}
else if (selectedsort=="bubble"){bubbleSort();}
else if (selectedsort=="quick"){ quickSort(dval,0,numberofvalues);}
if (dval.length<100000) {printout();} else {document.sort.distrib.value=dval.length+" sorted in array"};
}
function quickSort(arr, left, right)
{
var starttime = new Date().getTime();
var i = left;
var j = right;
var tmp;
pivotidx = (left + right) / 2;
var pivot = parseInt(arr[pivotidx.toFixed()]);
/* partition */
while (i <= j)
{
while (parseInt(arr[i]) < pivot)
i++;
while (parseInt(arr[j]) > pivot)
j--;
if (i <= j)
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
}
/* recursion */
if (left < j)
quickSort(arr, left, j);
if (i < right)
quickSort(arr, i, right);
var endtime = new Date().getTime();
var timed = endtime - starttime;
document.sort.functime.value=timed;
return arr;
}
function javascriptNativesort(){
var starttime = new Date().getTime();
dval = dval.sort(sortNumber);
var endtime = new Date().getTime();
var timed = endtime - starttime;
document.sort.functime.value=timed;
}
function sortNumber(a, b)
{
return a - b;
}
function jtindexsort(){
var starttime = new Date().getTime();
countval= new Array();
//Initiate array range
for (j=start;j<end;j++){
countval[j]=0;
}
var temp;
//Put them on correct index
for (var i=0;i<numberofvalues;i++){
temp=dval[i];
countval[temp]=countval[temp]+1;
}
p=0;
offset=0;
for (var i=start;i<end;i++){
dval.fill(i,offset,offset+countval[i]);
offset=offset+countval[i];
}
var endtime = new Date().getTime();
var timed = endtime - starttime;
document.sort.functime.value=timed;
}
function printout(){
valdist="";
for (var j=0;j<numberofvalues;j++){
valdist+=dval[j]+",";
}
document.sort.distrib.value=valdist;
}
</SCRIPT>
</HTML>

Michael Haufe (TNO)

unread,
Nov 4, 2017, 2:54:58 PM11/4/17
to
jonas.t...@gmail.com wrote:
> [...]

Fix all of the errors described at <http://jshint.com/>

jonas.t...@gmail.com

unread,
Nov 4, 2017, 2:57:29 PM11/4/17
to
As long the code is fast and have correct output i am happy. Fix the errors yourself if you like for me it works.

jonas.t...@gmail.com

unread,
Nov 4, 2017, 3:02:51 PM11/4/17
to
When i said improve my sort i meant the speed, not the syntax.
100 millions of values ranging from 0-1000 sorted in 18 seconds.
That is what i want to improve, using javascript on my computer.
I can't see any improvements now, can you?

Michael Haufe (TNO)

unread,
Nov 4, 2017, 3:40:25 PM11/4/17
to
jonas.t...@gmail.com wrote:
> Michael Haufe (TNO):
> > jonas.t...@gmail.com wrote:
> > > [...]
> >
> > Fix all of the errors described at <http://jshint.com/>
>
> As long the code is fast and have correct output i am happy. Fix the errors yourself if you like for me it works.

Thanks, but I think I'll avoid adding 'subjective sort' to my list of useful algorithms.

jonas.t...@gmail.com

unread,
Nov 4, 2017, 4:20:44 PM11/4/17
to
Subjective sort? i have no idea what you are on about?

Ben Bacarisse

unread,
Nov 4, 2017, 4:22:20 PM11/4/17
to
jonas.t...@gmail.com writes:
<snip>
> When i said improve my sort i meant the speed, not the syntax.

The algorithm is not yet correct. You have a bug (a different one) when
start is > 0.

> 100 millions of values ranging from 0-1000 sorted in 18 seconds.
> That is what i want to improve, using javascript on my computer.
> I can't see any improvements now, can you?

I can make it a few percent faster but that may well be system specific.

--
Ben.

jonas.t...@gmail.com

unread,
Nov 4, 2017, 4:26:26 PM11/4/17
to
Sure you can't have problem understand the algorithm

function jtindexsort(){
var starttime = new Date().getTime();
countval= new Array();
//Initiate array range
for (j=start;j<end;j++){
countval[j]=0;
}
var temp;

//Put them on correct index
for (var i=0;i<numberofvalues;i++){
temp=dval[i];
countval[temp]=countval[temp]+1;
}
p=0;



offset=0;
for (var i=start;i<end;i++){
dval.fill(i,offset,offset+countval[i]);
offset=offset+countval[i];
}

var endtime = new Date().getTime();
var timed = endtime - starttime;
document.sort.functime.value=timed;
}

If you think that ones to hard to analyse for your febble mind try the one below, it easy enough to teach a ten year old. Or to be created by a ten year old.

So tell me what is it you object to?

function jtindexsort(){
//Initiate array range
q=0;
countval.fill(q,start,end);
var temp;
//Put them on correct index
for (var i=0;i<numberofvalues;i++){
temp=dval[i];
countval[temp]=countval[temp]+1;
}

jonas.t...@gmail.com

unread,
Nov 4, 2017, 4:48:40 PM11/4/17
to
No Ben the algorithm works just fine unless you refer to the input max value being -1, it is the random generator bug as you said. Not the sorting algorithm. The result is the same using javascript sort or quicksort.

Michael Haufe (TNO)

unread,
Nov 4, 2017, 5:00:05 PM11/4/17
to
jonas.t...@gmail.com wrote:
Michael Haufe (TNO):
> > jonas.t...@gmail.com wrote:
> > > Michael Haufe (TNO):
> > > > jonas.t...@gmail.com wrote:
> > > > > [...]
> > > >
> > > > Fix all of the errors described at <http://jshint.com/>
> > >
> > > As long the code is fast and have correct output i am happy. Fix the errors yourself if you like for me it works.
> >
> > Thanks, but I think I'll avoid adding 'subjective sort' to my list of useful algorithms.
>
> Subjective sort? i have no idea what you are on about?

You haven't convinced me it's a valid sort (due to mentioned errors) let alone that it's faster than others since I won't trust the results nor implementation. If it has errors and you claim it works for you, that makes it subjective.

jonas.t...@gmail.com

unread,
Nov 4, 2017, 5:03:02 PM11/4/17
to
LoL i am sorry your a moron, the code so easy a 7 year old could understand it with some supervision.

jonas.t...@gmail.com

unread,
Nov 4, 2017, 5:05:32 PM11/4/17
to
Someone bring forward the buckets Michael Haufe needs help, anyone can help him out borrow him some buckets?

jonas.t...@gmail.com

unread,
Nov 4, 2017, 5:06:19 PM11/4/17
to

Michael Haufe (TNO)

unread,
Nov 4, 2017, 5:16:08 PM11/4/17
to
jonas.t...@gmail.com wrote:

> Sure you can't have problem understand the algorithm

I "have problem understand" the presentation. I don't even want to look at the algorithm in it's current form if it can't pass a basic linter and is presented in a quirks mode html page without formatting.

> function jtindexsort(){
> var starttime = new Date().getTime();
> countval= new Array();
> //Initiate array range
> for (j=start;j<end;j++){
> countval[j]=0;
> }
> var temp;
>
> //Put them on correct index
> for (var i=0;i<numberofvalues;i++){
> temp=dval[i];
> countval[temp]=countval[temp]+1;
> }
> p=0;
>
>
>
> offset=0;
> for (var i=start;i<end;i++){
> dval.fill(i,offset,offset+countval[i]);
> offset=offset+countval[i];
> }
>
> var endtime = new Date().getTime();
> var timed = endtime - starttime;
> document.sort.functime.value=timed;
> }
>
> If you think that ones to hard to analyse for your febble mind try the one below, it easy enough to teach a ten year old. Or to be created by a ten year old.

Gladly for my years of formal education in the relevant field I don't have to use my "febble" mind to criticize:

<http://www.typescriptlang.org/play/index.html#src=function jtindexsort() {%0D%0A var starttime %3D new Date().getTime()%3B%0D%0A countval %3D new Array()%3B%0D%0A %2F%2FInitiate array range%0D%0A for (j %3D start%3B j < end%3B j%2B%2B) {%0D%0A countval[j] %3D 0%3B%0D%0A }%0D%0A var temp%3B%0D%0A %2F%2FPut them on correct index%0D%0A for (var i %3D 0%3B i < numberofvalues%3B i%2B%2B) {%0D%0A temp %3D dval[i]%3B%0D%0A countval[temp] %3D countval[temp] %2B 1%3B%0D%0A }%0D%0A p %3D 0%3B%0D%0A offset %3D 0%3B%0D%0A for (var i %3D start%3B i < end%3B i%2B%2B) {%0D%0A dval.fill(i%2C offset%2C offset %2B countval[i])%3B%0D%0A offset %3D offset %2B countval[i]%3B%0D%0A }%0D%0A var endtime %3D new Date().getTime()%3B%0D%0A var timed %3D endtime - starttime%3B%0D%0A document.sort.functime.value %3D timed%3B%0D%0A}>

> So tell me what is it you object to?

Without even looking at the algorithm proper:

1- Free variables
2- mixing of DOM and the sorting algorithm
3- Not using the appropriate measurement API for this (performance.now)
4- combining performance measurement with the algorithm
5- redefining i
6- not using array literal notation

> function jtindexsort(){
> //Initiate array range
> q=0;
> countval.fill(q,start,end);
> var temp;
> //Put them on correct index
> for (var i=0;i<numberofvalues;i++){
> temp=dval[i];
> countval[temp]=countval[temp]+1;
> }
> offset=0;
> for (var i=start;i<end;i++){
> dval.fill(i,offset,offset+countval[i]);
> offset=offset+countval[i];
> }
> }

Again, no thought required on my part here:

<http://www.typescriptlang.org/play/index.html#src=function jtindexsort() {%0D%0A %2F%2FInitiate array range%0D%0A q %3D 0%3B%0D%0A countval.fill(q%2C start%2C end)%3B%0D%0A var temp%3B%0D%0A %2F%2FPut them on correct index%0D%0A for (var i %3D 0%3B i < numberofvalues%3B i%2B%2B) {%0D%0A temp %3D dval[i]%3B%0D%0A countval[temp] %3D countval[temp] %2B 1%3B%0D%0A }%0D%0A offset %3D 0%3B%0D%0A for (var i %3D start%3B i < end%3B i%2B%2B) {%0D%0A dval.fill(i%2C offset%2C offset %2B countval[i])%3B%0D%0A offset %3D offset %2B countval[i]%3B%0D%0A }%0D%0A}>

jonas.t...@gmail.com

unread,
Nov 4, 2017, 5:23:34 PM11/4/17
to
And still after all this time you come out as an idiot, well that is sad.

jonas.t...@gmail.com

unread,
Nov 4, 2017, 5:27:34 PM11/4/17
to
Please somone?
Can't you see the poor thing needs help, please someone give him some buckets, don't be cruel.

jonas.t...@gmail.com

unread,
Nov 4, 2017, 5:29:49 PM11/4/17
to

Michael Haufe (TNO)

unread,
Nov 4, 2017, 5:40:53 PM11/4/17
to
jonas.t...@gmail.com wrote:

> And still after all this time you come out as an idiot, well that is sad.

Triggered by evidence? What a shame. Obviously you don't want someone to help you "improve [your] sort" as the title says, instead you want a pat on the head and an "A" for effort as long as people don't look too deeply.

Fix the six issues I presented to you and I promise to take your solution more seriously than I do now.

jonas.t...@gmail.com

unread,
Nov 4, 2017, 5:48:31 PM11/4/17
to
In what way did you improve the sort?

jonas.t...@gmail.com

unread,
Nov 4, 2017, 5:52:05 PM11/4/17
to
Further i asked the poor inhabitants of comp.lang.javascript to point me to the best textsort and general datasort they can find. And i will "try" to beat it, but anal people are not interested, they wonder about the syntax.....

Michael Haufe (TNO)

unread,
Nov 4, 2017, 5:53:36 PM11/4/17
to
jonas.t...@gmail.com wrote:

> In what way did you improve the sort?

By deleting the copy on my computer

Ben Bacarisse

unread,
Nov 4, 2017, 6:08:51 PM11/4/17
to
jonas.t...@gmail.com writes:

> Den lördag 4 november 2017 kl. 21:22:20 UTC+1 skrev Ben Bacarisse:
>> jonas.t...@gmail.com writes:
>> <snip>
>> > When i said improve my sort i meant the speed, not the syntax.
>>
>> The algorithm is not yet correct. You have a bug (a different one) when
>> start is > 0.
<snip>
> No Ben the algorithm works just fine unless you refer to the input max
> value being -1, it is the random generator bug as you said. Not the
> sorting algorithm. The result is the same using javascript sort or
> quicksort.

Here's a screen short showing incorrect results:

http://bsb.me.uk/tmp/jt-sort.png

--
Ben.

jonas.t...@gmail.com

unread,
Nov 4, 2017, 6:08:51 PM11/4/17
to
But then you did not sort anything?
But then again you only copied other peoples crappy algorithms...
Like quicksort, mergesort etc
"To be honest they had their time, in a different era" today they are dinosaurs just like you.

Well when you copy or steal others work you remain an idiot for your whole life did you know?

Michael Haufe (TNO)

unread,
Nov 4, 2017, 6:11:14 PM11/4/17
to
Notation is a tool of thought [1], and it matters.

Secondly, if you did even a cursory glance at the subject you would have found candidates rather quickly[2]. You may even see the one you're close to reinventing.

[1] <http://www.jsoftware.com/papers/tot.htm>
[2] <https://en.wikipedia.org/wiki/Sorting_algorithm#Comparison_of_algorithms>

jonas.t...@gmail.com

unread,
Nov 4, 2017, 6:14:10 PM11/4/17
to
Den lördag 4 november 2017 kl. 23:08:51 UTC+1 skrev Ben Bacarisse:
> jonas.t...@gmail.com writes:
>
> > Den lördag 4 november 2017 kl. 21:22:20 UTC+1 skrev Ben Bacarisse:
> >> jonas.t...@gmail.com writes:
> >> <snip>
> >> > When i said improve my sort i meant the speed, not the syntax.
> >>
> >> The algorithm is not yet correct. You have a bug (a different one) when
> >> start is > 0.
> <snip>
> > No Ben the algorithm works just fine unless you refer to the input max
> > value being -1, it is the random generator bug as you said. Not the
> > sorting algorithm. The result is the same using javascript sort or
> > quicksort.
>
> Here's a screen short showing incorrect results:
>
> http://bsb.me.uk/tmp/jt-sort.png
>
> --
> Ben.

Well if you were intelligen you would understand it depends on the initialisation of arrays.

Option 1. Try run the rng separated from the sort, you think you could manage do that change?

Option 2. Press the button a second time you think your old hand could take it without it causing extreme pain?

Michael Haufe (TNO)

unread,
Nov 4, 2017, 6:16:40 PM11/4/17
to
Ben Bacarisse wrote:
>
> Here's a screen short showing incorrect results:
>
> http://bsb.me.uk/tmp/jt-sort.png
>

FAKE NEWS! ;)

Ben Bacarisse

unread,
Nov 4, 2017, 7:12:37 PM11/4/17
to
jonas.t...@gmail.com writes:

> Den lördag 4 november 2017 kl. 23:08:51 UTC+1 skrev Ben Bacarisse:
>> jonas.t...@gmail.com writes:
>>
>> > Den lördag 4 november 2017 kl. 21:22:20 UTC+1 skrev Ben Bacarisse:
>> >> jonas.t...@gmail.com writes:
>> >> <snip>
>> >> > When i said improve my sort i meant the speed, not the syntax.
>> >>
>> >> The algorithm is not yet correct. You have a bug (a different one) when
>> >> start is > 0.
>> <snip>
>> > No Ben the algorithm works just fine unless you refer to the input max
>> > value being -1, it is the random generator bug as you said. Not the
>> > sorting algorithm. The result is the same using javascript sort or
>> > quicksort.
>>
>> Here's a screen short showing incorrect results:
>>
>> http://bsb.me.uk/tmp/jt-sort.png
>
> Well if you were intelligen you would understand it depends on the
> initialisation of arrays.
>
> Option 1. Try run the rng separated from the sort, you think you could
> manage do that change?
>
> Option 2. Press the button a second time you think your old hand could
> take it without it causing extreme pain?

This display does not change. That's because there is a bug in the
code. I really don't mind if you don't believe me.

Option 3 is to fix the bug. That's what I did in the version I used to
compare times.

--
Ben.

jonas.t...@gmail.com

unread,
Nov 4, 2017, 8:14:47 PM11/4/17
to
Sorry Ben i am onto sorting strings now

Thomas 'PointedEars' Lahn

unread,
Nov 5, 2017, 2:21:21 PM11/5/17
to
Michael Haufe (TNO) wrote:

> jonas.t...@gmail.com wrote:
>> [...]
>
> Fix all of the errors described at <http://jshint.com/>

YHBT, HTH, HAND.

--
PointedEars
FAQ: <http://PointedEars.de/faq> | <http://PointedEars.de/es-matrix>
<https://github.com/PointedEars> | <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | Please do not cc me./Bitte keine Kopien per E-Mail.

Michael Haufe (TNO)

unread,
Nov 5, 2017, 7:14:14 PM11/5/17
to
Thomas 'PointedEars' Lahn wrote:
> Michael Haufe (TNO) wrote:
> > jonas.t...@gmail.com wrote:
> >> [...]
> >
> > Fix all of the errors described at <http://jshint.com/>

> YHBT, HTH, HAND.

What does "HAND" stand for?

Ben Bacarisse

unread,
Nov 5, 2017, 7:23:32 PM11/5/17
to
My guess would be (a slightly sarcastic) "have a nice day".

--
Ben.

Thomas 'PointedEars' Lahn

unread,
Nov 5, 2017, 8:54:09 PM11/5/17
to
Correct.
0 new messages