Skip to content

Commit 4585685

Browse files
sahupankaj10thuva4
authored andcommitted
Added shell and counting sort in Ruby (thuva4#478) (thuva4#480) (thuva4#479)
* Create ShellSort.rb * Update Readme.md for thuva4#478 -- shell sort in Ruby * Create CountingSort.rb * added Counting Sort in Ruby
1 parent 0315cd5 commit 4585685

3 files changed

Lines changed: 81 additions & 2 deletions

File tree

CountingSort/Ruby/CountingSort.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
=begin
2+
#Counting Sort is a linear time sort used when range of keys is already known.
3+
#Algorithm
4+
1. Take a count array to store the frequency of each value in given range
5+
2. change count[i] to count[i]+count[i-1],i.e each element now stores the prefix sum of counts.
6+
3. take each value from the array and put it at the correct index in output array using count, decrement value of count! (correct index of a[i] will be count[a[i]-1])
7+
4. Finally copy the values of output array to array.
8+
9+
# n is the size of array and k is the range of input
10+
#Time-complexity: O(n+k), Auxiliary-space:O(n+k), Not In-place, Not stable
11+
=end
12+
13+
14+
def counting_sort(a=[9,8,7,6],min=0,max=10)
15+
if min>max
16+
return "invalid range"
17+
end
18+
19+
n=max-min+1
20+
count=Array.new(n,0)
21+
len=a.length
22+
output=Array.new(len)
23+
24+
for i in 0...len
25+
count[a[i]-min]+=1
26+
end
27+
28+
for i in 1...n
29+
count[i]+=count[i-1]
30+
end
31+
32+
33+
for i in 0...len
34+
output[count[a[i]-min]-1]=a[i]
35+
count[a[i]-min]-=1
36+
end
37+
38+
for i in 0...len
39+
a[i]=output[i]
40+
end
41+
42+
return a
43+
44+
end
45+
46+
puts(counting_sort([9,8,1,2,3,7],-3,10))

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ BreadthFirstSearch | :+1: | :+1: | | :+1: | | | | |
1818
Borwein's Algorithm | :+1: | | | | :+1: | | | | ||
1919
BubbleSort | :+1: | :+1: | :+1: | :+1: | :+1: | :+1: | :+1: | :+1: | :+1: | | | :+1:
2020
Conjugate Gradient | | | | | :+1: | | | | ||
21-
CountingSort | :+1: | :+1: | | | :+1: | | | | |
21+
CountingSort | :+1: | :+1: | | | :+1: | | | | :+1: |
2222
CycleSort | :+1: | :+1: | | | :+1: | | | | |
2323
DepthFirstSearch | :+1: | :+1: | | | :+1: | :+1: | | | |
2424
Dijkstra's | :+1: | :+1: | | | :+1: | | :+1: | | |
@@ -55,7 +55,7 @@ Uniform-cost search | :+1: | | | | | :+1: | :+1: | |
5555
RadixSort | :+1: | :+1: | | | :+1: | | | |
5656
RobinCarp | :+1: | | | | | | | |
5757
SelectionSort | :+1: | :+1: | :+1: | :+1: | :+1: | :+1: | :+1: | :+1: | :+1:
58-
ShellSort | :+1: | :+1: | | | :+1: | | | |
58+
ShellSort | :+1: | :+1: | | | :+1: | | | | :+1: |
5959
Sieve of Eratosthenes | :+1: | :+1: | | | :+1: | :+1: | :+1: | |
6060
UnaryCoding | :+1: | :+1: | | | | :+1: | | |
6161
VEGAS Algorithm | | | | | :+1: | | | | ||

ShellSort/Ruby/ShellSort.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#Shell Sort implementation(Diminishing Increment Sort)
2+
#Time-complexity: O(n^2), In-place
3+
#will be using Knuth series :3n+1
4+
5+
def shell_sort(a)
6+
n=a.length
7+
h=1
8+
9+
while (h<n/3) #for computing increment factor "h"
10+
h= (3*h)+1
11+
end
12+
13+
while h>=1
14+
# Logic of insertion sort with inrement steps of "h"
15+
for i in h...n
16+
j=i
17+
while j>=h
18+
if a[j-h]>a[j]
19+
temp=a[j]
20+
a[j]=a[j-h]
21+
a[j-h]=temp
22+
end
23+
j-=h
24+
end
25+
end
26+
h/=3
27+
end
28+
29+
return a
30+
31+
end
32+
33+
puts(shell_sort([0,5,4,7,1,8,9,3,7,1,4,2,8,6]))

0 commit comments

Comments
 (0)