Created
July 17, 2020 22:33
-
-
Save EricGustin/9f1dd9cd9d7c3529ddea96363d5f6122 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class SortingAlgorithms: | |
| def quickSort(self, leftIndex, rightIndex, lst) -> int: | |
| def partition(leftIndex, rightIndex, lst): | |
| pivotElement = lst[(leftIndex + rightIndex) // 2] | |
| while leftIndex <= rightIndex: | |
| # Find element on the left of the partition that is greater than the partitionElement | |
| while lst[leftIndex] < pivotElement: | |
| leftIndex += 1 | |
| # Find element on the right of the partition that is less than the partitionElement | |
| while lst[rightIndex] > pivotElement: | |
| rightIndex -= 1 | |
| # Perform swap and adjust leftIndex and rightIndex accordingly | |
| if leftIndex <= rightIndex: | |
| tmp = lst[leftIndex] | |
| lst[leftIndex] = lst[rightIndex] | |
| lst[rightIndex] = tmp | |
| leftIndex += 1 | |
| rightIndex -= 1 | |
| return leftIndex | |
| partitionIndex = partition(leftIndex, rightIndex, lst) | |
| if leftIndex < partitionIndex - 1: | |
| self.quickSort(leftIndex, partitionIndex-1, lst) | |
| if rightIndex > partitionIndex: | |
| self.quickSort(partitionIndex, rightIndex, lst) | |
| # Driver Code | |
| obj = SortingAlgorithms() | |
| quickList = [i for i in range(99, 0, -1)] | |
| obj.quickSort(0, len(quickList)-1, quickList) | |
| print(quickList) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment