Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. int startIndex = 0;
  2. int endIndex = candidates.length - 1;
  3.  
  4. if(candidates.length <= 1){
  5. return ;
  6. }
  7. if (startIndex >= endIndex){
  8. return;
  9. }
  10. //seperating the array to implement merge sort
  11. int middle = (startIndex+endIndex)/2;
  12.  
  13. ExecutiveAssistant [] firstPart = new ExecutiveAssistant[middle-startIndex+1];
  14. ExecutiveAssistant [] secondPart = new ExecutiveAssistant[endIndex - middle];
  15. int i;
  16. for(i = 0; i<=middle; i++){
  17. firstPart[i-startIndex] = candidates[i];
  18. }
  19. for(i=middle+1; i<=endIndex; i++){
  20. secondPart[i-middle-i] = candidates[i];
  21. }
  22. //recurse
  23. mergeSort(firstPart);
  24. mergeSort(secondPart);
  25.  
  26. //mergeSort implemention kinda different cause here we know the length of the array not the first and last index.
  27. merge(candidates, firstPart, secondPart, startIndex,middle, endIndex);
  28. }
  29.  
  30. public static void merge(ExecutiveAssistant [] candidates, ExecutiveAssistant [] firstPart, ExecutiveAssistant [] secondPart, int startIndex, int middle, int endIndex){
  31. int firstPointer = 0;
  32. int secondPointer = 0;
  33. for(int i = startIndex; i<= endIndex; i++){
  34. if(firstPointer >= firstPart.length){
  35. candidates[i] = secondPart[secondPointer];
  36. secondPointer++ ;
  37. continue;
  38. }
  39. if(secondPointer >= secondPart.length){
  40. candidates[i] = firstPart[firstPointer];
  41. firstPointer++ ;
  42. continue;
  43. }
  44. else {
  45. candidates[i] = secondPart[secondPointer];
  46. secondPointer++ ;
  47. }
  48. }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement