Skip to main content
added 271 characters in body
Source Link
Doorknob
  • 71.7k
  • 20
  • 143
  • 385

Ruby, 99 chars (Gnome sort)

a=gets.scan /\w+/
p=1
while a[p]
a[p]>a[p-1]?p+=2:(a[p],a[p-1]=a[p-1],a[p])
p-=1if p>1
end
$><<a*?,

This just barely beats my bubble sort implementation:

Ruby, 110 104 101 chars (Bubble sortBubble sort)

s=gets.scan /\w+/
(z=s.size).times{(0..(z-2)).map{|i|s[i],s[i+1]=s[i+1],s[i]if s[i]>s[i+1]}}
$><<s*?,

It's a simple bubble sort implementation. ItThis does list.length iterations, because worst-case scenario takes list.length - 1 iterations and one more really doesn't matter, and saves 2 chars.

Sample run:

c:\a\ruby>bubblesort.rb
hello,this,is,a,test,for,my,sorting,thingy,for,codegolf,stackexchange,it,uses,bubble,sort,and,it,is,sorty,and,yay,filler,items,blah,blah,blah,potato
a,and,and,blah,blah,blah,bubble,codegolf,filler,for,for,hello,is,is,it,it,items,my,potato,sort,sorting,sorty,stackexchange,test,thingy,this,uses,yay

Just for fun, a Quicksort version:

Ruby, 113 chars (QuicksortQuicksort)

q=->a{if a[1]
p=a.shift
l=[]
g=[]
a.map{|x|(x>p ?g:l).push x}
q[l]+[p]+q[g]
else
a
end}
$><<q[gets.scan /\w+/]*?,

Ruby, 110 104 101 chars (Bubble sort)

s=gets.scan /\w+/
(z=s.size).times{(0..(z-2)).map{|i|s[i],s[i+1]=s[i+1],s[i]if s[i]>s[i+1]}}
$><<s*?,

It's a simple bubble sort implementation. It does list.length iterations, because worst-case scenario takes list.length - 1 iterations and one more really doesn't matter, and saves 2 chars.

Sample run:

c:\a\ruby>bubblesort.rb
hello,this,is,a,test,for,my,sorting,thingy,for,codegolf,stackexchange,it,uses,bubble,sort,and,it,is,sorty,and,yay,filler,items,blah,blah,blah,potato
a,and,and,blah,blah,blah,bubble,codegolf,filler,for,for,hello,is,is,it,it,items,my,potato,sort,sorting,sorty,stackexchange,test,thingy,this,uses,yay

Just for fun, a Quicksort version:

Ruby, 113 chars (Quicksort)

q=->a{if a[1]
p=a.shift
l=[]
g=[]
a.map{|x|(x>p ?g:l).push x}
q[l]+[p]+q[g]
else
a
end}
$><<q[gets.scan /\w+/]*?,

Ruby, 99 chars (Gnome sort)

a=gets.scan /\w+/
p=1
while a[p]
a[p]>a[p-1]?p+=2:(a[p],a[p-1]=a[p-1],a[p])
p-=1if p>1
end
$><<a*?,

This just barely beats my bubble sort implementation:

Ruby, 110 104 101 chars (Bubble sort)

s=gets.scan /\w+/
(z=s.size).times{(0..(z-2)).map{|i|s[i],s[i+1]=s[i+1],s[i]if s[i]>s[i+1]}}
$><<s*?,

This does list.length iterations, because worst-case scenario takes list.length - 1 iterations and one more really doesn't matter, and saves 2 chars.

Just for fun, a Quicksort version:

Ruby, 113 chars (Quicksort)

q=->a{if a[1]
p=a.shift
l=[]
g=[]
a.map{|x|(x>p ?g:l).push x}
q[l]+[p]+q[g]
else
a
end}
$><<q[gets.scan /\w+/]*?,
added 250 characters in body
Source Link
Doorknob
  • 71.7k
  • 20
  • 143
  • 385

Ruby, 110 104 101 chars (Bubble sort)

s=gets.scan /\w+/
(z=s.size).times{(0..(z-2)).map{|i|s[i],s[i+1]=s[i+1],s[i]if s[i]>s[i+1]}}
$><<s*?,

It's a simple bubble sort implementation. It does list.length iterations, because worst-case scenario takes list.length - 1 iterations and one more really doesn't matter, and saves 2 chars.

Sample run:

c:\a\ruby>codegolfsort\a\ruby>bubblesort.rb
hello,this,is,a,test,for,my,sorting,thingy,for,codegolf,stackexchange,it,uses,bubble,sort,and,it,is,sorty,and,yay,filler,items,blah,blah,blah,potato
a,and,and,blah,blah,blah,bubble,codegolf,filler,for,for,hello,is,is,it,it,items,my,potato,sort,sorting,sorty,stackexchange,test,thingy,this,uses,yay

Just for fun, a Quicksort version:

Ruby, 113 chars (Quicksort)

q=->a{if a[1]
p=a.shift
l=[]
g=[]
a.map{|x|(x>p ?g:l).push x}
q[l]+[p]+q[g]
else
a
end}
$><<q[gets.scan /\w+/]*?,

