Python- Quicksort program in-place

2024/10/6 14:28:44

I have a quicksort program here, but there seems to be a problem with the result. I think there must have been some issue in the areas highlighted below when referencing some values. Any suggestions?

#where l represents low, h represents high
def quick(arr,l,h):#is this the correct array for quicksorting?if len(x[l:h]) > 1:#r is pivot POSITIONr = h#R is pivot ELEMENTR = arr[r]i = l-1for a in range(l,r+1):  if arr[a] <= arr[r]:i+=1arr[i], arr[a] = arr[a], arr[i]#should I take these values? Note that I have repeated elements below, which is what I want to deal withquick(arr,l,arr.index(R)-1)quick(arr,arr.index(R)+arr.count(R),h)x = [6,4,2,1,7,8,5,3]quick(x,0,len(x)-1)print(x)
Answer

Please check this. I think you find your answer.

def partition(array, begin, end):pivot = beginfor i in xrange(begin+1, end+1):if array[i] <= array[begin]:pivot += 1array[i], array[pivot] = array[pivot], array[i]array[pivot], array[begin] = array[begin], array[pivot]return pivotdef quicksort(array, begin=0, end=None):if end is None:end = len(array) - 1if begin >= end:returnpivot = partition(array, begin, end)quicksort(array, begin, pivot-1)quicksort(array, pivot+1, end)array = [6,4,2,1,7,8,5,3]
quicksort(array)
print (array)
https://en.xdnf.cn/q/118939.html

Related Q&A

Maximum recursion error in `__eq__` implementation

class Coordinate(object):def __init__(self,x,y):self.x = xself.y = ydef getX(self):# Getter method for a Coordinate objects x coordinate.# Getter methods are better practice than just accessing an attr…

python swig : ImportError wrong ELF class: ELFCLASS64

I am trying to interface from python to c++ code via swig. I get the following error , while trying to execute my script.File "./driver.py", line 4, in <module>from fixMessageSim import…

cnn news webscraper return empty [] without information

so i wrote this code for now: from urllib import request from bs4 import BeautifulSoup import requests import csv import reserch_term = input(What News are you looking for today? )url = fhttps://editi…

Why the code shows all the addition process?

Code: sum=0 for i in range(10,91):sum=sum+iprint(sum)When I wrote this code, the answer was Output: 10 21 33 46 60 75 91 108 126 145 165 186 208 231 255 280 306 333 361 390 420 451 483 516 550 585 621 …

Creating a list of keywords by scrolling through a dataframe (python)

I have a dataframe that looks like this: dataFrame = pd.DataFrame({Name: ((" Verbundmrtel , Compound Mortar , Malta per stucchi e per incollaggio "),(" StoLevell In Absolute , StoLeve…

How to click this button with python selenium

Im looking to click the button highlighted in the screenshot below; have tried with pyautogui but found results to be inconsistent so trying selenium instead.Im having trouble identifying the button to…

How to find the average of numbers being input, with 0 breaking the loop?

I just need to figure out how to find the average of all these input numbers by the user while using 0 as a exit of the loop. I need to figure out how to eliminate using 0 as part of the average. examp…

NoSuchElementException when loading code using Selenium on Heroku

Error: ERROR:asyncio:Task exception was never retrieved2022-03-14T14:08:52.425684+00:00 app[worker.1]: future: <Task finished name=Task-30 coro=<Dispatcher._process_polling_updates() done, define…

Python alphanumeric

Problem:I have to go through text file that has lines of strings and determine about each line if it is alphanumeric or not. If the line is alphanumeric print for example "5345m34534l is alphanume…

Python 3 - exec() Vs eval() - Expression evaluation [duplicate]

This question already has answers here:Whats the difference between eval, exec, and compile?(3 answers)Closed 7 years ago.After reading query.below python code is still not clear,>>> exec(pri…