The Program of Algorithms ------- Sorting in Linear Time---- Counting Sort
2013-04-13 14:03
435 查看
Input: A[1...n], each 0<A[i]<=k
Output: B[1...n] = sorting of A
Auxiliary: C[1...k]
Restricts:
1. each element little than a const "k"
2. the element should be integer
3. the range of the elements shoud not be so large (or it will cost more than nlgn and waste more space)
The idea about this algorithm:
About the arrary C, C[i] store the information about how many numbers in the array A which less than
or equal "i".
To finish this task:
Step1: traverse the array A and C[A[i]]++. Such as A[0]=10, then the C[10]++.
Step2: use prefix sum to change the value in array C. After the transform C[i] give number of keys less than or equal i.
Advantage of Counting Sort: The Time Complexity is O(k+n), if k=O(n) the Time will be O(n), which beat all comparsion sort algorithm.
Disadvantage: It cost so many space. It can be represented as O(k).
Output: B[1...n] = sorting of A
Auxiliary: C[1...k]
Restricts:
1. each element little than a const "k"
2. the element should be integer
3. the range of the elements shoud not be so large (or it will cost more than nlgn and waste more space)
The idea about this algorithm:
About the arrary C, C[i] store the information about how many numbers in the array A which less than
or equal "i".
To finish this task:
Step1: traverse the array A and C[A[i]]++. Such as A[0]=10, then the C[10]++.
Step2: use prefix sum to change the value in array C. After the transform C[i] give number of keys less than or equal i.
#include <iostream> using namespace std; int * CountSort(int* A,int p,int q,int bound){ int size=q-p+1; int * C=new int[bound+1]; int * B=new int[size]; for(int i=0;i<=bound;i++){ C[i]=0; } for(int i=0;i<size;i++){ C[A[p+i]]++; } for(int i=1;i<=bound;i++){ C[i]+=C[i-1]; } for(int j=size-1;j>=0;j--){ B[C[A[p+j]]-1]=A[p+j]; C[A[p+j]]--; } return B; } int main(){ int Arr[15]={1,4,3,6,2,6,3,5,4,9,2,4,6,7,3}; int *Res; Res=CountSort(Arr,0,14,9); for(int i=0;i<14;i++){ cout<<Res[i]<<" "; } return 0; }
Advantage of Counting Sort: The Time Complexity is O(k+n), if k=O(n) the Time will be O(n), which beat all comparsion sort algorithm.
Disadvantage: It cost so many space. It can be represented as O(k).
相关文章推荐
- The Program of Algorithms ------- Sorting in Linear Time---- Bucket Sort
- The Proccess of Loading and Executing a Specific Program in Linux Kernel
- 7 Strace Examples to Debug the Execution of a Program in Linux
- Chapter 1 The Role of Algorithms in Computing
- 第一次外国人在qq上找我聊天【为了找php程序员】The first time of foreigners in qq chat on me.
- The Program of Algorithms ------- Diveide and Conquer ---- Fibonacci
- [转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)
- 算法导论 CH01 The Role of Algorithms in Computing
- java.lang.RuntimeException: Date pattern must be set for column update_time in the schema of component
- ValueError: The hardcoded shape for thenumber of rows in the filter (5) isn't the run time shape (6)
- The Program of Algorithms ------- Diveide and Conquer ---- Randomized-Select
- Time, Clocks, and the Ordering of Events in a Distributed System
- Note: Time clocks and the ordering of events in a distributed system
- Time, Clocks, and the Ordering of Events in a Distributed System
- Introduction to Algorithm - Summary of Chapter 8 - Sorting in Linear Time
- 推荐一篇文章<<Time, Clocks, and the Ordering of Events in a Distributed System>>
- [TroubleShooting] The remote copy of database xx has not been rolled forward to a point in time
- sorting data based on the value in second column of a file
- Question 17: Which of the following identify const-correctness failures in the C++ program below?
- Matrix and linear algebra in F#, Part IV: profile your program, find the bottleneck and speed it up: using matrix multiplication as an example[z]