Efficient yet simple sorting algorithms

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

Sir Fredrick

Guest
Oct 14, 1999
4,375
0
0
Yeah, there is certainly more to efficiency than reduced levels of recursion. My sorting algorithm is slightly less complex than the merge sort example I saw, so when working with small sets of data, my sort may be more efficient even if goes a few levels deeper. I also avoided wasting lots of memory by passing my vectors by reference with the start and end positions, rather than making two smaller vectors for the left and the right and passing those.

I could cut a few levels of recursion by adding in a test to see if it's already sorted (so it wouldn't go ALL the way down - if everything was sorted at the 60th recursive call, it would terminate, instead of at the 66th)...the problem with that is then I am performing an extra 60 operations each time, so in this case it would only pay off if my function had more than 10 comparisons in it already.

What is the O() all about anyway? (sorry, like I said, never took analysis of algorithms). n is the number of elements that must be sorted right? log n seems way too small then.

Interesting stuff, anyway.
 

IcemanJer

Diamond Member
Mar 9, 2001
4,307
0
0


<< The BIG O refers to run time complexity(time) of a given algorythm. >>

more specifically, Big-O is the UPPER LIMIT of the rum time complexity. Big-Theta is THE run time complexity since it factors in both the upper bound and lower bound.
 

CSoup

Senior member
Jan 9, 2002
565
0
0


<<
What is the O() all about anyway? (sorry, like I said, never took analysis of algorithms). n is the number of elements that must be sorted right? log n seems way too small then.
>>



yes, n is generally the problem size which in this case is the number of elements. O(n log n) is the best that a classical sorting algorithm can do. You might want to try removing the median finding code in algorithm and try one fo the methods I mentioned earlier. You will likely get better performance on average. Basically, in a quicksort, each level you do about O(n) amount of work. This is why you want to minimize the number of levels of recursion (not number). In a worst case quicksort you split always with 1 element followed by the rest. This leads to a recursion tree of depth n which gives the worst case running time of O(n^2). If you split evenly then you have log n levels and thus O(n log n) running time.

Big-O notation is a formal way to specify the time complexity of an algorithm. Simple way to look at it is that it gives the upper bound complexity, so something in O(n) is also in O(n^2), but O(n) is a better indicator. Theta notation gives tight bounds and Sigma notation gives lower bounds. All of these come in little and big versions also.
 

CSoup

Senior member
Jan 9, 2002
565
0
0
Also, if you are going to do timing, you need to have at least 10-100 million elements to get significant times. Any sorting algorithm should be able to sort 64 elements in no time at all (except maybe random sort).
 

darkshadow1

Senior member
Nov 2, 2000
460
0
0
A randomized quicksort should give you the best average time (still n lg n but better than a regular quicksort)

If you have any other information / assumptions then you could do others like selection or radix...
 

Sir Fredrick

Guest
Oct 14, 1999
4,375
0
0


<< Also, if you are going to do timing, you need to have at least 10-100 million elements to get significant times. Any sorting algorithm should be able to sort 64 elements in no time at all (except maybe random sort). >>



That's why I'm not really going to bother optimizing my algorithm further, I'm just interested in refreshing my memory on what's out there
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |