title: Quicksort not working with little larger array size
body: <p>Below is my quicksort code in ruby, and its working fine for array size like 20-25 but getting either stack level too deep error or its getting stuck for longer time. </p>
  
  <p>I am guessing i am doing a trivial mistake but not able to figure out.</p>
  
  <pre><code>  # This program is to do sorting using Quick sort.
  require 'colorize'
  
  class QuickSort
   attr_accessor :array
    def initialize(size)
     puts "Generating Random numbers for your array".cyan
     @array = (1..size.to_i).map do 
      rand(500) # Generating random numbers between 1 to 500.
     end
    # Boundary case
     if @array.size == 1
      puts  "Your sorted array is"
      p @array
      return
     end
     puts "Array Before Sorting".yellow
     p @array
     @head = 0
     @tail = @array.size-1
     startSort(@array,@head,@tail) #Start the searching logic.
    end
  
   # Quicksort logic
   def startSort(array,head,tail)
    if head &lt; tail
     pivot = partitionArray(array,head,tail) # Calling the sorting logic  
     startSort(array,head,pivot-1)
     startSort(array,pivot+1,@tail)
    end 
   end
  
  
   # This method is called to partition the array based on pivot.
   def partitionArray(array,head,tail)
    pivot_value = array[(head+tail)/2] # Choosing random pivot value.
   # Run this partition step until head is equal to tail
    while head &lt;= tail
      if array[head] &lt; pivot_value
       head += 1
      elsif array[head] &gt;= pivot_value
       if array[tail] &gt; pivot_value
        tail -= 1
       elsif array[tail] &lt;= pivot_value
        # Swapping head and tail values
        temp = array[head]
        array[head] = array[tail]
        array[tail] = temp
        # Moving each pointer forward from both the directions.
        head += 1
        tail -= 1
       end     
      end     
     end
     return head # Nothing but pivot
   end
  end
  
  
   puts "Enter the size of Array"
   @size = gets.chomp
   # Checking if entry is a valid integer or not.
   if @size.match(/^(\d)+$/) 
    @obj = QuickSort.new(@size)
    puts "Array after sorting is ".green
    p @obj.array
   else
    puts "Invalid Entry".red
   end
  </code></pre>