Does this code use recursion?(quicksort)

user3037568

im not that familiar with the concept of recursion, and im not sure if these codes have recursion in them or use recursion.

def quickSort(alist):
    quickSortHelper(alist,0,len(alist)-1)

def quickSortHelper(alist,first,last):
    if first<last:

        splitpoint = partition(alist,first,last)

        quickSortHelper(alist,first,splitpoint-1)
        quickSortHelper(alist,splitpoint+1,last)


def partition(alist,first,last):
    pivotvalue = alist[first]

    leftmark = first+1
    rightmark = last

    done = False
    while not done:

        while leftmark <= rightmark and \
              alist[leftmark] <= pivotvalue:
            leftmark = leftmark + 1

        while alist[rightmark] >= pivotvalue and \
              rightmark >= leftmark:
            rightmark = rightmark -1

        if rightmark < leftmark:
            done = True
        else:
            temp = alist[leftmark]
            alist[leftmark] = alist[rightmark]
            alist[rightmark] = temp

    temp = alist[first]
    alist[first] = alist[rightmark]
    alist[rightmark] = temp


    return rightmark

if it's included, pointing out where it is would be extremely helpful.

user2555451

The definition of a recursive function is one that calls itself within itself.

Furthermore, the only function that does this in your code is the second one:

def quickSortHelper(alist,first,last):
    if first<last:

        splitpoint = partition(alist,first,last)

        quickSortHelper(alist,first,splitpoint-1)  # Calls itself here
        quickSortHelper(alist,splitpoint+1,last)   # and here

So, the second function is recursive; the other two are not.


Note: As @Sean said in the comment below, the definition I gave for recursion is a very simplified one. There are other code structures (such as two or more methods alternately calling one another) that can be classified as using recursion. For a complete overview of recursion, please see the link I gave above.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Does Sonar use Jacoco plugin for the code coverage

Does Shapeless use reflection and is it safe to use in scala production code?

Why does WebGL use string representations of GLSL code blocks?

What does double splat (**) argument mean in this code example and why use it?

Does it make sense to use MetadataType to enforce validations in case of Code First?

Does karate use interpreter or complier to run the feature file based code?

What code does TelegramClient use to send stickers to groups?

Why does this code return different results when use different programs?

How does the use of `static` affect the speed of my code?

Does this code use the Proxy or Singleton pattern, or both of them?

I use this Javascript code, it does the job, but I feel it could be shorter

Why does hotspot use different assembly styles in same source code?

Does eclipse use Java Instrumentation API for Hot Code replace

Why does `Int32` use `int` in its source code?

Why does Python code use len() function instead of a length method?

Why does compiled Angular code use closures instead of classes?

how does code determine to use WebSphereRequestUpgradeStrategy vs TomcatRequestUpgradeStrategy?

Why does this code use .replaceWith instead of just .html

Why does Go use its own Code generator?

Java crypto comparisons: Does SunJCE use native code?

Why does Windows use ANSI Code page instead of UNICODE?

Why does use of # is not commenting instead getting no. of elements in the following code?

Why does LindkedIn use hidden <code> tags in their (updated) website?

where does app.use points to in my code?

Does toLocaleString use the users timezone or the computer that hosts the code?

What JSON formatter does Visual Studio Code use to format JSON?

Does running a code multiple time use a lot of memory?

Why does SPIR-V use a "4 byte" code, or "Word code"?

Why does Node.js' Assert.js use !!!value in their code? What purpose does it serve?