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

py quick sort

def partition(array, begin, end):
    pivot = begin
    for i in range(begin+1, end+1):
        if array[i] <= array[begin]:
            pivot += 1
            array[i], array[pivot] = array[pivot], array[i]
    array[pivot], array[begin] = array[begin], array[pivot]
    return pivot


def quicksort(array, begin=0, end=None):
    if end is None:
        end = len(array) - 1
    def _quicksort(array, begin, end):
        if begin >= end:
            return
        pivot = partition(array, begin, end)
        _quicksort(array, begin, pivot-1)
        _quicksort(array, pivot+1, end)
    return _quicksort(array, begin, end)
Comment

quicksort algorithm 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 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 :: prime number using python 
Python :: Using mapping in Converting categorical feature in to numerical features 
Python :: python check samplerate of mp3 
Python :: selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: 
Python :: load pt file 
Python :: hashlib sha 256 
Python :: numpy random for string 
Python :: circumference of a circle python 
Python :: Python program to combine each line from first file with the corresponding line in second file 
Python :: python try except continue loop 
Python :: get dictionary values python 
Python :: normalize numpy array 
Python :: Display the data types of the DataFrame 
Python :: python script that executes at time 
Python :: python3 lowercase 
Python :: traversing a tree in python 
Python :: insert single value in dataframe using index 
Python :: selenium chrome options suppress warnings python 
Python :: how to get circumference from radius 
Python :: Week of the year Pandas 
Python :: looping on string with python 
Python :: pytplot arc 
Python :: how to reset username and password in django admin 
Python :: python private method 
Python :: load image metadata with pil 
Python :: create the dataframe column based on condition 
Python :: list element swapping python 
Python :: post request socket python 
Python :: calculate term frequency python 
Python :: python two string equal 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =