Skip to main content
Commonmark migration
Source Link

##Ruby##

Ruby

Everyone knows: sorting is very slow and takes many cycles (the best you can do is something with n log(n)). Thus it is quite easy to check if the array is sorted. All you have to do is compare the runtime of sorting the array and sorting the sorted array.

array = [1, 5, 4, 2, 3] ## measure the time needed to sort the array 1m times tstart = Time.now 1000000.times { array.sort } trun = Time.now - tstart ## now do a reference measurement on a sorted array array.sort! tstart = Time.now 1000000.times { array.sort } treference = Time.now - tstart ## compare run times if trun > treference print "array was not sorted" else print "array was sorted" end 

##Ruby##

Everyone knows: sorting is very slow and takes many cycles (the best you can do is something with n log(n)). Thus it is quite easy to check if the array is sorted. All you have to do is compare the runtime of sorting the array and sorting the sorted array.

array = [1, 5, 4, 2, 3] ## measure the time needed to sort the array 1m times tstart = Time.now 1000000.times { array.sort } trun = Time.now - tstart ## now do a reference measurement on a sorted array array.sort! tstart = Time.now 1000000.times { array.sort } treference = Time.now - tstart ## compare run times if trun > treference print "array was not sorted" else print "array was sorted" end 

Ruby

Everyone knows: sorting is very slow and takes many cycles (the best you can do is something with n log(n)). Thus it is quite easy to check if the array is sorted. All you have to do is compare the runtime of sorting the array and sorting the sorted array.

array = [1, 5, 4, 2, 3] ## measure the time needed to sort the array 1m times tstart = Time.now 1000000.times { array.sort } trun = Time.now - tstart ## now do a reference measurement on a sorted array array.sort! tstart = Time.now 1000000.times { array.sort } treference = Time.now - tstart ## compare run times if trun > treference print "array was not sorted" else print "array was sorted" end 
Syntax coloring
Source Link
Victor Stafusa
  • 8.8k
  • 5
  • 42
  • 63

##Ruby##

Everyone knows: sorting is very slow and takes many cycles (the best you can do is something with n log(n)). Thus it is quite easy to check if the array is sorted. All you have to do is compare the runtime of sorting the array and sorting the sorted array.

array = [1, 5, 4, 2, 3] ## measure the time needed to sort the array 1m times tstart = Time.now 1000000.times { array.sort } trun = Time.now - tstart ## now do a reference measurement on a sorted array array.sort! tstart = Time.now 1000000.times { array.sort } treference = Time.now - tstart ## compare run times if trun > treference print "array was not sorted" else print "array was sorted" end 
array = [1, 5, 4, 2, 3] ## measure the time needed to sort the array 1m times tstart = Time.now 1000000.times { array.sort } trun = Time.now - tstart ## now do a reference measurement on a sorted array array.sort! tstart = Time.now 1000000.times { array.sort } treference = Time.now - tstart ## compare run times if trun > treference print "array was not sorted" else print "array was sorted" end 

##Ruby##

Everyone knows: sorting is very slow and takes many cycles (the best you can do is something with n log(n)). Thus it is quite easy to check if the array is sorted. All you have to do is compare the runtime of sorting the array and sorting the sorted array.

array = [1, 5, 4, 2, 3] ## measure the time needed to sort the array 1m times tstart = Time.now 1000000.times { array.sort } trun = Time.now - tstart ## now do a reference measurement on a sorted array array.sort! tstart = Time.now 1000000.times { array.sort } treference = Time.now - tstart ## compare run times if trun > treference print "array was not sorted" else print "array was sorted" end 

##Ruby##

Everyone knows: sorting is very slow and takes many cycles (the best you can do is something with n log(n)). Thus it is quite easy to check if the array is sorted. All you have to do is compare the runtime of sorting the array and sorting the sorted array.

array = [1, 5, 4, 2, 3] ## measure the time needed to sort the array 1m times tstart = Time.now 1000000.times { array.sort } trun = Time.now - tstart ## now do a reference measurement on a sorted array array.sort! tstart = Time.now 1000000.times { array.sort } treference = Time.now - tstart ## compare run times if trun > treference print "array was not sorted" else print "array was sorted" end 
Source Link
Howard
  • 23.6k
  • 2
  • 45
  • 83

##Ruby##

Everyone knows: sorting is very slow and takes many cycles (the best you can do is something with n log(n)). Thus it is quite easy to check if the array is sorted. All you have to do is compare the runtime of sorting the array and sorting the sorted array.

array = [1, 5, 4, 2, 3] ## measure the time needed to sort the array 1m times tstart = Time.now 1000000.times { array.sort } trun = Time.now - tstart ## now do a reference measurement on a sorted array array.sort! tstart = Time.now 1000000.times { array.sort } treference = Time.now - tstart ## compare run times if trun > treference print "array was not sorted" else print "array was sorted" end