Computer Science‎ > ‎

Basic Data Structures in Ruby - Quick Sort

Programming With Ruby

Quick Sort

The Quick Sort is a popular sorting algorithm, which has been discussed in detail, with examples and C code over here : Arrays and Sorting: Quick Sort ( with C Program source code). In this tutorial, we will take a quick look at the ruby implementation of Quick Sort.

def quickSort(arr,from,to)
    return if from >= to    
    pivot = arr[from]
    i = from
    for j in (1+from)
        if arr[j] < pivot
            i = i + 1
            temp = arr[i]
            arr[i] = arr[j]
            arr[j] = temp

puts "Sorted Array Using Quick Sort:"
quickSort(original_array,0,original_array.length - 1)
p original_array

Check out some of our other Ruby Tutorials :

Introduction to Ruby

 Introduction to Ruby and some playing around with the Interactive Ruby Shell (irb) Introduction to Ruby - Conditional statements and Modifiers: If-then, Unless, Case Introduction to Ruby Comments - Single and Multi-Line comments Introduction to Ruby Loops - Using While, Until, For, Break, Next , Redo, Retry
 Introduction to Ruby - Arrays - Sorting, Filtering (Select), Transforming, Multi-Dimensional Arrays Introduction to Ruby - Strings Introduction to Ruby - Making a Script Executable Introduction to Ruby - Regular Expressions, Match, Scan
 Introduction to Ruby - Computing Factorials Recursively : An Example of Recursion Introduction to Ruby - Binomial Coefficients (nCr) : An Example of Recursion Introduction to Ruby - Computing a Power Set : An Example of Recursion Introduction to Ruby - Towers of Hanoi : An Example of Recursion
 Introduction to Ruby - Strings: Substitution, Encoding, Built-In Methods 

Basic Data Structures With Ruby