计数排序的三种方法
来源:互联网 发布:mac win7双系统 编辑:程序博客网 时间:2024/04/28 09:00
// Count_Sort.cpp : 定义控制台应用程序的入口点。#include "stdafx.h"#include <iostream>using namespace std;const int len = 100;class CountSort //计数排序类{public:CountSort();~CountSort();void fristsort();void secondsort();void thirdsort(); friend ostream& operator<<(ostream& out,const CountSort& countsort);private:int *arr;int length;};CountSort::CountSort():length(len){ arr = new int[length]; for (int i=0; i<length; i++) { arr[i] = rand()%1000; }}CountSort::~CountSort(){delete[] arr;arr = NULL;}ostream& operator<<(ostream& out,const CountSort& countsort){ for (int i=0; i<countsort.length; i++) { cout<<countsort.arr[i]<<" "; } cout<<endl; return out;}/*第一种方法:对于原数组中每个元素,count数组记录比它小的元素个数,这个数即为该元素在新的排好序的数组下标*/void CountSort::fristsort(){int *count = new int[length]; //对于原数组中每个元素,count数组记录比它小的元素个数memset(count,0,length*sizeof(int));int *rarr = new int[length]; //rarr是新排好序的数组memset(rarr,0,length*sizeof(int));for (int i=0; i<length; i++){ for (int j=i+1; j<length; j++){if (arr[j] < arr[i]) { count[i]++; }else { count[j]++; }}rarr[count[i]] = arr[i];}for (int t=0; t<length; t++){arr[t] = rarr[t];}delete[] count;delete[] rarr;}/*第二种方法:用一个临时数组记录原数组中每个元素出现的次数*/void CountSort::secondsort(){ int max = 0;//max记录数组中最大的元素值 for (int i=0; i<length; i++) { if (arr[i] > max) { max = arr[i]; //找到最大值 } } int *count = new int[max+1];//count数组记录数组中每个元素出现的次数 memset(count,0,(max+1)*sizeof(int)); int *rarr = new int[max+1]; //rarr数组存放排好序的元素 memset(rarr,0,(max+1)*sizeof(int)); for (int i=0; i<length; i++) { count[arr[i]]++; //记录元素的个数 } for (int j=1; j<=max; j++) { count[j]+=count[j-1]; } for (int t=length-1; t>=0; t--) { rarr[count[arr[t]]-1] = arr[t]; count[arr[t]]--; } for (int z=0; z<length; z++) { arr[z] = rarr[z]; } delete[] count; delete[] rarr;}/*第三种方法:用一个临时数组记录原数组中每个元素出现的次数*/void CountSort::thirdsort(){int max = 0;//max记录数组中最大的元素值for (int i=0; i<length; i++){if (arr[i] > max){max = arr[i]; //找到最大值}}int *count = new int[max+1];//count数组记录数组中每个元素出现的次数memset(count,0,(max+1)*sizeof(int));for (int i=0; i<length; i++){count[arr[i]]++; //记录元素的个数}int z=0;for (int i=0; i<=max; i++){while(count[i]-- > 0){arr[z++]=i;}}delete[] count;} int _tmain(int argc, _TCHAR* argv[]){CountSort *pcountsort = new CountSort();cout<<"排序前:"<<endl;cout<<*pcountsort;//pcountsort->fristsort();pcountsort->secondsort(); //pcountsort->thirdsort();cout<<"排序后:"<<endl; cout<<*pcountsort;system("pause");return 0;}
vs2008运行正确,如有问题,请各位大牛指正!
计数排序假设n个输入元素中的每一个都是介于0到k之间的整数,此处k为某个整数,当 k= O(n)时, 计数排序的运行时间为O(n)。
计数排序的基本思想:对每一个输入元素x,确定出小于或等于x的元素个数,有了这一信息就可以把x直
接放到它最终在输出数组中的位置。
不适用情况:数组中有负数或有少量数极大
算法分析:
1.时间复杂度为 O(n)。
2.空间复杂度为 O(n)。
3.计数排序不是原地排序算法(指不申请多余的空间来进行的排序);
是稳定排序算法(指在排序前后具有相同关键字之间的相对顺序保持不变);
不是基于比较的排序算法,比较排序算法的时间复杂度为O(n*logn)。
- 计数排序的三种方法
- 三种线性的非基于比较的排序算法:计数排序、桶排序与基数排序
- 三种线性的非基于比较的排序算法:计数排序、桶排序与基数排序
- 三种线性的非基于比较的排序算法:计数排序、桶排序与基数排序
- 三种基于“分配”“收集”的线性排序算法---计数排序、桶排序与基数排序
- 三种线性排序算法(计数、基数、桶排序)的简单实现
- 排序算法(三)_计数排序、基数排序的Java实现
- 三种excel 多条件计数方法
- Java 三种线性排序之计数排序
- 三种排序方法
- 三种 排序方法
- 三种排序方法
- 冒泡排序法的三种方法。
- 三种排序方法的流程总结
- Excel电子表格排序的三种方法
- 三种排序方法的比较
- 总结的三种排序方法
- 冒泡排序的三种实现方法
- Android Log 及eclipse中文乱码
- EDM邮件制作指南
- Oracle:导出A库指定表部分数据到B库
- C#:Linq时代勿用Count函数判断集合非空
- Struts2文件上传下载(一)
- 计数排序的三种方法
- 红黑树
- request.getParameterMap()中的值
- Objective-C 语法之词典对象(NSDictionary/NSMutableDictionary)
- Java中的数组
- 元器件PDF资料查询
- Qt美化
- 标准网
- 数据结构(C语言)实训-电话客服模拟器