Merge Sort - How to Succeed in Algorithms Interview


Merge Sort - How to Succeed in Algorithms Interview

How to Succeed in Algorithms Interview Series


Merge Sort

  • divide and conquer: left, right, count values while merging left and right
  • merge procedure with auxiliary array

Merge sort code

private int merge(int[] A, int start, int mid, int end) {
    int[] temp = new int[A.length];
    int leftIndex = start;
    int rightIndex = mid + 1;
    int index = start;
    int sum = 0;
    while (leftIndex <= mid && rightIndex <= end) {
        if (A[leftIndex] <= A[rightIndex]) {
            temp[index++] = A[leftIndex++];
        } else {
            temp[index++] = A[rightIndex++];
            sum += mid - leftIndex + 1;
        }
    }
    while (leftIndex <= mid) {
        temp[index++] = A[leftIndex++];
    }
    while (rightIndex <= end) {
        temp[index++] = A[rightIndex++];
    }
    for (int i = start; i <= end; i++) {
        A[i] = temp[i];
    }
    return sum;
}
public Iterable<Integer> mergeKSortedIterators(Iterator<Integer>[] iters){
     Queue<IntIter> minHeap = new PriorityQueue<IntIter>(iters.length, new iteratorComp());
     List<Integer> result = new ArrayList<Integer>();
     for(Iterator<Integer> iter : iters){
         if(iter.hasNext()){
             minHeap.add(new IntIter(iter));
         }
     }
     while(!minHeap.isEmpty()){
         IntIter curr = minHeap.poll();
         result.add(curr.next);
         if(curr.iter.hasNext()){
             minHeap.add(new IntIter(curr.iter));
         }. visit 1point3acres.com for more.
     }
     return result;
 }
 public class iteratorComp implements Comparator<IntIter>{
     public int compare(IntIter a, IntIter b){
         return a.next - b.next;
     }
 }
 public class IntIter {
     int next;
     Iterator<Integer> iter;
     public IntIter(Iterator<Integer> iter) {
         this.next = iter.next();
         this.iter = iter;
     }
 }

Merge two sorted arrays

Divide and conquer + merge sort

Given an integer array nums, return the number of range sums that lie in [lower, upper] inclusive.
Range sum S(i, j) is defined as the sum of the elements in nums between indices i and j (i ≤ j), inclusive.
Note:
A naive algorithm of O(n2) is trivial. You MUST do better than that.
Example:
Given nums = [-2, 5, -1], lower = -2, upper = 2,
Return 3.
The three ranges are : [0, 0], [2, 2], [0, 2] and their respective sums are: -2, -1, 2.

private int countWhileMergeSort(long[] sums, int start, int end, int lower, int upper) { if (end - start <= 1) return 0; int mid = (start + end) / 2; int count = countWhileMergeSort(sums, start, mid, lower, upper) + countWhileMergeSort(sums, mid, end, lower, upper); int j = mid, k = mid, t = mid; long[] cache = new long[end - start]; for (int i = start, r = 0; i < mid; ++i, ++r) { while (k < end && sums[k] - sums[i] < lower) k++; while (j < end && sums[j] - sums[i] <= upper) j++; while (t < end && sums[t] < sums[i]) cache[r++] = sums[t++]; cache[r] = sums[i]; count += j - k; } System.arraycopy(cache, 0, sums, start, t - start); return count; } ```

Labels

adsense (5) Algorithm (69) Algorithm Series (35) Android (7) ANT (6) bat (8) Big Data (7) Blogger (14) Bugs (6) Cache (5) Chrome (19) Code Example (29) Code Quality (7) Coding Skills (5) Database (7) Debug (16) Design (5) Dev Tips (63) Eclipse (32) Git (5) Google (33) Guava (7) How to (9) Http Client (8) IDE (7) Interview (88) J2EE (13) J2SE (49) Java (186) JavaScript (27) JSON (7) Learning code (9) Lesson Learned (6) Linux (26) Lucene-Solr (112) Mac (10) Maven (8) Network (9) Nutch2 (18) Performance (9) PowerShell (11) Problem Solving (11) Programmer Skills (6) regex (5) Scala (6) Security (9) Soft Skills (38) Spring (22) System Design (11) Testing (7) Text Mining (14) Tips (17) Tools (24) Troubleshooting (29) UIMA (9) Web Development (19) Windows (21) xml (5)