Own sort method

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?

On Mon, Nov 26, 2012 at 12:45 PM, Artem B. [email protected] 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

Why?
It was be an exercise of learning guide :slight_smile:

Thanks, and what do you think about my code?

On Mon, Nov 26, 2012 at 1:38 PM, Artem B. [email protected] wrote:

Why?
It was be an exercise of learning guide :slight_smile:

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

Am 26.11.2012 13:38, schrieb Artem B.:

Why?
It was be an exercise of learning guide :slight_smile:

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]

Thank you. Problem is solved

Hi,

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

http://en.wikipedia.org/wiki/Gnome_sort

StupidSort/Gnome Sort vs Quicksort.

I predict that the OPs sort will be exponentially slower.

J

words[i], words[i+1] = words[i+1], words[i]

Exactly! I forget it! :slight_smile:

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

haha - my mind like Gnome:) Is it bad/slow method?

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 :slight_smile:
I got that C-methods very faster of my

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).

On Mon, Nov 26, 2012 at 11:09 AM, Artem B. [email protected] wrote:

Sure, it was for learnin Ruby :slight_smile:
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.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs