C++ to find the median of O of N algorithm and Kmin algorithm

  • 2020-04-02 02:47:19
  • OfStack

In this paper, the example of C++ implementation of the search median O(N) algorithm and Kmin algorithm, to share for your reference. Specific methods are as follows:

The partition operation of quick sort is used to complete the search algorithm of the median in O(N) time as follows:


#include <iostream>
#include <cassert>
#include <algorithm>
#include <iterator>

using namespace std;

int array[] = {1, 2, 10, 8, 9, 7, 5};
const int size = sizeof array / sizeof *array;

int partition(int *array, int left, int right)
{
 if (array == NULL)
 return -1;

 int pos = right;
 right--;
 while (left <= right)
 {
 while (left < pos && array[left] <= array[pos])
  left++;

 while (right >= 0 && array[right] > array[pos])
  right--;

 if (left >= right)
  break;

 swap(array[left], array[right]);
 }
 swap(array[left], array[pos]);

 return left;
}

int getMidIndex(int *array, int size)
{
 if (array == NULL || size <= 0)
 return -1;

 int left = 0;
 int right = size - 1;
 int midPos = right >> 1;
 int index = -1;

 while (index != midPos)
 {
 index = partition(array, left, right);

 if (index < midPos)
 {
  left = index + 1;
 }
 else if (index > midPos)
 {
  right = index - 1;
 } 
 else
 {
  break;
 }
 }

 assert(index == midPos);
 return array[index];
}

void main()
{
 int value = getMidIndex(array, size);

 cout << "value: " << value << endl;
}

Find the kmin algorithm as follows:


int findKMin(int *array, int size, int k)
{
 if (array == NULL || size <= 0)
 return -1;

 int left = 0;
 int right = size - 1;
 int index = -1;

 while (index != k)
 {
 index = partition(array, left, right);

 if (index < k)
 {
  left = index + 1;
 }
 else if (index > k)
 {
  right = index - 1;
 } 
 else
 {
  break;
 }
 }

 assert(index == k);
 return array[index];
}

Hope that this article is helpful to the learning of C++ programming algorithm design.


Related articles: