Algorithm class operations, using the partition method, merge sort.
#encoding: Utf-8 #author: Xu Jin, 4100213 #date: Oct #MergeSort #to sort an array by using MergeSort algorithm # Example output: #The original Array is:[4,-----------------, 4, 21, 2 9, Arrayint, Panax, M, M, m, MAX = + array.new for index in (0..12) Arrayint[index] = rand (10 0 #produce random number end puts "The original array are:" + arrayint.to_s def merge (arr, left, middle, right) ARRL , arrr = array.new, Array.new arrl[0. ( Middle-left)], arrr[0. ( right-middle-1)] = Arr[left. Middle], Arr[middle + 1. Right] arrl[arrl.size], arrr[arrr.size]= Max, Max for K. right) Arrl.first <= Arrr.first? (Arr[k] = Arrl.shift): (arr[k] = arrr.shift) End-def merge_sort (arr, left, right) if left < right then middle
= (left + right)/2 Merge_sort (arr, left, middle) merge_sort (arr, middle + 1, right) merge (arr, left, middle, right) End-end Merge_sort (arrayint, 0, ARRayint.length-1) puts "the sorted array is:" + arrayint.to_s