cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Your Friends List is a way to easily have access to the community members that you interact with the most! X

Csort, is it true?

SergeyPichuzhki
1-Visitor

Csort, is it true?

Hello!

Sorry for my english

I just found this "problem"

I think that first "mas" was sorted by first column. But "Mathcad 14" doesn't think so... And we get "mas1"... But again "Mathcad" doesn't think that "mas1" is sorted... and so on...

mathcad.jpg

This little thing brought me a lot of problems...

Can you tell me, is it error of "Mathcad 14"? Or it works as it should? And what should I do?..

Thank!

Sergey

ACCEPTED SOLUTION

Accepted Solutions

This seems to be a bug.

Clipboard01.jpg

Mike

View solution in original post

25 REPLIES 25

This seems to be a bug.

Clipboard01.jpg

Mike

Thank, Mike! Happy new year!
I think that it is bug too... I try this function on Mathcad 14 M011, M035, Mathcad 15 - all works the same way...

But what should I do?...

It is not a bug. When you sort by column 0 there are multiple solutions that are equally valid. The starting point is one valid solution, and the ending point is another. This may be considered undesirable behavior, but it's not a bug.

Tom wrote a Csort function that allows you to sort by multiple columns (csort does not preserve the order of previous sorts), but that also does not attempt to preserve the order of columns that are not specified for the sorting. So I modified it (in my opinion, the new version is an improvement over Tom's version, but then I wrote the modification, so I am biased ).

Edit: 11:50 AM Dec 31. I just uploaded a new file with a minor modificaton to the new function.

And what about rsort?

rsort behaves exactly the same as csort, except on the rows rather than the columns.

It is not a bug. When you sort by column 0 there are multiple solutions that are equally valid. The starting point is one valid solution, and the ending point is another. This may be considered undesirable behavior, but it's not a bug.

Oh right, my mistake. Nice function by the way.

Maybe this should be added to the Prime request.

Mike

LucMeekes
23-Emerald III
(To:RichardJ)

A word of caution. The routine by Tom, as well as the improvement by Richard, fail if the matrix is large enough and you want to sort successively by enough columns. Demonstration:

LucMeekes_6-1610403117932.png

Phew, it does.

But When I increase lc to 4 (add an extra column to the matrix):

LucMeekes_7-1610403366444.png

The maximum difference is 8, not 0.

LucMeekes_8-1610403435413.png

Row 8130 should follow, not precede rows 8131 and 8132.

QED

Luc

 

LucMeekes
23-Emerald III
(To:RichardJ)

Of course there's a solution. If you are willing to take a little more processing time you can get:

LucMeekes_3-1610404359647.png

With 10 million matrix elements, no problem sorting on 10 columns.

The routine is recursive:

LucMeekes_2-1610404098032.png

Success!
Luc

 

Thanks to all! Happy new year!

Okey, this may be considered as undesirable behavior (but it brought me many problems...(((( But in my opinion, it is not correctly.... Because csort will be do transforming of the same matrix many times. Yes, there are multiple solutions. But I think that there is only one highest priority solution, when you meet values in sorted matrix in the same order as in unsorted matrix, don't you think so?
I hope that developers will change this function and fix this "undesirable behavior". (this is not normal logic, when the function receives the correct answer, and gives back another correct answer)
Hope to see the response of developers.

Sergey

Yes, there are multiple solutions. But I think that there is only one highest priority solution, when you meet values in sorted matrix in the same order as in unsorted matrix, don't you think so?

In an ideal world, yes. But that's not the way the algorithm used for csort works. The input vector could be checked to see if it is already sorted, but csort does not do that, presumably because of the extra overhead required. A small wrapper program could easily be written in Mathcad to do that though.

Hope to see the response of developers.

The developers never comment on these forums. As far as I know, they don't even read them.

It seems that in the program of c(r)sort function we have not more operator but more or equal operator.

I think so too (don't know enough algorithms, it seems like "bubble sort"...)

I think so too (don't know enough algorithms, it seems like "bubble sort"...)

Bubble sort is a terrible algorithm. The csort function uses the heapsort algorithm (it says so in the help). You can read about it in chapter 8 here:

http://www.nrbook.com/a/bookcpdf.php

Note that the help also says: "The heapsort procedure is not a stable sort, meaning that if you sort values with repeated elements, they appear in an arbitrary order.".

On another note - I was a little surprised to see that Mathcad can sort via text.

Mike

I expect a lot of people would have complained if it couldn't!

Has that always been the case with Mathcad?

I take it Mathcad treats the letters as numbers?

Mike

As far as I can recall it has always worked with strings. The sort is alphabetic, so "a10" comes before "a2".

I understand all about csort and Mathcad))) Thanks to all)))

In my opinion it will be good if developers will add alternate functions (alt_csort and alt_rsort). And those functions will work as I want! 😃 (yes, they will work slowly... but they will work!) Some tasks can't be solved with csort and rsort as they are today...

How do you think? Can we tell about this to developers? How can I do this? 😃

Sergey Pichuzhkin wrote:

Can we tell about this to developers? How can I do this? 😃

The usual way is to write your message using invisible ink on black paper and seal it in a lead bottle floating it down your local river so that they can pick it up on the sea shores off Cambridge Mass. after it has gone around the Atlantic a few times

Well I think that has been the most effective method so far <big wink>

Philip

Bearing in mind, of course, that nothing gets implemented until it has been suggested by at least ten people, ten times each, over a period of at least ten years, we should all get out our black paper and invisible ink and start writing!

big_grin.gif

The usual way is to write your message using invisible ink...

I know 7 examples when the invisible variable/function/operator name is useful.

Do you know more?

Example 1. Invisible addition

Mathcad allow us to change the multiplication sign. A user may select the one from the following:

2×а 2·а 2 х a 2 а 2а

The multiplication sign is invisible in last two examples that conform to the tradition existing in mathematics do not place a sing between efficients, if the first is a constant and the second is a variable.

Comment

For that reason, a variable name cannot start with digits.

However, blank space between the two values may mean as addition, as multiplication. For example, 2 hours 30 minutes, 1 kilometer 200 metes etc. Here the invisible addition sign stands between the same quantities (time and length), and multiplication sign — between the constants and the units. Fig. 1 shows how to solve it in Mathcad.

6-053-Invisible-Plus.PNG

Example 2. Zero dimension quantity

Sometimes Mathcad is too pedantic in dimensional quantities. For example, one says that some equipment is situated at a height of twenty meters and another at zero and not specifies the units of that zero (meters, centimeters, feet, or inches etc). Nevertheless, Mathcad always displays the units of the dimensional values even when it is not necessary. In that case, we can hide an excess unit converting it to invisible (Fig. 2).

6-056-Invisible-Unit.PNG

Example 3. The Roman arithmetic

Mathcad works with decimal, binary, hexadecimal, octal numbers. However, we may need to make Mathcad work with forms more exotic, for example, with Roman numbers. For that, we insert the function with the invisible name that returns a Roman number if its argument is an Arabic and conversely the Arabic number if the argument is Roman (Fig. 3).

6-057-Invisible-Function.PNG

Example 4. The sparse matrix

Mathcad has powerful instruments to work with vectors and matrixes (arrays). There is one limitation: these arrays should be completely filled. In practice we sometimes deal with nonrectangular matrixes, for example, with triangular. The matrixes may have more complicated form. Fig. 4 shows how to imitate working with sparse matrix.

6-058-Sparse-Matrix.PNG

Example 5. Invisible matker on the plot:

http://twt.mpei.ac.ru/ochkov/Mathcad_14/Chapter1/1_055_Ivisible_Marker.png

Example 6. Duoble answer with two units:

http://twt.mpei.ac.ru/ochkov/Mathcad_14/Chapter1/1_054_Inv_Var_for_Unit.png

Example 7. Endless loop:

http://twt.mpei.ac.ru/ochkov/Mathcad_14/Chapter1/1_056_Invis_While.png

Some tasks can't be solved with csort and rsort as they are today..

That is true. So unless you have a great deal of patience and are prepared to wait until the alternative functions are implemented (or the heat death of the universe occurs, which may be sooner), use the functions I posted

As far as I can recall it has always worked with strings. The sort is alphabetic, so "a10" comes before "a2".

Now that has no logic at all.

Mike

I expect a lot of people would have complained if it couldn't!

Complained!!!!!!!!

WE DON'T COMPLANE ON HERE DO WE ??????

Mike

Announcements

Top Tags