GithubHelp home page GithubHelp logo

quicksort's Introduction

Quicksort

I implemented my own Quicksort algorithm in Ruby. Quicksort takes O(n * log n) time on average and O(n^2) in the worst case scenario. For space complexity, it takes O(n) space. I improved it slightly with an in-place partitioning algorithm that doesn't create any extra arrays. Space complexity is still O(n).

Partitioning

Instead of creating separate arrays for items smaller than the pivot and larger than the pivot, I partitioned each element. If an element was larger than the pivot, it would stay on the right hand side of the pivot. If an element was smaller than the pivot, it would move to the left hand side of the pivot. This was done via simple array index swapping:

  val = array[idx] #current element you're comparing against
  if ((pivot <=> val) < 1)
    #pivot is smaller than the value
  else
    #pivot is greater than the value

    array[idx] = array[pivot_idx + 1] #move the element in the immediate right of the pivot to the current index's place
    array[pivot_idx + 1] = pivot #move the pivot up one space.
    array[pivot_idx] = val  #move the smaller value to the immediate left of the pivot
    pivot_idx +=1
  end

Next, I recursively sorted the left hand side and the right hand side, which would give me my final array.

sort2!(array, start, left_length, &prc)  
sort2!(array, pivot_idx + 1, right_length, &prc)  

array

Future plans

To improve space complexity to O(log n) instead of O(n), I need to use tail recursion. I would recursively sort the smaller sized partition, which takes a max of O(log n) stack frames, and use tail recursion on the bigger sized partition, which does not add to the call stack.

quicksort's People

Contributors

bpsimusic avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.