d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Sorting Advice > 9000 Random Generated Integers Sorted
123Next
Add Reply New Topic New Poll
Member
Posts: 2,423
Joined: Mar 16 2010
Gold: 60.00
May 30 2013 02:28pm
In a personal project I need to sort 9000 randomly generated integers (from 1 - 100) and I was wondering which type of sort would be the most efficient? (Sorting from highest to lowest)

I greatly appreciate anyones help! Thanks!
Member
Posts: 50,343
Joined: Apr 3 2008
Gold: 0.00
May 30 2013 05:05pm
Quote (hell78 @ May 30 2013 12:28pm)
In a personal project I need to sort 9000 randomly generated integers (from 1 - 100) and I was wondering which type of sort would be the most efficient? (Sorting from highest to lowest)

I greatly appreciate anyones help! Thanks!

from abduct who is currently suspended:

Code

require 'benchmark'

def bubblesort( list )
 mylist = list.dup
 return mylist if mylist.size <= 1 # already sorted

 swapped = true
 while swapped
   swapped = false # maybe this time, we won't find a swap
   0.upto( mylist.size - 2 ) do |i|
     if mylist[i] > mylist[i + 1]
       mylist[i], mylist[i + 1] = mylist[i + 1], mylist[i] # swap values
       swapped = true # found a swap... keep going
     end
   end
 end

 mylist
end

def selectionsort( list )
 mylist = list.dup
 
 mylist.each_index do |x|
   index_of_min = x
   x.upto( mylist.size - 1 ) do |y|
     if mylist[index_of_min] > mylist[y]
       index_of_min = y
     end
   end
   
   mylist[x], mylist[index_of_min] = mylist[index_of_min], mylist[x]
 end
 
 mylist
end


numbers = []

5000.times { numbers.push Random.rand(0..100) }

puts "Testing bubble sort over 10 sorts with 5000 numbers"


time = Benchmark.realtime do
  10.times { new_numbers = bubblesort numbers }
end
average_bubblesort = time

puts "Testing selection sort over 10 sorts with 5000 numbers"

time = Benchmark.realtime do
 10.times { new_numbers = selectionsort numbers }
end
average_selectionsort = time


puts "Bubble sort average time over 10 sorts:    #{average_bubblesort / 10} seconds per sort"
puts "Selection sort average time over 10 sorts: #{average_selectionsort / 10} seconds per sort"


Code

output

Testing bubble sort over 10 sorts with 5000 numbers
Testing selection sort over 10 sorts with 5000 numbers
Bubble sort average time over 10 sorts:    5.2923027 seconds per sort
Selection sort average time over 10 sorts: 1.5013859 seconds per sort


Jesus fuck, If he makes me edit this one more time I'm going to shoot him.

He made me edit it again.

I hate him.

This post was edited by Rikuo on May 30 2013 05:16pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 30 2013 05:27pm
if you know they're integers from 1-100, make an integer array from 0-99 (treating 0 as 100 or offset). for each entry you see, increment the appropriate position in the array. after you finish going through all the numbers, go through your array and recreate the list in order.

This post was edited by carteblanche on May 30 2013 05:28pm
Member
Posts: 40,064
Joined: Nov 22 2009
Gold: 0.00
May 30 2013 11:17pm
quick sort implementation is pretty fast compared to bubble sort and selection sort, although ruby's sort! method beats them all by a long long shot. i do not know how ruby implements their sort though unfortunately.

Code

output

0.0004 seconds per sort (ruby sort)
0.1478085 seconds per sort (ruby implantation of quicksort)


Code

def quicksort(list, p, r)
 mylist = list.dup
   if p < r then
       q = partition(mylist, p, r)
       quicksort(mylist, p, q-1)
       quicksort(mylist, q+1, r)
   end
   
   mylist
end

def partition(list, p, r)
   pivot = list[r]
   i = p - 1
   p.upto(r-1) do |j|
       if list[j] <= pivot
           i = i+1
           list[i], list[j] = list[j],list[i]
       end        
   end
   list[i+1],list[r] = list[r],list[i+1]
   return i + 1
end

numbers = []

5000.times { numbers.push Random.rand(0..100) }

time = Benchmark.realtime do
 10.times { new_numbers = numbers.sort }
end

puts "#{time / 10} seconds per sort (ruby sort)"

time = Benchmark.realtime do
 10.times { new_numbers = quicksort numbers, 0, numbers.size-1 }
end

puts "#{time / 10} seconds per sort (ruby implantation of quicksort)"



From the duck with abs
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
May 30 2013 11:37pm
your input range is discrete.

everyone needs to stop overthinking this. radix sort, you fools. there is no better way.

This post was edited by irimi on May 30 2013 11:51pm
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
May 31 2013 04:27am
i did a very cheesy method which I think will make very good time (python):

Code
import random
randomlist= [random.randint(1,100) for _ in range(9000)]
totals=[0]*101
sortedoutput=[]
for number in randomlist:
   totals[number]+=1
for i, entry in enumerate(totals):
   for j in range(entry):
       sortedoutput.append(i)

Takes 0.017 seconds to create the list of 9000 random numbers.
Takes 0.003 seconds to "sort" them.

This post was edited by Azrad on May 31 2013 04:34am
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
May 31 2013 10:53am
Quote (Azrad @ May 31 2013 03:27am)
i did a very cheesy method which I think will make very good time (python):


Quote (irimi @ May 30 2013 10:37pm)
radix sort


:P
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Jun 1 2013 03:57pm
Abduct:
Here is my ruby radix sort implementation. faster than quick sort but rubys built in sort is still faster by a long shot

so here is the sorts ive tested so far

bubble sort, 5.2923027 seconds per sort
Selection sort, 1.5013859 seconds per sort
quick sort, 0.1478085 seconds per sort
radix sort, 0.004200199999999999 seconds per sort
custom sort, 0.0022002000000000002 seconds per sort
ruby's sort, 0.0004 seconds per sort

Code

 def radix_sort(base=10)
   ary = dup
   rounds = (Math.log(self.max.abs)/Math.log(base)).ceil
   rounds.times do |i|
     buckets = Hash.new {|h,k| h[k] = []}
     ary.each do |n|
       digit = (n/base**i) % base
       digit = digit + base unless n<0
       buckets[digit] << n
     end
     ary = buckets.values_at(*(0..2*base)).compact.flatten
     p [i, ary] if $DEBUG
   end
   ary
 end


Code

def customsort(array)
 temp = Array.new
 mylist = []
 array.each do |x|
   if temp[x] == nil
     temp[x] = 1
   else
     temp[x] = temp[x] + 1
   end
 end
 temp.each_with_index do |x, i|
   if (x)
     x.times do
       mylist.push i
     end
   end
 end
 mylist
end

numbers = []

5000.times { numbers.push Random.rand(0..100) }

time = Benchmark.realtime do
 10.times { new_numbers = customsort numbers }
end

puts "#{time / 10} seconds per sort (radix sort)"


This post was edited by Azrad on Jun 1 2013 04:10pm
Member
Posts: 3,321
Joined: May 20 2011
Gold: Locked
Jun 1 2013 08:07pm
Wouldn't Merge sort be the best because its worst and best case times are both nlogn. I also prefer it because it is recursive :)
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 1 2013 08:38pm
Quote (slapnutz2284 @ Jun 1 2013 10:07pm)
Wouldn't Merge sort be the best because its worst and best case times are both nlogn.  I also prefer it because it is recursive :)


Go back under your bridge.
Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll