A variety of sorting algorithms implemented by Ruby _ruby Special Topics

Source: Internet
Author: User

Complexity of Time: Theta (n^2)

Bubble sort

Copy Code code as follows:

Def Bubble_sort (a)
(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 a
End

Selection sort

Copy Code code as follows:

Def Selection_sort (a)
b = []
A.size.times do |i|
Min = a.min
b << min
A.delete_at (min) (a.index)
End
Return b
End

Insertion Sort

Copy Code code as follows:

Def Insertion_sort (a)
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 a
End

Shell sort

Copy Code code as follows:

Def Shell_sort (a)
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 a
End

complexity of Time: Theta (N*LOGN)

Merge sort

Copy Code code 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 (a)
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 Code code 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 (a)
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 (a)
Return a If A.size <= 1
Size = A.size
Build_heap (a)
While size > 0
A[0], a[size-1] = a[size-1], a[0]
Size = Size-1
Heapify (A, 0, size)
End
Return a
End

Quick sort

Copy Code code as follows:

Def Quick_sort (a)
(X=a.pop)? Quick_sort (a.select{|i| i <= x}) + [x] + quick_sort (a.select{|i| i > x}): []
End

Time complexity: Theta (N)

Counting sort

Copy Code code as follows:

Def Counting_sort (a)
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 Code code as follows:

def kth_digit (n, i)
while (i > 1)
n = N/10
i = I-1
End
N% 10
End

Def Radix_sort (a)
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 a
End

Bucket sort
Copy Code code as follows:

Def Quick_sort (a)
(X=a.pop)? Quick_sort (a.select{|i| i <= x}) + [x] + quick_sort (a.select{|i| i > x}): []
End

def first_number (N)
(n *). to_i
End

Def Bucket_sort (a)
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 (a)

# Result:
[0, 0.01, 0.1234567, 0.13, 0.44, 0.55, 0.75, 0.98]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.