Skip to main content

Quick Sort

Quick Sort is an algorithm for sorting which is based on divide and conquer approach. An element is chosen as pivot and partitions the given list. The elements are swapped, based on comparison with pivot value, in order to obtain a sorted list. The partitioned sublists are recursively called to sort the elements in list. The selection of pivot can decide the efficiency of the quick sort.

Illustration:

Algorithm:

partition(list,lowestIndex,highestIndex)
    pointerI=lowestIndex-1
    pivot=Element at highestIndex
    for pointerJ from lowestIndex to HighestIndex
        if ElementAtpointerJ < pivot
            pointerI=pointerI+1
            swap ElementAtpointerI with ElementAtpointerJ
    swap Element at pointerI+1 with pivot
    return i+1

quickSort(list,lowestIndex,highestIndex)
    if lowestIndex < highestIndex
        pivotIndex=partition(list,lowestIndex,highestIndex)    
        recursiveCall on left part, quickSort(list,lowestIndex,pivotIndex-1)
        recursiveCall on right part, quickSort(list,pivotIndex+1,highestIndex)

Time Complexity:

  • Worst Case: O(n2)
  • Average Case: O(nlogn)
  • Best Case: O(nlogn)

Space Complexity: O(logn)

Program for Quick Sort:

Output:

Explanation:


Popular posts from this blog

Image to Pencil Sketch using Python and OpenCV

In this post, we will go through a program to get a pencil sketch from an image using python and OpenCV.  Step 1:  To use OpenCV, import the library. Step 2: Read the Image. Step 3: Create a new image by converting the original image to grayscale. Step 4: Invert the grayscale image. We can invert images simply by subtracting from 255, as grayscale images are 8 bit images or have a maximum of 256 tones. Step 5: Blur the inverted image using GaussianBlur  method in OpenCV library and invert the blurred image.  Step 6: Divide the grayscale values of the image by the values of image received from step-5 ( Note: We inverted the grayscale image and we blurred this image and then again inverted it ). Diving an image from its smoothened form will highlight the edges and we get the image like Pencil Sketch. Steps Illustration: Code: Execution Output: