├── Quicksort algorithm.txt ├── Quicksort Complexity.txt └── Quicksort code.py /Quicksort algorithm.txt: -------------------------------------------------------------------------------- 1 | 2 | quickSort(array, leftmostIndex, rightmostIndex) 3 | if (leftmostIndex < rightmostIndex) 4 | pivotIndex <- partition(array,leftmostIndex, rightmostIndex) 5 | quickSort(array, leftmostIndex, pivotIndex - 1) 6 | quickSort(array, pivotIndex, rightmostIndex) 7 | 8 | partition(array, leftmostIndex, rightmostIndex) 9 | set rightmostIndex as pivotIndex 10 | storeIndex <- leftmostIndex - 1 11 | for i <- leftmostIndex + 1 to rightmostIndex 12 | if element[i] < pivotElement 13 | swap element[i] and element[storeIndex] 14 | storeIndex++ 15 | swap pivotElement and element[storeIndex+1] 16 | return storeIndex + 1 -------------------------------------------------------------------------------- /Quicksort Complexity.txt: -------------------------------------------------------------------------------- 1 | Quicksort Complexity 2 | Time Complexity 3 | Best O(n*log n) 4 | Worst O(n2) 5 | Average O(n*log n) 6 | 7 | 8 | 1. Time Complexities 9 | Worst Case Complexity [Big-O]: O(n2) 10 | 11 | It occurs when the pivot element picked is either the greatest or the smallest element. 12 | 13 | This condition leads to the case in which the pivot element lies in an extreme end of the sorted array. One sub-array is always empty and another sub-array contains n - 1 elements. Thus, quicksort is called only on this sub-array. 14 | 15 | However, the quicksort algorithm has better performance for scattered pivots. 16 | Best Case Complexity [Big-omega]: O(n*log n) 17 | It occurs when the pivot element is always the middle element or near to the middle element. 18 | Average Case Complexity [Big-theta]: O(n*log n) 19 | It occurs when the above conditions do not occur. -------------------------------------------------------------------------------- /Quicksort code.py: -------------------------------------------------------------------------------- 1 | 2 | # function to find the partition position 3 | def partition(array, low, high): 4 | 5 | # choose the rightmost element as pivot 6 | pivot = array[high] 7 | 8 | # pointer for greater element 9 | i = low - 1 10 | 11 | # traverse through all elements 12 | # compare each element with pivot 13 | for j in range(low, high): 14 | if array[j] <= pivot: 15 | # if element smaller than pivot is found 16 | # swap it with the greater element pointed by i 17 | i = i + 1 18 | 19 | # swapping element at i with element at j 20 | (array[i], array[j]) = (array[j], array[i]) 21 | 22 | # swap the pivot element with the greater element specified by i 23 | (array[i + 1], array[high]) = (array[high], array[i + 1]) 24 | 25 | # return the position from where partition is done 26 | return i + 1 27 | 28 | # function to perform quicksort 29 | def quickSort(array, low, high): 30 | if low < high: 31 | 32 | # find pivot element such that 33 | # element smaller than pivot are on the left 34 | # element greater than pivot are on the right 35 | pi = partition(array, low, high) 36 | 37 | # recursive call on the left of pivot 38 | quickSort(array, low, pi - 1) 39 | 40 | # recursive call on the right of pivot 41 | quickSort(array, pi + 1, high) 42 | 43 | 44 | data = [8, 7, 2, 1, 0, 9, 6] 45 | print("Unsorted Array") 46 | print(data) 47 | 48 | size = len(data) 49 | 50 | quickSort(data, 0, size - 1) 51 | 52 | print('Sorted Array in Ascending Order:') 53 | print(data) --------------------------------------------------------------------------------