L0neGamer

Old quickSort vs new one

Nov 11th, 2015
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  1. def quickSort(listToSort):
  2.     """Sorts listToSort using the QuickSort algorithm - technically broken"""
  3.     pivot = listToSort[len(listToSort)//2]
  4.     lowerList = []
  5.     higherList = []
  6.    
  7.     for i in range(len(listToSort[1:])):
  8.         if listToSort[i] <= pivot:
  9.             lowerList.append(listToSort[i])
  10.         elif listToSort[i] > pivot:
  11.             higherList.append(listToSort[i])
  12.  
  13.     #if all values equal each other, make checkValues false
  14.     checkValuesLower = not all(x == lowerList[0] for x in lowerList)
  15.     checkValuesHigher = not all(x == higherList[0] for x in higherList)
  16.    
  17.     if len(lowerList) > 1 and checkValuesLower:
  18.         sortedLowerList = quickSort(lowerList)
  19.     else:
  20.         sortedLowerList = lowerList
  21.    
  22.     if len(higherList) > 1 and checkValuesHigher:
  23.         sortedHigherList = quickSort(higherList)
  24.     else:
  25.         sortedHigherList = higherList
  26.  
  27.     return(sortedLowerList+[pivot]+sortedHigherList)
  28.  
  29. def quickSort2(listToSort):
  30.     pivot = listToSort[0]
  31.     lowerList = []
  32.     higherList = []
  33.  
  34.     for i in listToSort[1:]:
  35.         if i > pivot:
  36.             higherList.append(i)
  37.         elif i <= pivot:
  38.             lowerList.append(i)
  39.            
  40.     checkValuesLower = not all(x == lowerList[0] for x in lowerList)
  41.     checkValuesHigher = not all(x == higherList[0] for x in higherList)  
  42.  
  43.     if len(lowerList) > 1 and checkValuesLower:
  44.         lowerList = quickSort2(lowerList)
  45.     if len(higherList) > 1 and checkValuesHigher:
  46.         higherList = quickSort2(higherList)
  47.  
  48.     return(lowerList+[pivot]+higherList)
Advertisement
Add Comment
Please, Sign In to add comment