Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

quick sort python

def quick_sort(array):
    if len(array) < 2:
        return array
    else:
        pivot = array[0]
        less = [i for i in array[1:] if i <= pivot]
        greater = [i for i in array[1:] if i > pivot]
        return quick_sort(less) + [pivot] + quick_sort(greater)


arr = [7, 2, 4, 0, 1, 5, 8, 3, 2, 0]
print(quick_sort(arr)) # [0, 0, 1, 2, 2, 3, 4, 5, 7, 8]
Comment

quick sort python

def quick_sort(array):
    sort(array, 0, len(array) - 1)


def sort(array, left, right):
    if left >= right:
        return array
    pivot = array[(left + right) // 2]
    index = partition(array, left, right, pivot)

    sort(array, left, index - 1)
    sort(array, index, right)


def partition(array, left, right, pivot):
    while left <= right:
        while array[left] < pivot:
            left += 1
        while array[right] > pivot:
            right -= 1

        if left <= right:
            array[left], array[right] = array[right], array[left]
            left += 1
            right -= 1

    return left


arr = [7, 2, 4, 0, 1, 5, 8, 3, 2, 0]
quick_sort(arr)
print(arr) # [0, 0, 1, 2, 2, 3, 4, 5, 7, 8]
Comment

python with quick sort

[12, 19, 21, 27, 28, 29, 31, 41, 44, 44, 58, 66, 76, 78, 83, 87, 88, 97, 99]
Comment

quicksort function in python

#Code With Redoy: https://www.facebook.com/codewithredoy/
#My python lectures: https://cutt.ly/python-full-playlist

def quick_sort(array):
    if len(array) < 2:
        return array
    else:
        #select pivot element
        pivot = array[0]
        #partitioning the main array into less and greater
        less = [i for i in array[1:] if i <= pivot]
        greater = [i for i in array[1:] if i > pivot]
        
        #calling recursively 
        return quick_sort(less) + [pivot] + quick_sort(greater)

array = [9,8,7,6,5,4,3,2,1]
res = quick_sort(array)
print(res)
Comment

python with quick sort

array = [29,99,27,41,66,28,44,78,87,19,31,76,58,88,83,97,12,21,44]

quick_sort(array, 0, len(array) - 1)
print(array)
Comment

python quick sort

/* low  --> Starting index,  high  --> Ending index */
quickSort(arr[], low, high)
{
    if (low < high)
    {
        /* pi is partitioning index, arr[p] is now
           at right place */
        pi = partition(arr, low, high);

        quickSort(arr, low, pi - 1);  // Before pi
        quickSort(arr, pi + 1, high); // After pi
    }
}
Python3
Comment

quicksort python3


 
def partition(l, r, nums):
    # Last element will be the pivot and the first element the pointer
    pivot, ptr = nums[r], l
    for i in range(l, r):
        if nums[i] <= pivot:
            # Swapping values smaller than the pivot to the front
            nums[i], nums[ptr] = nums[ptr], nums[i]
            ptr += 1
    # Finally swapping the last element with the pointer indexed number
    nums[ptr], nums[r] = nums[r], nums[ptr]
    return ptr
 
# With quicksort() function, we will be utilizing the above code to obtain the pointer
# at which the left values are all smaller than the number at pointer index and vice versa
# for the right values.
 
 
def quicksort(l, r, nums):
    if len(nums) == 1:  # Terminating Condition for recursion. VERY IMPORTANT!
        return nums
    if l < r:
        pi = partition(l, r, nums)
        quicksort(l, pi-1, nums)  # Recursively sorting the left values
        quicksort(pi+1, r, nums)  # Recursively sorting the right values
    return nums
Comment

quicksort python

"""Basic Quicksort
"""
from random import randint
def qs_basic(nums, low=0, high=None):
    if high is None:
        high = len(nums) - 1
    # Sorting
    if low < high:
        pivot = nums[randint(low, high)]
        # i finds greater than pivot, j finds smaller than pivot
        i, j = low, high
        
        # Sort the pivot
        while i <= j:
            while nums[i] < pivot:
                i += 1
            while nums[j] > pivot:
                j -= 1
                
            # Pivot still not sorted
            if i <= j:
                nums[i], nums[j] = nums[j], nums[i]
                i += 1
                j -= 1
                
        # Recursive Partition
        qs_basic(nums, low, j)
        qs_basic(nums, i, high)
    return nums # for easy testing
Comment

PREVIOUS NEXT
Code Example
Python :: python post request multi argument 
Python :: Encrypting a message in Python 
Python :: Python NumPy insert Function Example Working with arrays 
Python :: Print statement with multiple variables 
Python :: 3d array 
Python :: tree in python 
Python :: python how to create a function 
Python :: hexdigest python 
Python :: double underscore methods python 
Python :: Implement a binary search of a sorted array of integers Using pseudo-code. 
Python :: pandas get row if difference previous 
Python :: python count the vowels 
Python :: time a function python 
Python :: python math packege power e 
Python :: How to find the maximum subarray sum in python? 
Python :: python list remove() 
Python :: create a colun in pandas using groupby 
Python :: convert time python 
Python :: python - input: integer 
Python :: how to run python on ios 
Python :: slack notification pytthon 
Python :: how to define a dictionary in python 
Python :: how to earn money as a python developer 
Python :: how to add elements in a list together python 
Python :: variable python 
Python :: multivaluedictkeyerror django 
Python :: drop columns 
Python :: login views django template passing 
Python :: python unbound variable 
Python :: python if greater than and less than 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =