Three methods of counting and sorting

Source: Internet
Author: User
// Count_sort.cpp: defines the entry point of the console application. # Include "stdafx. H" # include <iostream> using namespace STD; const int Len = 100; Class countsort // count sorting class {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;}/* Method 1: For each element in the original array, count Array records the number of elements smaller than it. This number is the subscript of this element in the new sorted array */void countsort: fristsort () {int * COUNT = new int [length]; // for each element in the original array, the Count Array records the number of elements smaller than it. memset (count, 0, length * sizeof (INT); int * rarr = new int [length]; // rarr is the newly sorted array 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;}/* Method 2: use a temporary array to record the number of times each element appears in the original array */void countsort: secondsort () {int max = 0; // Max records the maximum element value in the array for (INT I = 0; I <length; I ++) {If (ARR [I]> MAX) {max = arr [I]; // find the maximum value} int * COUNT = new int [Max + 1]; // The Count Array records the number of occurrences of each element in the array memset (count, 0, (MAX + 1) * sizeof (INT )); int * rarr = new int [Max + 1]; // The rarr array stores the sorted element memset (rarr, 0, (MAX + 1) * sizeof (INT )); for (INT I = 0; I <length; I ++) {count [arr [I] ++; // number of record elements} 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;}/* method 3: use a temporary array to record the number of times each element appears in the original array */void countsort: thirdsort () {int max = 0; // Max records the maximum element value in the array for (INT I = 0; I <length; I ++) {If (ARR [I]> MAX) {max = arr [I]; // find the maximum value} int * COUNT = new int [Max + 1]; // The Count Array records the number of occurrences of each element in the array memset (count, 0, (MAX + 1) * sizeof (INT); For (INT I = 0; I <length; I ++) {count [arr [I] ++; // number of record elements} 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 <"Before sorting:" <Endl; cout <* pcountsort; // pcountsort-> fristsort (); pcountsort-> secondsort (); // pcountsort-> thirdsort (); cout <"after sorting: "<Endl; cout <* pcountsort; System (" pause "); Return 0 ;}

Vs2008 is running correctly. If you have any questions, please correct me!

Counting sorting assume that each of the N input elements is an integer between 0 and K. Here K is an integer. When k = O (N, the running time of counting sorting is O (n ).

The basic idea of counting sorting: For each input element x, determine the number of elements smaller than or equal to X. With this information, you can direct X

Place it in the output array.

Not applicable: the array contains negative values or a small number is extremely large.

Algorithm analysis:

1. the time complexity is O (n ).

2. The space complexity is O (n ).

3. Counting sorting is notIn-situ sortingAlgorithm (sorting without applying for extra space );

YesStabilitySorting Algorithm (that is, the relative order between the same keywords remains unchanged before and after sorting );

It is not a comparison-based sorting algorithm. The time complexity of the comparison sorting algorithm is O (n * logn ).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.