Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7995399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T14:24:18+00:00 2026-06-04T14:24:18+00:00

I’m trying to program a quicksort-ish algorithm using the Dutch national flag algorithm .

  • 0

I’m trying to program a quicksort-ish algorithm using the Dutch national flag algorithm. I’ve tried everything I can to make this work and I’m getting really frustrated. Can you take a look and help me find the bugs?

import random

a = []
for i in range(100):
    a.append(random.randint(1, 100))

print(a)
def partion(array, left, right, lPiv, rPiv):
        high = len(array) -1
        p = left
        i = left
        while i < high:
                if array[i] < lPiv and array[i] < rPiv:
                        array[i],array[p]=array[p],array[i]
                        p = p+1
                        i = i+1
                elif array[i] > lPiv and array[i] > rPiv:
                        array[i],array[high]=array[high],array[i]
                        high = high-1
                else:
                        i = i+1

        return [p, high]

def piv(array, left, right):
    aMin = array[left]
    aMax = array[left]

    for i in array:
      if i < aMin:
          aMin = i
      if i > aMax:
          aMax = i
    return [aMin + ((aMax - aMin) /3), aMin + ((aMax-aMin)/3)*2]


def sort(array, left, right, depth):
    apiv = piv(array, left, right)
    part = partion(array, left, right, apiv[0], apiv[1])
    if right-left >= 3:
        piv1 = piv(array, left, part[0])
        part1 = partion(array, left, part[0], piv1[0], piv1[1])
        sort(array, left, part1[0], depth+1)


        piv2 = piv(array, part[0], part[1])
        part2 = partion(array, part[0], part[1], piv2[0], piv2[1])
        sort(array, part[0], part[1])

        piv3 = piv(array, part[1], right)
        part3 = partion(array, part[1], right, piv3[0], piv3[1] )
        sort(array, part[1], right)

    elif right-left < 3:
        if array[right] < array[left]:
            array[right],array[left] = array[left], array[right]
        else:
            return


sort(a, 0, len(a), 1)
print(a)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T14:24:19+00:00Added an answer on June 4, 2026 at 2:24 pm

    A few bugs in your codes

    1. sort() takes 4 parameters, so sort(array, part[0], part[1]) fails. btw depth is not necessary.
    2. if array[right] < array[left]: and sort(a, 0, len(a), 1) .
      Right is out of array’s index range when it is the length of the array.
    3. piv should deal with the partion not the whole array
    4. if numbers in the partion are all same, aMin + ((aMax - aMin) /3) and aMin + ((aMax-aMin)/3)*2 are the same index
    5. since partition only deals with a partion not thw whole array, high = len(array) -1 should be high = right -1
    6. part1 = partion(array, left, part[0], piv1[0], piv1[1]) is wrong. Sort() partitions the array at it’s beginning, don’t partition twice in the same recursion. just call sort(array,…).
    7. your partition partitionalgorithm is wrong and I can’t find a simple fix for it

    Here’s my implementation.

    import random
    from itertools import islice
    a = []
    for i in range(100):
        a.append(random.randint(1, 100))
    
    def partition(array, left, right, lPiv, rPiv):
            q = right -1
            p = left
            r = left
            while p<=q:
                if array[p]<=lPiv:
                    array[p], array[r] = array[r], array[p]
                    p = p+1
                    r = r+1
                elif lPiv<array[p]<=rPiv:
                    p = p+1
                else:
                    array[p], array[q] = array[q], array[p]
                    q=q-1
    
            return (p, q+1)
    
    def piv(array, left, right):
        aMin = min(islice(a,left,right))
        aMax = max(islice(a,left,right))
        return (aMin + ((aMax - aMin) /3.0), aMin + ((aMax-aMin)/3.0)*2)
    
    
    def sort(array, left, right):
        if right-left >= 3:
            piv_left, piv_right = piv(array, left, right)
            if piv_left == piv_right:
                return
            pt_left, pt_right = partition(array, left, right, piv_left, piv_right)
            sort(array, left, pt_left)
            sort(array, pt_left, pt_right)
            sort(array, pt_right, right)
        elif right-left <3:
            if left<right and array[right-1] < array[left]:
                array[right-1],array[left] = array[left], array[right-1]
    
    
    sort(a, 0, len(a))
    print a
    
    #test
    assert( a==sorted(a) )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.