Lab3003
  • FacultyAlgorithmChallenge
  • README
  • Question Bank
  • Python Documentation
    • BetterMap
    • Board
    • Card
    • Circle
    • Deck
    • README.md
    • Guide For Bookmarklet
    • Hand
    • HashMap
    • Hist
    • Kangaroo
    • LinearMap
    • Markov
    • Point
    • PokerDeck
    • PokerHand
    • Rectangle
    • State
    • Test
    • ThinkPython
    • ThinkPython2
    • Time
    • Think Python
    • thinkpython
  • Files
  • Image Files
  • lab
    • InsertSortTDD
    • countingBits
    • cs3003mergeSortLectureGuideLine
    • index_min
    • insort
    • One-line factorial function in Python
  • Manual for Python Lab
    • COURSE OBJECTIVES
    • 1_timelines
    • FDPNotes
    • PC-2_Python
    • lab1-kgashok.py
    • Lab 10 :Matrix Multiplication
    • Lab 11: Programs that take command line arguments (word count)
    • Lab 11: Programs that take command line arguments
    • Lab 12: Compute the most frequently used words from the text file
    • Lab 12: Find the most frequent words in a text read from a file
    • Lab 12a: File content sorter
    • Lab 2: Find the square root of a number (Newton’s method)
    • Lab 3: Compute power of a number (Exponentiation)
    • Lab 3: Exponentiation (power of a number)
    • lab4-binaraysearch-anvar.py
    • Solution for Binary Search
    • lab4-joelanandraj-binarysearch.py
    • Lab 4: Linear and Binary Search
    • lab4-lin-anvar.py
    • Linear Searcher
    • Lab 5 : find n prime numbers
    • lab6-kgashok.py
    • lab7-kgashok.py
    • lab8-kgashok.py
    • Lab 8: Selection and Insertion sort using Python
    • Merge Sort
    • Quick Sort
    • labSheet
    • pc0
    • sortPythonic
    • Sorting
  • misc
    • Bookmarklets
    • Guide For Bookmarklet
    • pythonResources
    • FDP for Python
    • Agenda for Workshop
      • Agenda
  • notes
    • Problem Set
    • InsertSortTDD
    • MergeSort
    • cs3003-unit1-notes
    • cs3003-unit3
    • cs3003-unit4-examples
    • Unit 4 Lists, Tuples and Dictionaries
    • cs3003insertsortLectureGuide
    • cs3003mergeSortLectureGuideLine
    • Designing and Delivering Effective Lectures
    • histogram.py
    • selectSortLectureGuide
  • Sandbox to try ThinkPy2 code
    • Code from ThinkPython Github repository
  • Important Topics
    • 3003-syllabus
    • GE8161 PROBLEM SOLVING AND PYTHON PROGRAMMING LABORATORY L T P C
    • Unit II Version 2
    • Unit IV material
    • UNIT III CONTROL FLOW, FUNCTIONS
    • Unit 1 Algorithm Problem Solving
    • Unit_V_Notes
    • UpdatedSyllabus
    • glossary
    • glossaryGeneration.py
    • importantTopics-kgashok
    • memCSV
    • Tower of Hanoi
    • Notes for Unit 2 - Illustrative programs
    • unit5-updated_notes
    • unit_1_notes
    • Unit 3 Control Flow Version 1
    • unit 3
      • UNIT III CONTROL FLOW, FUNCTIONS
    • unit_i_img
Powered by GitBook
On this page
  • Problem statement
  • Solution key
  • CloudCoder exercises
  • Pre-Lab Questions
  • Post-Lab Questions
  • Bonus
  • Interview Grade
  • Related Material
  • The Quicksort Dance
  • Compare Merge and Quick
  1. Manual for Python Lab

Quick Sort

Problem statement

Write a python program to sort the given list using quick sort algorithm.

Examples:

Sample Input0: [1, 2, 3, 4]
Sample Output0: [1, 2, 3, 4]

Sample Input1: [5, 12, 3, 21, 4]
Sample Output1: [3, 4, 5, 12, 21]

Sample Input2: [7.1, 9.2, 3.1]
Sample Output2: [3.1, 7.1, 9.2]

Solution key

def partition(lst, start, end, pivot):

  lst[pivot], lst[end] = lst[end], lst[pivot]
  store_index = start
  for i in range(start, end):
    if lst[i] < lst[end]:
     lst[i], lst[store_index] = lst[store_index], lst[i]
    store_index += 1
  lst[store_index], lst[end] = lst[end], lst[store_index]
  return store_index

def quick_sort(lst, start, end):
    if start >= end:
      return lst
    pivot = round((start+end)/2)
    new_pivot = partition(lst, start, end, pivot)
    quick_sort(lst, start, new_pivot - 1)
    quick_sort(lst, new_pivot + 1, end)

def sort(lst):
    if (len(lst)==0):
        return "invalid input"
    for i in lst:
        result=isinstance(i,str)
        if result==True:
            return "invalid input"
            break
    quick_sort(lst, 0, len(lst) - 1)
    #print(lst)
    return lst

CloudCoder exercises

[To be updated]

Pre-Lab Questions

  1. Quick sort uses ___________.

    • a. Divide and Conquer Technique

    • b. Greedy Approach

    • c. Back Tracking

    • d. None of the above

  2. How to select a pivot element?

  3. Given the following list of numbers [1, 20, 11, 5, 2, 9, 16, 14, 13, 19] what would be the first pivot value using the median method?

  4. Given the following list of numbers [14, 17, 13, 15, 19, 10, 3, 16, 9, 12] what is the contents of the list after the second partitioning according to the quicksort algorithm?

  5. What is the average case complexity for quick sort algorithm?

    • a. O(n)

    • b. O(n*n)

    • c. O(nlogn)

    • d. O(logn)

  6. What is the worst case complexity for quick sort algorithm?

    • a. O(n)

    • b. O(n*n)

    • c. O(nlogn)

    • d. O(logn)

Post-Lab Questions

  1. What are the advantages and disadvantages of quick sort?

  2. List out the applications of quick sort.

  3. If the list is empty, what will be the value returned from the function?

  4. If the list contain any string element, what will be the value returned from the function?

  5. Does the selection of pivot element help to optimize the quick sort algorithm performance?

Bonus

Interview Grade

Related Material

The Quicksort Dance

Compare Merge and Quick

http://j.mp/mergeVsQuick

PreviousMerge SortNextlabSheet

Last updated 3 years ago

CD External Link: CD Local Link:

http://cyberdojo1.kgfsl.com/kata/edit/8EE56C12AC?avatar=parrot
http://10.100.8.8/kata/edit/8EE56C12AC?avatar=parrot
http://j.mp/quickSortDance
https://www.quora.com/What-is-an-intuitive-explanation-of-QuickSort/answer/Arjun-Subramaniam?srid=ul6v
https://www.quora.com/What-is-the-best-explanation-of-the-QuickSort-partition-algorithm