Ruby array sorting implementation and performance comparison
Wei renyan 2010.8.21
Recently, I have been studying programming Pearl River. I have explained various sorting methods and gradually explained the advantages and disadvantages of optimizing each sort, the technology intuitively describes how to improve program performance through optimization, learn a lot of knowledge, and broaden the ideas for solving problems. The biggest feature of this book is that he raises many questions for you to think about and solve. These problems are very practical. (We recommend you read and taste her ). The following are two sorting methods I implemented using RUBY: insert sorting and quick sorting (after several hours of debugging, I finally finished it, haha ).
The Code is as follows >>>
#! /Usr/bin/ruby <br/> require "time" <br/> puts "Hello, world... /n "; </P> <p> N = 10000 <br/> A = [] <br/> # randomly generated array data <br/> for I in 0 .. n-1 <br/> If I % 3 = 0 & I % 2 = 0 <br/> A [I] =-rand (100) <br/> elsif I % 4 = 0 & I % 5 = 0 <br/> A [I] =-rand (100) <br/> A [I-1] =-A [I-1] <br/> else <br/> A [I] = rand (100) <br/> end <br/> # A = [, 0,-,] <br/> puts. join ("") <br/> # insert sort 1 <br/> def sort (a) <br/> # array length <br/> Len =. length </P> <p> for I in 0 .. len-1 <br/> T = A [I] <br/> for J in 0 .. I <br/> If a [J]> T <br/> A [J], t = t, A [J] <br/> end <br/> A [I] = T <br/> end </P> <p> return a <br /> end <br/> # insert sort 2 <br/> def sort1 () <br/> # array length <br/> Len =. length <br/> for I in 0 .. len-1 </P> <p> for J in 0 .. I <br/> If j> 0 and a [J-1]> A [I] <br/> A [I], a [J-1] = A [J-1], A [I] <br/> end </P> <p> return a <br/> end <br/> # Quick sorting <br/> def qsort () <br/> L = 0 <br/> Len =. length <br/> U = len-1 </P> <p> qsort1 (A, L, U) <br/> return a <br/> end <br/> # actual running process of quick sorting <br/> def qsort1 (A, L, U) <br/> if l> = u <br/> return <br/> end </P> <p> M = L <br/> # puts "ST: /NL: # {L}/Tm: # {m} "<br/> for I in L + 1 .. U <br/> # puts "I: # {I}/Tm: # {m} "<br/> If a [I] <A [l] </P> <p> M = m + 1 <br/> # puts" [# {m}] <-A [# {I}]: A [# {m}] <-# {A [I]} "<br/> # puts" A [# {I}] <-A [# {m}]: A [# {I}] <-# {A [m]} "<br/> A [m], a [I] = A [I], A [m] <br/> end <br/> # puts. join ("") </P> <p> end </P> <p> A [L], a [m] = A [m], A [l] <br/> # puts. join ("") <br/> # puts "ed:/NL: # {L}/Tm: # {m}" <br/> # puts "ST1: /n: A [# {L }.. # {M-1}] "<br/> qsort1 (A, l m-1) </P> <p> # puts" st2:/N: A [# {m + 1 }.. # {u}] "<br/> qsort1 (a, m + 1, U) <br/> end <br/> # test quick sorting <br/> Start = time. now <br/> puts "start... # {start} "<br/> B = qsort (a) <br/> # B = qsort1 (A, 0,. length-1) <br/> puts B. join ("") <br/> ED = time. now <br/> cs = ed-start <br/> puts "end... # {ed} "<br/> puts" consnum .. # {CS} s/n "<br/> # test insertion sorting <br/> Start = time. now <br/> puts "start... # {start} "<br/> B = sort (a) <br/> puts B. join ("") <br/> ED = time. now <br/> cs = ed-start <br/> puts "end... # {ed} "<br/> puts" consnum .. # {CS} s/n "</P> <p>
If there is a large amount of data, the two implementations are significantly different in order of magnitude. I just heard that I have never seen them before. Now I finally know the beauty of the algorithm.
Test results:
N: 100
Insert sorting: 0.0 s
Fast sorting: 0.0 s
N: 1000
Insert sorting: 0.078 s
Fast sorting: 0.406 s
N: 10000
Insert sorting: 1.125 s
Fast sorting: 34.953 s
N: 50000
Insert sorting: 11.078 s
Fast sorting: 899.968 s
N: 100000
Insert sorting: 36.796 s
Fast sorting: 3404.172 s
The above is just the rough data tested on my machine (because at that time, I was still using QQ and Firefox ).
In this practice, I remember that in ruby, ++ I and I ++ cannot be used. Haha. Practice is the best learning process.
Note: If you need to reprint the record, please indicate the source. Thank you!
Http://blog.csdn.net/savechina