Created
April 8, 2014 01:41
-
-
Save Harryyan/10081495 to your computer and use it in GitHub Desktop.
Ruby实现希尔排序
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
def shell_sort data,increment | |
len_data = data.length | |
len_increment = increment.length | |
for k in 0..(len_increment - 1) | |
for i in increment[k]..(len_data - 1) | |
tmp = data[i] | |
j = i | |
while j>=increment[k] do | |
if tmp >= data[j - increment[k]] | |
break | |
else | |
data[j] = data[j - increment[k]] | |
end | |
j -=increment[k] | |
end | |
data[j] = tmp | |
end | |
end | |
return data | |
end | |
increment = [5,3,1] | |
data = [1,2,3,4,5,6,7,8,9,11].shuffle | |
data2 = [34,28,81,79,22,16,55,24,99,64,46] | |
p "Before Shell Sort:" | |
p data | |
result = shell_sort data,increment | |
p "After Sort:" | |
p result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment