From de6f4c8e1fdb141056fe9fa5bf80bbdeb143786e Mon Sep 17 00:00:00 2001 From: Sandali Samarawickrama Date: Tue, 2 Oct 2018 09:17:52 +0530 Subject: [PATCH] Add quicksort.py --- sorting/quick_sort.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 sorting/quick_sort.py diff --git a/sorting/quick_sort.py b/sorting/quick_sort.py new file mode 100644 index 0000000..cbb917c --- /dev/null +++ b/sorting/quick_sort.py @@ -0,0 +1,28 @@ +# This function takes last element as pivot, places +# the pivot element at its correct position in sorted +# array, and places all smaller (smaller than pivot) +# to left of pivot and all greater elements to right +# of pivot +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 low < high: + + pi = partition(arr,low,high) + + quickSort(arr, low, pi-1) + quickSort(arr, pi+1, high) + + + pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy