예제 #1
0
 int partition(vector<string> &nuts, vector<string> &bolts, int s, int e, Comparator compare) {
     for(int i = s; i <= e; i++) {
         if(compare.cmp(nuts[s], bolts[i]) == 0) {
             swap(bolts[s], bolts[i]);
             break;
         }
     }
     int p = s+1, i = s+1;
     while(i <= e) {
         if(compare.cmp(nuts[s], bolts[i]) == 1) {
             swap(bolts[p], bolts[i]);
             p++;
         }
         i++;
     }
     swap(bolts[s], bolts[--p]);
     swap(nuts[s], nuts[p]);
     i = s; 
     int j = p+1;
     while(i <= p-1 && j <= e) {
         if(compare.cmp(nuts[i], bolts[p]) == 1) {
             swap(nuts[i], nuts[j]);
             j++;
         } else {
             i++;
         }
     }
     return p;
 }
 void sort(vector<string> &nuts, vector<string> &bolts, int left, int right, Comparator compare) {
     if (left >= right) return;
     
     fprintf(stderr, "%d %d ", left, right);
     
     int ni, nj, bi, bj, m;
     ni = bi = left; nj = bj = right;
     m = (left + right) >> 1;
     
     // recover bolts
     int bp = 0;
     while (bi < bj) {
         while (bi < bj && compare.cmp(nuts[m], bolts[bi]) < 0) bi ++;
         while (bi < bj && compare.cmp(nuts[m], bolts[bj]) > 0) bj --;
         
         if (compare.cmp(nuts[m], bolts[bi]) == 0) bp = bj; else
         if (compare.cmp(nuts[m], bolts[bj]) == 0) bp = bi;
         
         if (bi < bj) {
             swap(bolts, bi, bj);
             bi ++; bj --;
         }
         // print(bolts);
     }
     // printf("%d %d\n", bi, bp);
     while (compare.cmp(nuts[m], bolts[bi]) < 0) bi ++;
     fprintf(stderr, "%d %d ", bi, bp);
     if (bp < bi) { swap(bolts, bp, bi - 1); bp = bi - 1; } else
     if (bp > bi) { swap(bolts, bp, bi); bp = bi; }
     // print(bolts);
     
     // recover nuts
     swap(nuts, m, bp);
     m = bp;
     int np = 0;
     while (ni < m) {
         while (ni < m && compare.cmp(nuts[ni], bolts[m]) > 0) ni ++;
         while (ni < m && compare.cmp(nuts[nj], bolts[m]) < 0) nj --;
         
         if (ni < m) {
             swap(nuts, ni, nj);
             ni ++; nj --;
         }
     }
     // print(nuts);
     
     fprintf(stderr, "%d|", m);
     sort(nuts, bolts, left, m - 1, compare);
     sort(nuts, bolts, m + 1, right, compare);
 }
예제 #3
0
 void quicksort(vector<string>& nuts, vector<string>& bolts, int begin, int end, Comparator cmp) {
     if (begin >= end) return;
     int pivot_pos;
     int left = 0;
     for (int i = begin; i <= end; ++i) {
         int res = cmp.cmp(nuts[begin], bolts[i]);
         if (res == 0) {
             // find the corresponding place for bolt
             pivot_pos = i;
         } else if (res == 1) {
             // find number of smaller bolts
             ++left;
         }
     }
     // put pivot into position
     swap(nuts, begin, begin+left);
     swap(bolts, pivot_pos, begin+left);
     pivot_pos = begin + left;
     // do mutual quicksort
     int i = begin, j = end;
     while (i < pivot_pos && pivot_pos < j) {
         while (i < pivot_pos && cmp.cmp(nuts[pivot_pos], bolts[i]) == 1) ++i;
         while (j > pivot_pos && cmp.cmp(nuts[pivot_pos], bolts[j]) == -1) --j;
         if (i < j) {
             swap(bolts, i, j);
             ++i;
             --j;
         }
     }
     i = begin, j = end;
     while (i < pivot_pos && pivot_pos < j) {
         while (i < pivot_pos && cmp.cmp(nuts[i], bolts[pivot_pos]) == -1) ++i;
         while (j > pivot_pos && cmp.cmp(nuts[j], bolts[pivot_pos]) == 1) --j;
         if (i < j) {
             swap(nuts, i, j);
             ++i;
             --j;
         }
     }
     quicksort(nuts, bolts, begin, pivot_pos-1, cmp);
     quicksort(nuts, bolts, pivot_pos+1, end, cmp);
 }
    // All the smaller elements should be in the left side of the pivot,
    // and all the bigger elements should in the right side of the pivot.
    int partition(vector<string>& arr,
                  int left, int right, const string& pivot,
                  Comparator& compare) {
        for (int i = left; i < right; ) {
            if (compare.cmp(arr[i], pivot) == SMALLER ||  // Smaller.
                    (compare.cmp(arr[i], pivot) == REVERSE &&
                     compare.cmp(pivot, arr[i]) == BIGGER)) {
                swap(arr[left++], arr[i++]);
            } else if (compare.cmp(arr[i], pivot) == BIGGER ||  // Bigger.
                       (compare.cmp(arr[i], pivot) == REVERSE &&
                        compare.cmp(pivot, arr[i]) == SMALLER)) {
                ++i;
            } else {  // Equal.
                swap(arr[i], arr[right]);
            }
        }
        // Put the pivot to the partition index.
        swap(arr[left], arr[right]);

        // Return the partition index of an array.
        return left;
    }
예제 #5
0
    int partition(vector<string> &str, string pivot, Comparator compare, int l, int u){

        int mid = l; // middle
        for (int i = l + 1; i <= u; i++) {
            if (compare.cmp(str[i], pivot) == -1 || 
                compare.cmp(pivot, str[i]) == 1) {
                // str[i] smaller than pivot
                mid++;
                swap(str, i, mid);
            } else if (compare.cmp(str[i], pivot) == 0 || 
                       compare.cmp(pivot, str[i]) == 0) {
                // swap nuts[l]/bolts[l] with pivot
				// l位置放pivot
                swap(str, i, l);
                i--;
            }
        }
        // move pivot to proper index
        swap(str, mid, l);

        return mid;
 
    }
 /**
  * @param nuts: a vector of integers
  * @param bolts: a vector of integers
  * @param compare: a instance of Comparator
  * @return: nothing
  */
 void sortNutsAndBoltsN2(vector<string> &nuts, vector<string> &bolts, Comparator compare) {
     int n = nuts.size();
     int m = bolts.size();
     
     for (int i = 0; i < n - 1; i ++) {
         int j = i;
         while (j < m && compare.cmp(nuts[i], bolts[j])) j ++;
         string temp = bolts[j];
         bolts[j] = bolts[i];
         bolts[i] = temp;
     }
     
     for (int i = 0; i < n; i ++) {
         printf("%s ", bolts[i].c_str());
     }
 }