Python- How to implement quick sort when middle element is the pivot?

2024/10/9 2:29:57

There are many different versions of quickSort that pick pivot in different ways.

  • Always pick the first element or the last element as the pivot
  • Pick a random element as a pivot.
  • Pick median as the pivot.

I have implemented using the last element as the pivot everything worked fine but when I tried to implement the same logic to the middle element, it's no working fine.

Here's my code in python:

import random
import time
start = time.time()def quickSort(a,l,r):if(l<r):p = partition(a,l,r)quickSort(a,l,p-1)quickSort(a,p+1,r)def partition(a,l,r):i = l-1p = a[(l+(r-l))//2]for j in range(l,r):if a[j] <= p:i += 1a[i],a[j] = a[j],a[i]a[i+1],a[r] = a[r],a[i+1]return (i+1)N = 10
a = [random.randint(1,100) for i in range(N)]
print(a)
quickSort(a,0,N-1)
print(a)
print("It took %s milli seconds"%((time.time()-start)*100))

This is the output

[88, 35, 55, 68, 96, 23, 44, 77, 78, 71]
[35, 55, 23, 68, 44, 77, 88, 96, 78, 71]
It took 1.5625953674316406 milli seconds
Answer

Using Hoare partition scheme is better suited to using the middle value as pivot, and Hoare partition scheme is typically faster than the Lomuto partition scheme used in the question.

def qsort(a, lo, hi):if(lo >= hi):returnp = a[(lo + hi) // 2]       # pivot, any a[] except a[hi]i = lo - 1j = hi + 1while(1):while(1):               # while(a[++i] < p)i += 1if(a[i] >= p):breakwhile(1):               # while(a[--j] < p)j -= 1if(a[j] <= p):breakif(i >= j):breaka[i],a[j] = a[j],a[i]qsort(a, lo, j)qsort(a, j+1, hi)

As commented above, if you still want to use Lomuto partition scheme, swap the middle element with the last element and use your code with the last element.

https://en.xdnf.cn/q/118639.html

Related Q&A

How to expose a form when a radio button is checked?

Consider the following sample html template,<html><body><input type="radio" name="x">x</input><br><input type="radio" name="y"&g…

How to generate perlin noise in pygame?

I am trying to make a survival game and I have a problem with perlin noise. My program gives me this:But I want something like islands or rivers. Heres my code: #SetUp# import pygame, sys, random pygam…

Inputting range of ports with nmap optparser

This is the scriptimport nmap import optparsedef nmapScan(tgtHost,tgtPort):nmScan = nmap.PortScanner()nmScan.scan(tgtHost,tgtPort)state=nmScan[tgtHost][tcp][int(tgtPort)][state]print "[*] " +…

Implementing a Python algorithm for solving the n-queens problem efficiently

I am working on a project that requires me to solve the n-queens problem efficiently using Python. I have already implemented a basic recursive algorithm to generate all possible solutions, but I am lo…

Annotations with pointplot

I am using a pointplot in seaborn.import seaborn as sns sns.set_style("darkgrid") tips = sns.load_dataset("tips") ax = sns.pointplot(x="time", y="total_bill", hu…

How do I get data in tuples and tuples in lists?

I am trying to figure out the route that a car takes in a fictional manhattan. I have defined the starting position, being(1,2)(in a 2 dimensional grid).manhattan=[[1, 2, 3, 4, 5],[6, 7, 8, 9, 10],[11…

How to return a value from a table or dataframe given 2 inputs? Python

Lets say this is my dataframe:and the user inputs B and 2Then the function would return clemintineIs there a way to do this without using a bunch of if elif statements. The actual dataframe Im working …

tkinter progressbar for multiprocessing

I have a program that encrypts files and I used multiprocessing to make it faster, but I am having trouble with the tkinter progress bar. I have implemented it but it completes immediately or lags in b…

How to add and subtract in python

So I am making a statcalc and everything is working except adding. When I select the option to add it just skips it and says select an option. I was wondering whats wrong with it?numberstoadd = input(…

Python: deferToThread XMLRPC Server - Twisted - Cherrypy?

This question is related to others I have asked on here, mainly regarding sorting huge sets of data in memory.Basically this is what I want / have:Twisted XMLRPC server running. This server keeps seve…