Various sorting algorithms implemented by Ruby and Ruby
Time Complexity: Average (n ^ 2)
Bubble sort
Copy codeThe Code is as follows:
Def bubble_sort ()
(A. size-2). downto (0) do | I |
(0.. I). each do | j |
A [j], a [j + 1] = a [j + 1], a [j] if a [j]> a [j + 1]
End
End
Return
End
Selection sort
Copy codeThe Code is as follows:
Def selection_sort ()
B = []
A. size. times do | I |
Min = a. min
B <min
A. delete_at (a. index (min ))
End
Return B
End
Insertion sort
Copy codeThe Code is as follows:
Def insertion_sort ()
A. each_with_index do | el, I |
J = I-1
While j> = 0
Break if a [j] <= el
A [j + 1] = a [j]
J-= 1
End
A [j + 1] = el
End
Return
End
Shell sort
Copy codeThe Code is as follows:
Def shell_sort ()
Gap = a. size
While (gap> 1)
Gap = gap/2
(Gap .. a. size-1). each do | I |
J = I
While (j> 0)
A [j], a [j-gap] = a [j-gap], a [j] if a [j] <= a [j-gap]
J = j-gap
End
End
End
Return
End
Time Complexity: Random (n * logn)
Merge sort
Copy codeThe Code is as follows:
Def merge (l, r)
Result = []
While l. size> 0 and r. size> 0 do
If l. first <r. first
Result <l. shift
Else
Result <r. shift
End
End
If l. size> 0
Result + = l
End
If r. size> 0
Result + = r
End
Return result
End
Def merge_sort ()
Return a if a. size <= 1
Middle = a. size/2
Left = merge_sort (a [0, middle])
Right = merge_sort (a [middle, a. size-middle])
Merge (left, right)
End
Heap sort
Copy codeThe Code is as follows:
Def heapify (a, idx, size)
Left_idx = 2 * idx + 1
Right_idx = 2 * idx + 2
Bigger_idx = idx
Bigger_idx = left_idx if left_idx <size & a [left_idx]> a [idx]
Bigger_idx = right_idx if right_idx <size & a [right_idx]> a [bigger_idx]
If bigger_idx! = Idx
A [idx], a [bigger_idx] = a [bigger_idx], a [idx]
Heapify (a, bigger_idx, size)
End
End
Def build_heap ()
Last_parent_idx = a. length/2-1
I = last_parent_idx
While I> = 0
Heapify (a, I, a. size)
I = I-1
End
End
Def heap_sort ()
Return a if a. size <= 1
Size = a. size
Build_heap ()
While size> 0
A [0], a [size-1] = a [size-1], a [0]
Size = size-1
Heapify (a, 0, size)
End
Return
End
Quick sort
Copy codeThe Code is as follows:
Def quick_sort ()
(X = a. pop )? Quick_sort (a. select {| I <= x}) + [x] + quick_sort (a. select {| I> x}): []
End
Time Complexity: Average (n)
Counting sort
Copy codeThe Code is as follows:
Def counting_sort ()
Min = a. min
Max = a. max
Counts = Array. new (max-min + 1, 0)
A. each do | n |
Counts [n-min] + = 1
End
(0... counts. size). map {| I | [I + min] * counts [I]}. flatten
End
Radix sort
Copy codeThe Code is as follows:
Def kth_digit (n, I)
While (I> 1)
N = n/10
I = I-1
End
N % 10
End
Def radix_sort ()
Max = a. max
D = Math. log10 (max). floor + 1
(1. d). each do | I |
Tmp = []
(0 .. 9). each do | j |
Tmp [j] = []
End
A. each do | n |
Kth = kth_digit (n, I)
Tmp [kth] <n
End
A = tmp. flatten
End
Return
End
Bucket sort
Copy codeThe Code is as follows:
Def quick_sort ()
(X = a. pop )? Quick_sort (a. select {| I <= x}) + [x] + quick_sort (a. select {| I> x}): []
End
Def first_number (n)
(N * 10). to_ I
End
Def bucket_sort ()
Tmp = []
(0 .. 9). each do | j |
Tmp [j] = []
End
A. each do | n |
K = first_number (n)
Tmp [k] <n
End
(0 .. 9). each do | j |
Tmp [j] = quick_sort (tmp [j])
End
Tmp. flatten
End
A = [0.75, 0.13, 0, 0.44, 0.55, 0.01, 0.98, 0.1234567]
P bucket_sort ()
# Result:
[0, 0.01, 0.1234567, 0.13, 0.44, 0.55, 0.75, 0.98]