# 重复元素选择算法 selection 源代码

// Copyright (C) 2004 BenBear
//
// This file is an algorithm of selection.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 2, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License along
// with this library; see the file COPYING.  If not, write to the Free
// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
// USA.

#include <algorithm>
#include <iostream>
#include <cstdlib>
using namespace std;

template <typename BiIter>
bool
next_selection (BiIter first, BiIter last)
{
if (first == last)
return false;

BiIter i = last;
if (--i == first)
return false;

BiIter j = i;
if (*--i == 0)
{
while ((i != first) && (*i == 0))
--i;
if (*i == 0)
{
iter_swap (first, j);
return false;
}

BiIter ii = i;
*(++ii) = *j + 1;
*j = 0;
--*i;
}
else
{
--*i;
++*j;
}
return true;
}

template <typename BiIter>
bool
prev_selection (BiIter first, BiIter last)
{
if (first == last)
return false;

BiIter i = last;
--i;
if (first == i)
return false;

if (*i == 0)
{
while (*--i == 0)
;
if (i == first)
{
*--last = *i;
*i = 0;
return false;
}
BiIter j = i;
--j;
++*j;
--*i;
iter_swap (i, --last);
}
else
{
BiIter j = i;
--j;
++*j;
--*i;
}
return true;
}

// for test:
int main()
{
const int n = 4;
int a[n] = {0, 0, 0, 3};

do
{
for (int i = 0; i < n; ++i)
for (int j = 0; j < a[i]; ++j)
cout << (i + 1);
cout << endl;
}
while (prev_selection (a, a+n));

cout << "------------------" << endl;

int b[n] = {3, 0, 0, 0};
do
{
for (int i = 0; i < n; ++i)
for (int j = 0; j < b[i]; ++j)
cout << (i + 1);
cout << endl;
}
while (next_selection (b, b+n));

system ("pause");
}

#### 【数据结构与算法】选择算法 selection

2017-02-16 04:59:13

#### 随机选择算法---Randomized Selection Algorithm

2013-07-24 11:14:58

#### 总结 特征选择（feature selection）算法笔记

2017-01-02 16:50:18

#### 选择算法伪代码，C代码，以及分析

2017-02-14 12:04:56

#### Java机器学习库ML之二Feature Selection（特征选择）

2017-06-21 15:25:07

#### 特征选择算法总结

2015-10-20 16:36:14

#### Feature Selection: A Data Perspective --阅读笔记2 传统数据的特征选择算法

2017-11-27 17:39:43

#### 机器学习中特征选择概述

2014-06-20 16:50:13

#### 特征选择方法学习笔记（一）

2013-12-11 11:27:44

#### 重复的选择算法 selection（计数法）

2004-08-25 19:53:00