SergOmarov

qsort

Jun 21st, 2015
231
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local arr={1,3,6,2,7,4,8,9,5}
  2. function minToMax(a,b)
  3. return a>b
  4. end
  5.  
  6. function partition (array, startI, endI,f)
  7. local marker = startI;
  8. for i = startI,endI do
  9.  
  10. if ( f(array[i],array[endI]) ) then
  11.  
  12. local temp = array[marker];
  13. array[marker] = array[i];
  14. array[i] = temp;
  15. marker =marker+ 1;
  16. end
  17. end
  18. return marker - 1,array
  19. end
  20.  
  21. function quicksort(array, start, endI, f)
  22. local pivot,arr1 = partition(array, start, endI,f);
  23. arr1=quicksort(arr1, start, pivot-1,f);
  24. return quicksort(arr1, pivot+1, endI,f);
  25. end
  26.  
  27. arr=quicksort(arr,1,#arr,minToMax)
RAW Paste Data