Ruby, 110 104 101 chars

s=gets.scan /\w+/
(z=s.size).times{(0..(z-2)).map{|i|s[i],s[i+1]=s[i+1],s[i]if s[i]>s[i+1]}}
$><<s*?,

It's a simple bubble sort implementation. It does list.length iterations, because worst-case scenario takes list.length - 1 iterations and one more really doesn't matter, and saves 2 chars.

Sample run:

c:\a\ruby>codegolfsort.rb
hello,this,is,a,test,for,my,sorting,thingy,for,codegolf,stackexchange,it,uses,bubble,sort,and,it,is,sorty,and,yay,filler,items,blah,blah,blah,potato
a,and,and,blah,blah,blah,bubble,codegolf,filler,for,for,hello,is,is,it,it,items,my,potato,sort,sorting,sorty,stackexchange,test,thingy,this,uses,yay

Ruby, 110 104 101 chars (Bubble sort)

s=gets.scan /\w+/
(z=s.size).times{(0..(z-2)).map{|i|s[i],s[i+1]=s[i+1],s[i]if s[i]>s[i+1]}}
$><<s*?,

It's a simple bubble sort implementation. It does list.length iterations, because worst-case scenario takes list.length - 1 iterations and one more really doesn't matter, and saves 2 chars.

Sample run:

c:\a\ruby>bubblesort.rb
hello,this,is,a,test,for,my,sorting,thingy,for,codegolf,stackexchange,it,uses,bubble,sort,and,it,is,sorty,and,yay,filler,items,blah,blah,blah,potato
a,and,and,blah,blah,blah,bubble,codegolf,filler,for,for,hello,is,is,it,it,items,my,potato,sort,sorting,sorty,stackexchange,test,thingy,this,uses,yay

Just for fun, a Quicksort version:

Ruby, 113 chars (Quicksort)

q=->a{if a[1]
p=a.shift
l=[]
g=[]
a.map{|x|(x>p ?g:l).push x}
q[l]+[p]+q[g]
else
a
end}
$><<q[gets.scan /\w+/]*?,
added 8 characters in body
Source Link
Doorknob
  • 71.7k
  • 20
  • 143
  • 385

Ruby, 110 104104 101 chars

s=gets.chop.splitscan ?,/\w+/
(z=s.size).times{(0..(z-2)).map{|i|s[i],s[i+1]=s[i+1],s[i]if s[i]>s[i+1]}}
$><<s*?,

It's a simple bubble sort implementation. It does list.length iterations, because worst-case scenario takes list.length - 1 iterations and one more really doesn't matter, and saves 2 chars.

Sample run:

c:\a\ruby>codegolfsort.rb
hello,this,is,a,test,for,my,sorting,thingy,for,codegolf,stackexchange,it,uses,bubble,sort,and,it,is,sorty,and,yay,filler,items,blah,blah,blah,potato
a,and,and,blah,blah,blah,bubble,codegolf,filler,for,for,hello,is,is,it,it,items,my,potato,sort,sorting,sorty,stackexchange,test,thingy,this,uses,yay

Ruby, 110 104 chars

s=gets.chop.split ?,
(z=s.size).times{(0..(z-2)).map{|i|s[i],s[i+1]=s[i+1],s[i]if s[i]>s[i+1]}}
$><<s*?,

It's a simple bubble sort implementation. It does list.length iterations, because worst-case scenario takes list.length - 1 iterations and one more really doesn't matter, and saves 2 chars.

Sample run:

c:\a\ruby>codegolfsort.rb
hello,this,is,a,test,for,my,sorting,thingy,for,codegolf,stackexchange,it,uses,bubble,sort,and,it,is,sorty,and,yay,filler,items,blah,blah,blah,potato
a,and,and,blah,blah,blah,bubble,codegolf,filler,for,for,hello,is,is,it,it,items,my,potato,sort,sorting,sorty,stackexchange,test,thingy,this,uses,yay

Ruby, 110 104 101 chars

s=gets.scan /\w+/
(z=s.size).times{(0..(z-2)).map{|i|s[i],s[i+1]=s[i+1],s[i]if s[i]>s[i+1]}}
$><<s*?,

It's a simple bubble sort implementation. It does list.length iterations, because worst-case scenario takes list.length - 1 iterations and one more really doesn't matter, and saves 2 chars.

Sample run:

c:\a\ruby>codegolfsort.rb
hello,this,is,a,test,for,my,sorting,thingy,for,codegolf,stackexchange,it,uses,bubble,sort,and,it,is,sorty,and,yay,filler,items,blah,blah,blah,potato
a,and,and,blah,blah,blah,bubble,codegolf,filler,for,for,hello,is,is,it,it,items,my,potato,sort,sorting,sorty,stackexchange,test,thingy,this,uses,yay
deleted 210 characters in body
Source Link
Doorknob
  • 71.7k
  • 20
  • 143
  • 385
Loading
deleted 210 characters in body
Source Link
Doorknob
  • 71.7k
  • 20
  • 143
  • 385
Loading
Source Link
Doorknob
  • 71.7k
  • 20
  • 143
  • 385
Loading