Skip to main content

All you need to know about Quick Sort Algorithm.

Introduction to Quick Sort Algorithm.

A sorting algorithm basically sorts the data structure like array or list in certain order. The order either may be the numerical order or alphabetical order.
A Quick-Sort Algorithm is one of the fastest sorting algorithms, which sorts the array based on partitioning the array by choosing an element of the same array as a pivot. All element greater than the pivot are moved to the right direction and all element smaller than the pivot are moved to left.

The basic idea of this algorithm is to select a pivot element in an array and to put the pivot element in its right place and move all smaller entries than pivot of the array to left, and greater entries than pivot to right of the array. 
Now the array is divided into two part one part of greater entries than the pivot and another part of the smaller entries than the current pivot element.
Now the two-part are recursively applied quicksort to left part and to the right part. Recursion basically refers to a function having its own application within its definition.

Things to consider while applying Quick Sort

Pivot element should be chosen carefully

The pivot element is responsible for the efficiency of this algorithm, depending upon pivot element the sorting operation could be really fast or slow
Common practices for choosing pivot element are:
  • Choosing the fixed element of the given array. the fixed element may be the array of index 0 or index n-1, i.e the first and last or middle.
    • This may be the bad choice because it may result in either of the partitions to be empty if the selected element of that array turned out to be largest or smallest.
  • Choosing randomly by random generator.
  • Choosing by taking the median element as pivot, i.e (N/2) element if the array is of size N.and it results in computational complexity.
  • Taking the first, middle and the last element and choosing the medium element from the selection for Pivot element.

Complexity of Quick Sort algorithm

In Worst case, time complexity will be  O(N2) , i.e, it occurs when the pivot element, chosen was smallest or largest resulting one partition to be empty,
In the best case, time complexity will be O(nlogn), where n is the number of items and it occurs when the pivot is the median of the array which results in the equal partition on left and right.

Python Implementation of quick sort.


def quick_sort(given_list):
if len(given_list) < 1:
return given_list
else:
pivot_element = given_list[0] #here I am choosing the first element to be a pivot
left = quick_sort([element for element in given_list[1:] if element < pivot_element]) # moving smaller to left
right = quick_sort([element for element in given_list[1:] if element > pivot_element]) #moving greater to right
return left + [pivot_element] + right
#usage
mylist=[8,5,9,4,3,7,2,12,10]
print (quick_sort(mylist))
#Outputs [2, 3, 4, 5, 7, 8, 9, 10, 12]


link to code

DISCLAIMER: There might be the best explanation to sorting algorithm than this. This was written as learning purpose by a dummy , to the dummies :D

Comments

Popular posts from this blog

Big Oh notation and Algorithmic Complexity analysis

Introduction When executing a particular task using algorithms, one common thing that hits every problem solver brain is the efficient and fast algorithm to solve that problem. But, what do exactly fast and efficient means? Is it the measure of real processing time? No, it's not the measure of real time like second and minutes because the old computer with Pentium processor may take a long time to process the same algorithm than the new Intel i3 processor, or, A bad algorithm written in Assembly may execute faster than a good algorithm written in python. So, run time of program can't be considered to measure algorithmic efficiency.  Hence, to measure the algorithmic efficiency, the concept of Asymptotic Complexity of a program and a notation for describing this called Big Oh  was introduced. Big Oh is the worst case, Big Omega and Big Theta are best and average case notations. Worst case means we are mostly unlucky for that problem domain , i.e precondition of task do

Going Through MNIST(Mixed National Institute of Standards and Technology) Datasets in TensorFlow

MNIST Talking About MNIST(Mixed National Institute of Standards and Technology), It is the larger database of the handwritten digit which is used to train and test various Machine learning and Image processing task. [MNIST Wikipedia entry] As a beginner to Machine learning and equally beginner to TensorFlow, I have decided to go through the first tutorial entitled   MNIST For ML Beginners  (Mixed National Institute of Standards and Technology) [In Tensorflow tutorials,  make sure to use the your version of mnist_softmax.py by selecting version in branch section, Do not use master branch because the tutorial will be of different tensorflow version ] for tensorflow 0.11 mnist_softmax.py file ] I started my development environment on Pycharm IDE with interpreter location at virtual environment tensorflow which I created previously during Installation of TensorFlow.  Going through Tutorials The data hosted on yann.lecun.com  is automatically downloaded and read. This is all

Getting Started With Tensorflow

TensorFlow Introduction It is an open source library for machine learning. It was developed at Google by Google brain team. For more info regarding Tensorflow check the Wikipedia entry . Getting Started with Installation Referring to GitHub  link  and choosing the virtual environment Installation Procedure. Following this link  here  for installation procedure of Tensorflow library  CPU version. We can choose any version listed along with CPU/GPU version. After activating the python virtual environment as listed on installation manual console of our environment will be changed like this as in the picture below. Console window changed with (tensorflow) at beginning.  After this steps below, export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.11.0-cp27-none-linux_x86_64.whl pip install --upgrade $TF_BINARY_URL Faced a problem during installation with this message in console. tensorflow-0.11.0-cp27-none-linux_x86_64.whl is no