Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: Efficient s...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 3 of 5 Topic 9530 of 9823
Post > Topic >>

Re: Efficient sorting

by Martin York <Martin.YorkAmazon@[EMAIL PROTECTED] > Apr 23, 2008 at 01:10 PM

> for (int j = 0; j < count; j++)
>         {
>         for (i = j+1; i < count; i++)
>                 {
>                 operations++;
>                 if (vector_in[i] < vector_in[j])
>                         {
>                         temp_int = vector_in[i];
>                         vector_in[i] = vector_in[j];
>                         vector_in[j] = temp_int;
>                         }
>                 }
>         }

AKA: Bubble sort.
Sorting algorithms are measured in term of their complexity:
Bubble sort has a complexity of O(n^2)

In bubble sort, if you had an array of n elements, th sort requires
two nested loops that both basically iterate over the whole length of
the array (though you have tried to optimizeing this by limiting the
inner loop this does not affect the complexity) [PS your optimization
fails].

There are multiple sorts available (do a google on sorting
algorithms), The most optimal sort is called a bucket sort. Under
optimal conditions has a complexity of O(n).

-- 
      [ See http://www.gotw.ca/resources/clcm.htm
for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]
 




 5 Posts in Topic:
Efficient sorting
"C++ Newbie" &l  2008-04-22 15:21:08 
Re: Efficient sorting
Thomas Maeder <maeder@  2008-04-22 22:19:12 
Re: Efficient sorting
Martin York <Martin.Yo  2008-04-23 13:10:25 
Re: Efficient sorting
Markus Moll <markus.mo  2008-04-24 15:19:37 
Re: Efficient sorting
"Jeff Baker" &l  2008-04-25 03:39:00 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 1:41:08 CDT 2008.