Answers for "python quicksort"

11

Quick Sort python

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

def quick_sort_recursion(array, begin, end):
    if begin >= end:
        return
    pivot_idx = partition(array, begin, end)
    quick_sort_recursion(array, begin, pivot_idx-1)
    quick_sort_recursion(array, pivot_idx+1, end)

def quick_sort(array, begin=0, end=None):
    if end is None:
        end = len(array) - 1
    
    return quick_sort_recursion(array, begin, end)
Posted by: Guest on January-07-2020
1

quicksort code in python

#this function is for getting correct position of pivot
def pivot_place(list1,first,last):
    pivot = list1[first]
    left = first + 1
    right = last

    while True :

        while left <= right and  list1[left] <= pivot :
            left += 1
        while left<=right and list1[right] >= pivot:
            right -= 1

        if right < left :
            break
        else:
            list1[left],list1[right] = list1[right],list1[left]
    list1[first],list1[right] = list1[right],list1[first]

    return right
def quicksort(list1,first,last):
    if first < last :
        p = pivot_place(list1,first,last)
        quicksort(list1,first,p-1)
        quicksort(list1,p+1,last)
n = int(input("How many elements you want : "))
list1 = [int(input("Enter elements : ")) for x in range(n)]
quicksort(list1,0,n - 1)
print(list1)
Posted by: Guest on March-12-2021
2

quicksort in python

def partition(arr, low, high):
    i = (low-1)         
    pivot = arr[high]   
 
	for j in range(low, high):
        if arr[j] <= pivot:
 
            i = i+1
            arr[i], arr[j] = arr[j], arr[i]
 
    arr[i+1], arr[high] = arr[high], arr[i+1]
    return (i+1)
 
def quickSort(arr, low, high):
    if len(arr) == 1:
        return arr
    if low < high:
        pi = partition(arr, low, high)
 
		quickSort(arr, low, pi-1)
        quickSort(arr, pi+1, high)
 
arr = [10, 7, 8, 9, 1, 5]
n = len(arr)
quickSort(arr, 0, n-1)
print("Sorted array is:")
for i in range(n):
    print("%d" % arr[i]),
Posted by: Guest on November-18-2020
1

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)
Posted by: Guest on May-26-2021
0

python with quick sort

[12, 19, 21, 27, 28, 29, 31, 41, 44, 44, 58, 66, 76, 78, 83, 87, 88, 97, 99]
Posted by: Guest on May-26-2021
0

Quick Sort Algorithm in Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

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
Posted by: Guest on August-22-2021

Python Answers by Framework

Browse Popular Code Answers by Language