Guest User

Untitled

a guest
Feb 19th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. -(void) swapFirst:(int)first withSecond:(int)second inArray:(NSMutableArray *)array
  2. {
  3. NSNumber *temp = array[first];
  4. array[first] = array[second];
  5. array[second] = temp;
  6. return;
  7. }
  8.  
  9. -(int) findKthLargestNumber:(int)k
  10. inArray:(NSMutableArray *)array
  11. begin:(int)begin
  12. end:(int)end
  13. {
  14. int pivot = (begin + end) / 2;
  15. [self swapFirst:pivot withSecond:end inArray:array];
  16. int pivotPosition = begin;
  17. for (int i = begin; i < end; ++i) {
  18. if ([array[i] intValue] < [array[end] intValue]) {
  19. [self swapFirst:i withSecond:pivotPosition inArray:array];
  20. pivotPosition++;
  21. }
  22. }
  23. [self swapFirst:pivotPosition withSecond:end inArray:array];
  24. if (pivotPosition == k - 1) {
  25. return [array[pivotPosition] intValue];
  26. } else if (pivotPosition < k - 1) {
  27. return [self findKthLargestNumber:k inArray:array begin:pivotPosition + 1 end:end];
  28. } else {
  29. return [self findKthLargestNumber:k inArray:array begin:begin end:pivotPosition - 1];
  30. }
  31. }
Add Comment
Please, Sign In to add comment