Re: stable sorting

new topic     » goto parent     » topic index » view thread      » older message » newer message

Hi Bernie,

thanks for your pseudocode.
I tried to translate it:

-------------------==--------------------==------------------------
include sort.e

constant NAME = 1, 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_name (sequence a, sequence b)
   -- Compare two sequences (records) according to MARK.
   return compare(a[NAME], b[NAME])
end function

function compare_mark (sequence a, sequence b)
   -- Compare two sequences (records) according to MARK.
   return compare(a[MARK], b[MARK])
end function

integer by_name, by_mark
sequence sorted

by_name = routine_id("compare_name")
by_mark = routine_id("compare_mark")


> -- first sort by name

sorted = custom_sort(by_name, statistics)

> -- then do the following
> sequence temp temp = {}

sequence temp
temp = repeat({0,0}, length(statistics))

> -- reverse name and mark
> for i = 1 to length() do
>   temp[i][1] = statistics[i][1]
>   temp[i][2] = statistics[i][2]
> end for

-- I think, in the above lines there is a typo.
-- If not, I also could say:
--    temp = statistics   smile
-- So I tried:

for i = 1 to length(statistics) do
  temp[i][1] = sorted[i][2]
  temp[i][2] = sorted[i][1]
end for

> -- then sort
> temp = custom_sort(by_mark, temp)

temp = custom_sort(by_mark, temp)

> -- then reverse the name
> for i = 1 to length() do
>   statistics[i][1] = temp[i][1]
>   statistics[i][2] = temp[i][2]
> end for

-- Again a typo, I believe. So I tried:

for i = 1 to length(statistics) do
  sorted[i][1] = temp[i][2]
  sorted[i][2] = temp[i][1]
end for

> -- now statistics is sorted by name and mark
> -- A seperate function could be used for swapping

puts(1, "sorted by mark\n\n")
for i = 1 to length(sorted) do
   printf(1, "%12s %d\n", sorted[i])
end for
-------------------==--------------------==---------------------------

Now 'statistics' is sorted by name, it's not sorted by mark, so that
students with the same mark are listed in alphabetical order.
Did I make an error in translating your pseudocode?

Best regards,
   Juergen

-- 
 /"\  ASCII ribbon campain  |  I didn't have time to write
 \ /  against HTML in       |  a short letter, so I wrote
  X   e-mail and news,      |  a long one instead.
 / \  and unneeded MIME     |  [Mark Twain]

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu