コード例 #1
0
    int kth_element(vector<int> &nums, int k, int start_index, int end_index) {
        if (start_index >= end_index) {
            return nums[start_index];
        }
        int pivot = nums[start_index];
        int scan_end = end_index;
        int i = start_index + 1;
        while (i <= scan_end) {
            if (nums[i] > pivot) {
                int temp = nums[i];
                nums[i] = nums[scan_end];
                nums[scan_end] = temp;
                scan_end--;
            } else {
                nums[i-1] = nums[i];
                i++;
            }
        }
        i--;
        nums[i] = pivot;

        if (i == k) {
            return nums[i];
        } else if (i <= k) {
            return kth_element(nums, k, i+1, end_index);
        } else {
            return kth_element(nums, k, start_index, i-1);
        }
    }
コード例 #2
0
 node* kth_element(node* &a, int k) {
     if (a == null || k <= 0 || k > a->size)	return null;
     if (k == a->lson->size + 1)
         return a;
     if (k <= a->lson->size)
         return kth_element(a->lson, k);
     else
         return kth_element(a->rson, k - a->lson->size - 1);
 }
コード例 #3
0
ファイル: hypre_merge_sort.c プロジェクト: ducpdx/hypre
/**
 * @param num_threads number of threads that participate in this merge
 * @param my_thread_num thread id (zeor-based) among the threads that participate in this merge
 */
static void hypre_parallel_merge(
   HYPRE_Int *first1, HYPRE_Int *last1, HYPRE_Int *first2, HYPRE_Int *last2,
   HYPRE_Int *out,
   HYPRE_Int num_threads, HYPRE_Int my_thread_num)
{
   HYPRE_Int n1 = last1 - first1;
   HYPRE_Int n2 = last2 - first2;
   HYPRE_Int n = n1 + n2;
   HYPRE_Int n_per_thread = (n + num_threads - 1)/num_threads;
   HYPRE_Int begin_rank = hypre_min(n_per_thread*my_thread_num, n);
   HYPRE_Int end_rank = hypre_min(begin_rank + n_per_thread, n);

#ifdef DBG_MERGE_SORT
   assert(std::is_sorted(first1, last1));
   assert(std::is_sorted(first2, last2));
#endif

   HYPRE_Int begin1, begin2, end1, end2;
   kth_element(&begin1, &begin2, first1, first2, n1, n2, begin_rank);
   kth_element(&end1, &end2, first1, first2, n1, n2, end_rank);

   while (begin1 > end1 && begin1 > 0 && begin2 < n2 && first1[begin1 - 1] == first2[begin2])
   {
#ifdef DBG_MERGE_SORT
      printf("%s:%d\n", __FILE__, __LINE__);
#endif
      begin1--; begin2++; 
   }
   while (begin2 > end2 && end1 > 0 && end2 < n2 && first1[end1 - 1] == first2[end2])
   {
#ifdef DBG_MERGE_SORT
      printf("%s:%d\n", __FILE__, __LINE__);
#endif
      end1--; end2++;
   }

#ifdef DBG_MERGE_SORT
   assert(begin1 <= end1);
   assert(begin2 <= end2);
#endif

   hypre_merge(
      first1 + begin1, first1 + end1,
      first2 + begin2, first2 + end2,
      out + begin1 + begin2);

#ifdef DBG_MERGE_SORT
   assert(std::is_sorted(out + begin1 + begin2, out + end1 + end2));
#endif
}
コード例 #4
0
ファイル: kth_element.cpp プロジェクト: weidagang/coding
// find the kth smallest number in nums[fromIdx, toIdx] (k starts from 0)
int kth_element(int fromIdx, int toIdx, int k) {
    // partition the array into 2 parts at random
    int idx = partition(fromIdx, toIdx, fromIdx);

    int m = idx - fromIdx;

    if (m == k) {
        return idx;
    }
    else if (m > k) {
        return kth_element(fromIdx, idx - 1, k);
    }
    else {
        // (k - m - 1) means we skipped (m + 1) items of the left part
        return kth_element(idx + 1, toIdx, k - m - 1);
    }
}
コード例 #5
0
ファイル: kth_two_sorted_array.c プロジェクト: seven-gao/alg
int main() {
  int a[] = {1, 3, 5, 7, 9};
  int b[] = {2, 4, 6, 8, 10};

  int e = kth_element(a, 5, b, 5, 6);

  printf("%d\n", e);
  return 0;
}
コード例 #6
0
int main() {
	size_t n, k;

	scanf("%u %u", (unsigned *)&n, (unsigned *)&k);

	int *a = new int[n];

	for (size_t i = 0; i < n; ++i) {
		scanf("%u", &a[i]);
	}

	printf("%u", a[kth_element(a, n, k)]);

	return 0;
};
コード例 #7
0
 /**
  * @param nums: A list of integers
  * @return: The majority number
  */
 int majorityNumber(vector<int> nums) {
     return kth_element(nums, nums.size() / 2, 0, nums.size() - 1);
 }
