Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6687175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:15:27+00:00 2026-05-26T05:15:27+00:00

While playing around with as’ vector.sort() I found out that it behaves normally in

  • 0

While playing around with as’ vector.sort() I found out that it behaves normally in every case except where it needs to sort a vector with only 2 or 3 distinctive values. If this is the case, the sort() function runs extremely slowly.
Here’s my code:

var test:Vector.<int>=new Vector.<int>  ;
for (var i:int=0; i<6000; i++) {
    test.push(Math.floor(Math.random()*2));
}
var timer:Number
var timer2:Number
timer=new Date().getTime();
test.sort(compare)
timer2=new Date().getTime();
trace(timer2-timer)
function compare(x:int,y:int):Number {
    if (x>y) {
        return 1;
    } else if (x<y) {
        return -1;
    } else {
        return 0;
    }
}

simply copypaste this code.
What might be the problem here?
thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:15:28+00:00Added an answer on May 26, 2026 at 5:15 am

    This doesn’t actually happen with 2 or 3 values, more precisely, the effect is more noticeable the fewer distinct values there are in the vector. Each sorting algorithm has it’s cons and pros. The built-in one (I reckon it’s the bubble sort) will perform nearly it’s worst case when there are few distinct values. You could try this simple alternative (sort of radix sort), but notice that it will have larger memory footprint, and the larger the more distinct values there are in the vector.

    package examples 
    {
    import flash.display.Sprite;
    
    /**
     * An example for stackoverflow.com
     * @author wvxvw
     */
    public class KindOfRadixSort extends Sprite
    {
    
        public function KindOfRadixSort() 
        {
            super();
            this.test();
        }
    
        private function test():void
        {
            var test:Vector.<int> = new <int>[];
            var timer:Number;
            var timer2:Number;
    
            for (var i:int; i < 6000; i++) test.push(Math.random() * 5);
    
            timer = new Date().getTime();
            test.sort(compare);
            timer2 = new Date().getTime();
            trace(timer2 - timer); // 2488
    
            timer = new Date().getTime();
            this.kindOfRadixSort(test);
            timer2 = new Date().getTime();
            trace(timer2 - timer); // 3
        }
    
        private function compare(x:int, y:int):int
        {
            var result:int;
            if (x > y) result = 1;
            else if (x < y) result = -1;
            return result;
        }
    
        private function kindOfRadixSort(vector:Vector.<int>):Vector.<int>
        {
            // Arrays are sparse, therefore we don't allocate memory when having
            // gaps between filled indices as we would have we used vector.
            var cache:Array = [];
            var result:Vector.<int> = new <int>[];
            var i:int;
    
            for each (i in vector)
            {
                if (cache[i]) (cache[i] as Vector.<int>).push(i);
                else cache[i] = new <int>[i];
            }
            // A for-each loop could be better here, and, in fact, for-each on
            // arrays usually provides elements in order 0..length, however,
            // the specs say the order is undefined.
            for (i = cache.length - 1; i >= 0; i--)
            {
                if (cache[i] is Vector.<int>)
                    result = (cache[i] as Vector.<int>).concat(result);
            }
            return result;
        }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While playing around with Google Closure Compiler, I found a case where I couldn't
While playing around with the emulator, I noticed that when trying to view a
Huh that's ironic ,while playing around today i wondered if a can Increase Int64.MaxValue
I've noticed, while playing around with it, that if you scroll up and down
I have been playing around with wf4 for a while. I found various articles,
While playing around with regexps in Scala I wrote something like this: scala> val
There is an eval() function in Python I stumbled upon while playing around. I
I've been playing around the Facebook Graph API for a while now. I'm trying
While playing around with playframework yabe-siena-gae, I noticed a datastore file created for the
While playing around with one-to-one associations in castle activerecord I stumbled upon the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.