2011 4-25 计数排序
2011-04-26 10:31
197 查看
#pragma once
#include <iostream>
// #include <utility>
using std::cout;
using std::endl;
using std::pair;
template<typename value_type>
void counterSortImp(const value_type *const array, const pair<value_type, value_type> &scope, size_t size) {
// Firstly, calculate the count of any kind in the scope
size_t difference = scope.second - scope.first;
int *collection = new int[difference]();
for (size_t i=0; i<size; ++i)
{
int index = array[i] - scope.first;
++collection[index];
}
// Get the index of every kind of value_type
for (int i=1; i<difference; ++i)
{
collection[i] += collection[i-1];
}
// Sort the result into sorted array
value_type *sorted = new value_type[size]();
for (int i=size-1; i>=0; --i)
{
int index = array[i] - scope.first;
sorted[collection[index]-1] = array[i];
--collection[index];
}
// if you need, copy sorted to array by your self.
}
#include <iostream>
// #include <utility>
using std::cout;
using std::endl;
using std::pair;
template<typename value_type>
void counterSortImp(const value_type *const array, const pair<value_type, value_type> &scope, size_t size) {
// Firstly, calculate the count of any kind in the scope
size_t difference = scope.second - scope.first;
int *collection = new int[difference]();
for (size_t i=0; i<size; ++i)
{
int index = array[i] - scope.first;
++collection[index];
}
// Get the index of every kind of value_type
for (int i=1; i<difference; ++i)
{
collection[i] += collection[i-1];
}
// Sort the result into sorted array
value_type *sorted = new value_type[size]();
for (int i=size-1; i>=0; --i)
{
int index = array[i] - scope.first;
sorted[collection[index]-1] = array[i];
--collection[index];
}
// if you need, copy sorted to array by your self.
}
相关文章推荐
- Dynamics CRM 2011 编程系列(25):插件的依赖注入
- 6/25/2011 10:56:51 PM
- android开发指南之新手篇【2011-11-25】
- 25 JAN 2011 Note
- 2011-11-25杂记
- may 25,2011
- 2011网络&IT界大事件TOP 25
- NetBeans 时事通讯(刊号 # 134 - Jan 25, 2011)
- 6/25/2011 8:25:05 PM
- NetBeans 时事通讯(刊号 # 134 - Jan 25, 2011)
- absolute fixed效果 复制网页打开就是代码 http://www.cnblogs.com/0banana0/archive/2011/05/25/2056643.html
- 6/25/2011 9:30:09 PM
- NetBeans 时事通讯(刊号 # 134 - Jan 25, 2011)
- NetBeans 时事通讯(刊号 # 134 - Jan 25, 2011)
- NetBeans 时事通讯(刊号 # 135 - Jan 25, 2011)
- 【SCRUM Report】Beta 3--2011/05/25
- 2011-110-25
- NetBeans 时事通讯(刊号 # 135 - Jan 25, 2011)
- EXTJS 日期时间转换 Mon Apr 25 01:00:00 UTC+0800 2011 转成 Y-m-dTH:i:s 2011-4-28T18:02:00
- 读书时间 05/25/2011 Linux C/C++ 孤儿进程