def heapify(arr, n, i) largest = i # Initialize largest as root l =2*i +1# left = 2*i + 1 r =2*i +2# right = 2*i + 2# If left child is larger than rootif l < n and arr[l]> arr[largest] largest = lend# If right child is larger than largest so farif r < n and arr[r]> arr[largest] largest = rend# If largest is not rootif largest != i arr[i], arr[largest]= arr[largest], arr[i]# Recursively heapify the affected sub-tree heapify(arr, n, largest)endend# main function to do heap sortdef heap_sort(arr) n = arr.length# Build heap (rearrange array) (n/2-1).downto(0) do|i| heapify(arr, n, i)end# One by one extract an element from heap (n-1).downto(0) do|i|# Move current root to end arr[0], arr[i]= arr[i], arr[0]# call max heapify on the reduced heap heapify(arr, i, 0)endend# Driver codearr =[12, 11, 13, 5, 6, 7]heap_sort(arr)n = arr.lengthputs"Sorted array is"for i in0..n-1doprint arr[i]endputs
Sorted array is
567111213
Sequential Search
# Ruby program to search an item into the array# using linear searcharr =[12,45,23,39,37];i =0;item =0;flag =0;print"Enter item: ";# item = gets.chomp.to_i;item =12flag =-1while(i<arr.size)if(arr[i]==item) flag = i;break;end i = i +1;endif(flag>=0)print"Item found at index: ",flag,"\n";elseprint"Item not found\n";end# Ruby program to search an item into the array# using linear searcharr =[12,45,23,39,37];i =0;item =0;flag =0;print"Enter item: ";# item = gets.chomp.to_i;item =45flag =-1while(i<arr.size)if(arr[i]==item) flag = i;break;end i = i +1;endif(flag>=0)print"Item found at index: ",flag,"\n";elseprint"Item not found\n";end
Enter item: Item found at index: 0
Enter item: Item found at index: 1
Quick Sort
# This function takes last element as pivot, places# the pivot element at its correct position in sorted# array, and places all smaller (smaller than pivot)# to left of pivot and all greater elements to right# of pivotdef partition(arr, low, high) i = low -1 pivot = arr[high]for j in low..high -1if arr[j]<= pivot i +=1 arr[i], arr[j]= arr[j], arr[i]endend arr[i +1], arr[high]= arr[high], arr[i +1]return i +1end# The main function that implements QuickSort# arr[] --> Array to be sorted,# low --> Starting index,# high --> Ending indexdef quick_sort(arr, low, high)if low < high# pi is partitioning index, arr[p] is now# at right place pi = partition(arr, low, high)# Separately sort elements before# partition and after partition quick_sort(arr, low, pi -1) quick_sort(arr, pi +1, high)endend# Function to print an arraydef print_array(arr, size)for i in0..size -1print arr[i], " "endputsend# Driver codearr =[10, 7, 8, 9, 1, 5]n = arr.lengthquick_sort(arr, 0, n -1)puts"Sorted array: "print_array(arr, n)
def linear_search(array, element) i =0while i < array.lengthif array[i]== elementreturn"#{element} at index #{array.index(element)}"end i+=1endreturn-1endp linear_search([1,2], 2)
"2 at index 1"
"2 at index 1"
Binary Search
def binary_search(n, arr) middle = arr[arr.length/2] i =0 j = arr.length-1while i < jif middle == nreturntrueelsif middle < n i = middle middle = i + j /2elsep"Middle is greater than n" j = middlep"j: #{j}" middle = i + j /2p"middle: #{middle}"endendfalseendp binary_search(1, [1,3])
"Middle is greater than n"
"j: 3"
"middle: 1"
true
true
Shell Sort
classShellSorterdefself.sort(a =[3, 1, 2]) numComp =0 len = a.length k = len/2while(k >0.0)# Increment = k. We sort all k-sequences (sequences of elements# k-apart) using insertion sort over all k-sequences concurrently.for i in k..len-1do t = a[i] j = i while( (j >= k) && (a[j-k]> t) ) do numComp +=1 a[j]= a[j-k] j -= kend++numComp a[j]= t;end k = (k/(k ==2 ? 2 : 2.2)).floor# "divide by 2.2" increment sequenceendreturn numCompenddefself.randomArray(len =10) a =[]if(len<0) then len =0endfor i in0..len-1do a[i]= (rand*len).floorendreturn a;enddefself.test(len =10, show =false) a = randomArray(len)if(show) thenputs" In: "+ a.join(" ") end c = sort(a)if(show)puts" Out: "+ a.join(" ")puts"comp: #{c}"endendendShellSorter.test(20, true)