1. RE: stable sorting
> From: Juergen Luethje [mailto:eu.lue at gmx.de]
> The following code (modified after 'csort.ex') does *not* perform a
> stable sorting.
>
> ----------=----------=----------=----------=----- begin of code
> include sort.e
>
> constant MARK = 2
> constant statistics = {
> {"Doe", 3},
> {"Einstein", 1},
> {"Goldberg", 1},
> {"Irving", 4},
> {"Jones", 3},
> {"Miller", 2},
> {"Neuman", 2},
> {"Petersen", 4},
> {"Smith", 2},
> {"Zander", 5}
> }
>
> function compare_mark (sequence a, sequence b)
> -- Compare two sequences (records) according to MARK.
> return compare(a[MARK], b[MARK])
> end function
try:
function compare_mark (sequence a, sequence b)
integer c
-- Compare two sequences (records) according to MARK.
c = compare(a[MARK], b[MARK])
if c then
return c
end if
return compare(a[1],b[1])
end function
Matt Lewis
2. RE: stable sorting
Matthew Lewis wrote:
>
> > From: Juergen Luethje [mailto:eu.lue at gmx.de]
>
> > The following code (modified after 'csort.ex') does *not* perform a
> > stable sorting.
> >
> > ----------=----------=----------=----------=----- begin of code
> > include sort.e
> >
> > constant MARK = 2
> > constant statistics = {
> > {"Doe", 3},
> > {"Einstein", 1},
> > {"Goldberg", 1},
> > {"Irving", 4},
> > {"Jones", 3},
> > {"Miller", 2},
> > {"Neuman", 2},
> > {"Petersen", 4},
> > {"Smith", 2},
> > {"Zander", 5}
> > }
> >
> > function compare_mark (sequence a, sequence b)
> > -- Compare two sequences (records) according to MARK.
> > return compare(a[MARK], b[MARK])
> > end function
>
> try:
>
> function compare_mark (sequence a, sequence b)
> integer c
> -- Compare two sequences (records) according to MARK.
> c = compare(a[MARK], b[MARK])
> if c then
> return c
> end if
> return compare(a[1],b[1])
> end function
>
>
Here's what I do:
----------------------
include sort.e
-- Don't call this function directly, see below
integer sort_i
function sort_by_index(object x1, object x2)
integer comp, ix
if integer(sort_i) then
if sort_i > 0 then
return compare(x1[sort_i],x2[sort_i])
else
return -compare(x1[-sort_i],x2[-sort_i])
end if
end if
for j = 1 to length(sort_i)-1 do
ix = sort_i[j]
if ix > 0 then
comp = compare(x1[ix],x2[ix])
else
comp = -compare(x1[-ix],x2[-ix])
end if
if comp != 0 then
return comp
end if
end for
ix = sort_i[length(sort_i)]
if ix > 0 then
return compare(x1[ix],x2[ix])
else
return -compare(x1[-ix],x2[-ix])
end if
end function
-- This is the function you actually call.
-- s = sequence to be sorted (assumed to be at least 2-dimensional)
-- i = integer index of element (column) to sort on,
-- or sequence of indexes to sort on multiple fields
-- (in order of preference).
-- If any given index is positive, then it will sort that element in
ascending order.
-- If negative, it will sort that element in descending order.
global function sort_by_element(sequence s,object i)
sort_i = i
return custom_sort(routine_id("sort_by_index"),s)
end function
3. RE: stable sorting
Andy Serpa wrote:
>
> Matthew Lewis wrote:
> >
> > > From: Juergen Luethje [mailto:eu.lue at gmx.de]
> >
> > > The following code (modified after 'csort.ex') does *not* perform a
> > > stable sorting.
> > >
> > > ----------=----------=----------=----------=----- begin of code
> > > include sort.e
> > >
> > > constant MARK = 2
> > > constant statistics = {
> > > {"Doe", 3},
> > > {"Einstein", 1},
> > > {"Goldberg", 1},
> > > {"Irving", 4},
> > > {"Jones", 3},
> > > {"Miller", 2},
> > > {"Neuman", 2},
> > > {"Petersen", 4},
> > > {"Smith", 2},
> > > {"Zander", 5}
> > > }
> > >
> > > function compare_mark (sequence a, sequence b)
> > > -- Compare two sequences (records) according to MARK.
> > > return compare(a[MARK], b[MARK])
> > > end function
> >
> > try:
> >
> > function compare_mark (sequence a, sequence b)
> > integer c
> > -- Compare two sequences (records) according to MARK.
> > c = compare(a[MARK], b[MARK])
> > if c then
> > return c
> > end if
> > return compare(a[1],b[1])
> > end function
> >
> >
> Here's what I do:
>
> ----------------------
> include sort.e
>
> -- Don't call this function directly, see below
> integer sort_i
Messed up. That should be "object sort_i"
4. RE: stable sorting
- Posted by rforno at tutopia.com
Feb 27, 2003
Juergen:
I have not tested it, but I think that substituting "compare" by a
"custom_compare" in all places where "compare" appears will do the trick.
For an example, please look at my diff package.
Best regards.
----- Original Message -----
From: Juergen Luethje <eu.lue at gmx.de>
Subject: stable sorting
>
> I wrote:
>
> > Subject: Re: Euphoria 2.4 Alpha-test Release!
>
> [...]
> > 5) Documentation of sort():
> > Nothing special with version 2.4, I just came across it.
> > I think it would be valuable to mention, whether or not the sorting
> > of sort() is stable, along with a short explanation for beginners,
> > what that means.
> [...]
>
> Probably I should have been somewhat more specific. I actually was
> thinking more of custom_sort(), than of sort():
> Say I have a list of records with student names and marks, already
> sorted by name. Then i want to sort this list by mark, so that students
> with the same mark remain listed in alphabetical order.
>
> The following code (modified after 'csort.ex') does *not* perform a
> stable sorting.
>
> ----------=----------=----------=----------=----- begin of code
> include sort.e
>
> constant MARK = 2
> constant statistics = {
> {"Doe", 3},
> {"Einstein", 1},
> {"Goldberg", 1},
> {"Irving", 4},
> {"Jones", 3},
> {"Miller", 2},
> {"Neuman", 2},
> {"Petersen", 4},
> {"Smith", 2},
> {"Zander", 5}
> }
>
> function compare_mark (sequence a, sequence b)
> -- Compare two sequences (records) according to MARK.
> return compare(a[MARK], b[MARK])
> end function
>
> integer by_mark
> sequence sorted_by_mark
>
> by_mark = routine_id("compare_mark")
> sorted_by_mark = custom_sort(by_mark, statistics)
>
> puts(1, "sorted by mark\n\n")
> for i = 1 to length(sorted_by_mark) do
> printf(1, "%12s %d\n", sorted_by_mark[i])
> end for
> ----------=----------=----------=----------=----- end of code
>
> Is there a library in the archive, with which this can be achieved?
> I didn't find one.
>
> Ricardo, I saw that your functions GradeDown() and GradeUp() in
> 'genfunc.e' perform stable sorting. But given the above example, there
> is no way to tell them just to sort by mark, and not to compare the
> whole records, right?
>
>
> Best regards,
> Juergen
>
> --
> /"\ ASCII ribbon campain |
> \ / against HTML in | Superstition brings bad luck.
> X e-mail and news, |
> / \ and unneeded MIME |
>
>
>
> TOPICA - Start your own email discussion group. FREE!
>