Results 1 to 5 of 5

Thread: C++: Counting Sort

  1. #1

    Thread Starter
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    C++: Counting Sort

    Any optimizations?

    Code:
    // v is the vector to sort
    // min is the smallest item in the array
    // max is the largest.
    // counting sort functions in O(range * n) time
    void counting_sort(vector<int>& v, int min, int max)
    {
    	int range = max - min + 1;
    	int* counts = new int[range];
    	int offset = -min;
    	vector<int>::size_type size = v.size();
    
    	memset(counts, 0, range * sizeof(int));
    
    	for(int i = 0; i < size; ++i)
    		++counts[v[i] + offset];
    
    	int index = 0;
    	for(int i = 0; i < range; ++i)
    		while(counts[i]--)
    			v[index++] = i - offset;
    
    	delete [] counts;
    	
    	return;
    }
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: C++: Counting Sort

    Yeah. Not sure if it makes the code any faster, but it does drop 1 line..
    Code:
    // v is the vector to sort
    // min is the smallest item in the array
    // max is the largest.
    // counting sort functions in O(range * n) time
    void counting_sort(vector<int>& v, int min, int max)
    {
    	int range = max - min + 1;
    	int *counts = new int[range];
    	int offset = -min;
    	vector<int>::size_type size = v.size();
    
    	memset(counts, 0, range * sizeof(int));
    
    	for(int i = 0; i < size; ++i)
    		++counts[v[i] + offset];
    
    	//int index = 0;
    	for(int i = 0, index = 0; i < range; ++i)
    		while(counts[i]--)
    			v[index++] = i - offset;
    
    	delete [] counts;
    	
    	return;
    }
    I don't live here any more.

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Doesn't make it faster.

    What's the algorithm to do, exactly? What do you want to count?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    I use counting sort when I need to sort a large amount of numbers that fall in a relatively small range. Because counting sort never has to compare numbers, it can operate in O(n) time, but is only useful in a narrow range of situations.

    What it actually does is instead of comparing, it counts (hence, counting sort )the number of time each value occurs and places that value in an array at the index of the value. When it finishes counting, it then iterates over the array and places each item back in the original array the number of times indicated.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Radix or Bucket sort, this kind of thing?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width