class QuickSort

Public Class Methods

run(data) click to toggle source
# File lib/compare-sort.rb, line 227
def self.run(data)
        return data if data.length <= 1

        return self.sort_section(data, 0, data.length-1)
end
sort_section(data, start_loc, end_loc) click to toggle source
# File lib/compare-sort.rb, line 233
def self.sort_section(data, start_loc, end_loc)
        return data if end_loc - start_loc <1
        wall = start_loc
        pivot = data[end_loc]


        for i in start_loc..end_loc #valid indicies
                if data[i]<pivot
                        smaller_datum = data[i]
                        data.delete_at(i)
                        data.insert(wall, smaller_datum)
                        wall += 1
                end 
        end 
                data.insert(wall, pivot)
                data.delete_at(end_loc + 1)

        self.sort_section(data, start_loc, wall-1)
        self.sort_section(data, wall+1, end_loc)
end