コード例 #8
0
ファイル: kth_element.cpp プロジェクト: weidagang/coding
int main() {
    for (int k = 1; k <= N; ++k) {
        int idx = kth_element(0, N - 1, k - 1);
        std::cout << "The " << k << " th element is: " << nums[idx] << std::endl;
    }
}
コード例 #9
0
/* Coalesce feature descriptors for each feature point */
void BundlerApp::CoalesceFeatureDescriptors()
{
    if (m_features_coalesced)
        return;

    int num_points = (int) m_point_data.size();
    int *num_observations = new int[num_points];

    for (int i = 0; i < num_points; i++) {
	num_observations[i] = 0;
	m_point_data[i].m_desc = new float[DESCRIPTOR_LENGTH];
	for (int j = 0; j < DESCRIPTOR_LENGTH; j++) 
	    m_point_data[i].m_desc[j] = 0.0f;
    }

    float *xx = new float[num_points * DESCRIPTOR_LENGTH];

    int num_images = GetNumImages();
    for (int i = 0; i < num_images; i++) {
	if (!m_image_data[i].m_camera.m_adjusted)
	    continue;

	printf("[CoalesceFeatureDescriptors] Adding features from "
	       "image %d\n", i);
        fflush(stdout);

	m_image_data[i].LoadKeys(true);

	for (int j = 0; j < num_points; j++) {
	    int num_views = (int) m_point_data[j].m_views.size();
	    int key_seen = -1;

	    if (num_views == 0)
		continue;

	    for (int k = 0; k < num_views; k++) {
		if (m_point_data[j].m_views[k].first == i) {
		    key_seen = m_point_data[j].m_views[k].second;
		    break;
		}
	    }

	    if (key_seen >= 0) {
                KeypointWithDesc &key = GetKeyWithDesc(i, key_seen);
		for (int k = 0; k < DESCRIPTOR_LENGTH; k++) {
                    float x = (float) key.m_d[k];
		    m_point_data[j].m_desc[k] += key.m_d[k];
                    xx[DESCRIPTOR_LENGTH * j + k] += x * x;
		}

		num_observations[j]++;
	    }
	}

	m_image_data[i].UnloadKeys();
    }

    double *variance = new double[num_points];

    for (int i = 0; i < num_points; i++) {
	if (num_observations[i] != (int) m_point_data[i].m_views.size())
	    printf("[CoalesceFeatureDescriptors] "
		   "Mismatch in observation count\n");

	if (num_observations[i] == 0) continue;
	
	for (int j = 0; j < DESCRIPTOR_LENGTH; j++) {
	    m_point_data[i].m_desc[j] /= num_observations[i];
            xx[DESCRIPTOR_LENGTH * i + j] /= num_observations[i];
        }

        variance[i] = 0.0;
        
        for (int j = 0; j < DESCRIPTOR_LENGTH; j++) {
            double x = m_point_data[i].m_desc[j];
            variance[i] += xx[DESCRIPTOR_LENGTH * i + j] - x * x;
        }
    }

    double median = 
        kth_element(num_points, iround(0.5 * num_points), variance);

    printf("[CoalesceFeatureDescriptors] Median squared variance: "
           "%0.3f\n", median);
    fflush(stdout);

    delete [] num_observations;
    delete [] xx;
    delete [] variance;

    m_features_coalesced = true;
}