Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -(void) swapFirst:(int)first withSecond:(int)second inArray:(NSMutableArray *)array
- {
- NSNumber *temp = array[first];
- array[first] = array[second];
- array[second] = temp;
- return;
- }
- -(int) findKthLargestNumber:(int)k
- inArray:(NSMutableArray *)array
- begin:(int)begin
- end:(int)end
- {
- int pivot = (begin + end) / 2;
- [self swapFirst:pivot withSecond:end inArray:array];
- int pivotPosition = begin;
- for (int i = begin; i < end; ++i) {
- if ([array[i] intValue] < [array[end] intValue]) {
- [self swapFirst:i withSecond:pivotPosition inArray:array];
- pivotPosition++;
- }
- }
- [self swapFirst:pivotPosition withSecond:end inArray:array];
- if (pivotPosition == k - 1) {
- return [array[pivotPosition] intValue];
- } else if (pivotPosition < k - 1) {
- return [self findKthLargestNumber:k inArray:array begin:pivotPosition + 1 end:end];
- } else {
- return [self findKthLargestNumber:k inArray:array begin:begin end:pivotPosition - 1];
- }
- }
Add Comment
Please, Sign In to add comment