Forum: Ruby Own sort method

Posted by Artem B. (artem_b)
on 2012-11-26 12:45
I have wrote my own sort method for array:

http://pastebin.com/r4rDDs4A

But now I want to compare my method with core-method, how can I do it?
Where can I find official core-method sort?
Posted by Robert Klemme (robert_k78)
on 2012-11-26 13:05
(Received via mailing list)
On Mon, Nov 26, 2012 at 12:45 PM, Artem B. <lists@ruby-forum.com> wrote:
> I have wrote my own sort method for array:

Why?

> http://pastebin.com/r4rDDs4A
>
> But now I want to compare my method with core-method, how can I do it?
> Where can I find official core-method sort?

https://github.com/ruby/ruby/blob/trunk/array.c#LC2289

Cheers

robert
Posted by Artem B. (artem_b)
on 2012-11-26 13:38
> Why?
It was be an exercise of learning guide :)

Thanks, and what do you think about my code?
Posted by Robert Klemme (robert_k78)
on 2012-11-26 13:49
(Received via mailing list)
On Mon, Nov 26, 2012 at 1:38 PM, Artem B. <lists@ruby-forum.com> wrote:
>> Why?
> It was be an exercise of learning guide :)
>
> Thanks, and what do you think about my code?

You are not sorting Arrays but Arrays which must be filled with
objects that respond to #[] and return items which can be compared
with #< (line 4).

I am not even sure that the sorting will work - at least with this input

words = %w{cb ca b a}

you won't get a regular order:

["a", "b", "cb", "ca"]

That's because you sort by the first letter only.  You'll also get
wrong output with this input

words = ["", "z", "a"]

Kind regards

robert
Posted by Artem B. (artem_b)
on 2012-11-26 13:54
Thank you. Problem is solved
Posted by unknown (Guest)
on 2012-11-26 15:32
(Received via mailing list)
Am 26.11.2012 13:38, schrieb Artem B.:
>> Why?
> It was be an exercise of learning guide :)
>
> Thanks, and what do you think about my code?

You can swap array elements more easily with a parallel assignment:

   words[i], words[i+1] = words[i+1], words[i]
Posted by Jan E. (jacques1)
on 2012-11-26 15:50
Hi,

this is Gnome Sort/Stupid sort, by the way:

http://en.wikipedia.org/wiki/Gnome_sort
Posted by Jarrod Henry (Guest)
on 2012-11-26 15:56
(Received via mailing list)
StupidSort/Gnome Sort vs Quicksort.

I predict that the OPs sort will be exponentially slower.

J
Posted by Artem B. (artem_b)
on 2012-11-26 15:58
> words[i], words[i+1] = words[i+1], words[i]

Exactly! I forget it! :)

> this is Gnome Sort/Stupid sort, by the way:

haha - my mind like Gnome:) Is it bad/slow method?
Posted by Jan E. (jacques1)
on 2012-11-26 17:23
Artem B. wrote in post #1086501:
> haha - my mind like Gnome:) Is it bad/slow method?

Yes -- compared to "serious" algorithms like quick sort.

I hope you didn't actually write that code for performance? Because a
"home made" implementation written in a slow language doesn't stand a
chance against an optimized algorithm written in C (like Ruby's sort()
method).
Posted by Artem B. (artem_b)
on 2012-11-26 18:09
> I hope you didn't actually write that code for performance? Because a
"home made" implementation written in a slow language doesn't stand a
chance against an optimized algorithm written in C (like Ruby's sort()
method).

Sure, it was for learnin Ruby :)
I got that C-methods very faster of my
Posted by Stu (Guest)
on 2012-11-27 09:49
(Received via mailing list)
On Mon, Nov 26, 2012 at 11:09 AM, Artem B. <lists@ruby-forum.com> wrote:

>
>
> Sure, it was for learnin Ruby :)
> I got that C-methods very faster of my
>
> --
> Posted via http://www.ruby-forum.com/.
>
>
Do a bubble sort for learning sorts and algorithms. For complexity 
attempt
to do it against single characters instead of strings. After that look 
into
the divide and conquer concepts for further study and understanding of 
the
"way" of thinking with various types of  input.
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.