Quicksort-based Sorting System

From GM-RKB
Jump to navigation Jump to search

A Quicksort-based Sorting System is a sorting system (that applies a Quicksort algorithm.



References

2017

function qsort!(a,lo,hi)
   i, j = lo, hi
   while i < hi
       pivot = a[(lo+hi)>>>1]
       while i <= j
           while a[i] < pivot; i = i+1; end
           while a[j] > pivot; j = j-1; end
           if i <= j
               a[i], a[j] = a[j], a[i]
               i, j = i+1, j-1
           end
       end
       if lo < j; qsort!(a,lo,j); end
       lo, j = i, hi
   end
   return a
end

2017

2017b

def quicksort(arr):
   if len(arr) <= 1:
       return arr
   pivot = arr[len(arr) // 2]
   left = [x for x in arr if x < pivot]
   middle = [x for x in arr if x == pivot]
   right = [x for x in arr if x > pivot]
   return quicksort(left) + middle + quicksort(right)
print quicksort([3,6,8,10,1,2,1])
[1, 1, 2, 3, 6, 8, 10]