Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int startIndex = 0;
- int endIndex = candidates.length - 1;
- if(candidates.length <= 1){
- return ;
- }
- if (startIndex >= endIndex){
- return;
- }
- //seperating the array to implement merge sort
- int middle = (startIndex+endIndex)/2;
- ExecutiveAssistant [] firstPart = new ExecutiveAssistant[middle-startIndex+1];
- ExecutiveAssistant [] secondPart = new ExecutiveAssistant[endIndex - middle];
- int i;
- for(i = 0; i<=middle; i++){
- firstPart[i-startIndex] = candidates[i];
- }
- for(i=middle+1; i<=endIndex; i++){
- secondPart[i-middle-i] = candidates[i];
- }
- //recurse
- mergeSort(firstPart);
- mergeSort(secondPart);
- //mergeSort implemention kinda different cause here we know the length of the array not the first and last index.
- merge(candidates, firstPart, secondPart, startIndex,middle, endIndex);
- }
- public static void merge(ExecutiveAssistant [] candidates, ExecutiveAssistant [] firstPart, ExecutiveAssistant [] secondPart, int startIndex, int middle, int endIndex){
- int firstPointer = 0;
- int secondPointer = 0;
- for(int i = startIndex; i<= endIndex; i++){
- if(firstPointer >= firstPart.length){
- candidates[i] = secondPart[secondPointer];
- secondPointer++ ;
- continue;
- }
- if(secondPointer >= secondPart.length){
- candidates[i] = firstPart[firstPointer];
- firstPointer++ ;
- continue;
- }
- else {
- candidates[i] = secondPart[secondPointer];
- secondPointer++ ;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement