Efficient yet simple sorting algorithms

Sir Fredrick

Guest
Oct 14, 1999
4,375
0
0
As some of you know (thanks to my thread about typos), I recently had to develop an algorithm in C++ to sort a vector. The only sorting algorithm I could remember from data structures was bubblesort, which is horribly inefficient. I know that quicksort is pretty good, but I couldn't remember how it works, so I sort of just made up my own. It seems to be pretty good, when sorting 64 elements, it makes 66 recursive calls (the worst-case and best-case are the same). For my purposes, that's good enough, but what are some better sorting algorithms that aren't too complicated? Any links to good online explanations of them would be cool too.
 

littleprince

Golden Member
Jan 4, 2001
1,339
1
81
64 recursive calls that go through your vector?
so n+2 squared

how bout a merge sort?
i'm sure a google search would help
nlogn by the way
 

Sir Fredrick

Guest
Oct 14, 1999
4,375
0
0
Why squared? The vector is size 64, the function was executed 67 times (if you count the first one), so isn't that just n+3?
I never took analysis of algorithms
 

Sir Fredrick

Guest
Oct 14, 1999
4,375
0
0
Here's what I did:
select a pivot point (an element in the middle), move everything smaller than that value to the left, everything larger than it to the right, break that in half, and call the same function recursively on the left and right sides.
 

CSoup

Senior member
Jan 9, 2002
565
0
0


<< Here's what I did:
select a pivot point (an element in the middle), move everything smaller than that value to the left, everything larger than it to the right, break that in half, and call the same function recursively on the left and right sides.
>>



That gives you an implementation of the quicksort algorithm.
Best and average case running time is O(n log n)
Worst case running time is O(n^2)

Most implementations of quicksort stop after the partitions are of some small size (like 10) and does insertion sort from their because of this bad behavior on mostly sorted lists.

Oh, and by the way, O(n log n) average case is the best that a classical sort algorithm can do.
 

hwstock

Senior member
Oct 7, 2001
254
0
0


<< As some of you know (thanks to my thread about typos), I recently had to develop an algorithm in C++ to sort a vector. The only sorting algorithm I could remember from data structures was bubblesort, which is horribly inefficient. I know that quicksort is pretty good, but I couldn't remember how it works, so I sort of just made up my own. It seems to be pretty good, when sorting 64 elements, it makes 66 recursive calls (the worst-case and best-case are the same). For my purposes, that's good enough, but what are some better sorting algorithms that aren't too complicated? Any links to good online explanations of them would be cool too. >>



For small sorts, bubblesort isn't bad. You don't have to remember much about quicksort -- it is part of the standard C library. You merely need supply (quicksort) a pointer to a function that compares two of the items to be sorted.
 

CyberZenn

Senior member
Jul 10, 2001
462
0
0
ive never heard of a quicksort, but in this sort of situation i believe binary sorts are the fastest. O(log n) if i recall correctly - its hard to get a more effecient alg. than that, and if you manage to find one you can probably be inducted into the computer science hall of fame. or something.
 

CSoup

Senior member
Jan 9, 2002
565
0
0
here is a page that explains quicksort with an animation. It does not do a running time analysis though, but the analysis of quicksort is interesting because its worst case and average case differ so much.
 

CSoup

Senior member
Jan 9, 2002
565
0
0
Are you sure the best case number of recursive calls is 66? Your description of the algorithm seems to indicate a best case number of recursions of O(log n). This is if the pivot chosen is very near the middle.
 

Sir Fredrick

Guest
Oct 14, 1999
4,375
0
0


<< Are you sure the best case number of recursive calls is 66? Your description of the algorithm seems to indicate a best case number of recursions of O(log n). This is if the pivot chosen is very near the middle. >>



Well since I don't check to see if the list is sorted at any given point, I should have about the same number of recursive calls no matter what, right? I could be wrong, I'll verify on monday...
 

breakit23

Golden Member
Oct 9, 1999
1,741
0
0
Are you in AP comp science? I had the same assignment.

Their are a bunch of sorts you can do: Shell sort, merge sort, Insert sort, or select Sort. You can search for them online or just check sn
 

lebe0024

Golden Member
Dec 6, 2000
1,101
0
76
Quicksort is VERY simple and VERY fast. Java uses quicksort for their "javasort()" function.

Complexity:

Worst: O(n^2).

Average: either (n lg n) or Theta(n lg n), I can't remember.
 

lebe0024

Golden Member
Dec 6, 2000
1,101
0
76
Merge sort would be fastest, but an array can't be sorted within itself, it needs other arrays as storage. Quick sort is the fastest, with Heapsort coming in a close second. You don't want to use heapsort though, it's incredibly complicated.
 

Sir Fredrick

Guest
Oct 14, 1999
4,375
0
0
Ok, I just did a best-case on paper, and I get 63 calls, including the original. That's pretty close to 66.

Here's what I did, each line is another recursive call:

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32
33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64

1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16
17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32
33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48
49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64

1,2,3,4,5,6,7,8
9,10,11,12,13,14,15,16
17,18,19,20,21,22,23,24
25,26,27,28,29,30,31,32
33,34,35,36,37,38,39,40
41,42,43,44,45,46,47,48
49,50,51,52,53,54,55,56
57,58,59,60,61,62,63,64

1,2,3,4
5,6,7,8
9,10,11,12
13,14,15,16
17,18,19,20
21,22,23,24
25,26,27,28
29,30,31,32
33,34,35,36
37,38,39,40
41,42,43,44
45,46,47,48
49,50,51,52
53,54,55,56
57,58,59,60
61,62,63,64


1,2
3,4
5,6
7,8
9,10
11,12
13,14
15,16
17,18
19,20
21,22
23,24
25,26
27,28
29,30
31,32
33,34
35,36
37,38
39,40
41,42
43,44
45,46
47,48
49,50
51,52
53,54
55,56
57,58
59,60
61,62
63,64
 

charrison

Lifer
Oct 13, 1999
17,033
1
81
umm..you can let c++ do all the heavy lifting.
Include <algorythms> and write the compare function then you can

sort(vector.begin(),vector.end(),sortfunction);

almost too easy
 

Sir Fredrick

Guest
Oct 14, 1999
4,375
0
0


<< umm..you can let c++ do all the heavy lifting.
Include <algorythms> and write the compare function then you can

sort(vector.begin(),vector.end(),sortfunction);

almost too easy
>>



Sounds too good to be true. I'll have to look into that. Thanks.

Some of those other references look good too, I'm going to bookmark this stuff in case I need it at a later date, rather than trying to reinvent the wheel every time.
 

CSoup

Senior member
Jan 9, 2002
565
0
0


<<

<< Are you sure the best case number of recursive calls is 66? Your description of the algorithm seems to indicate a best case number of recursions of O(log n). This is if the pivot chosen is very near the middle. >>



Well since I don't check to see if the list is sorted at any given point, I should have about the same number of recursive calls no matter what, right? I could be wrong, I'll verify on monday...
>>



Opps, I actually meant that the best and average case number of levels of recursions should be O(log n). This would happen in the case you show where you are splitting perfectly in the middle each time.

Normally if you run quicksort on the list you gave the performance will be really horrible since when it splits, it will normally put 1 item in the first half and the rest in the second half. This is because the basic implementation just takes the first number as the pivot. The reason for this is that it works well normally and runs faster then if you try to find the true median every time. Other modifications are to randomly pick a pivot, to take first, last, and middle elements from the list and average them, etc. People usually never find the true median like you are doing. In any case, yes, the number of recursions is about the same in all cases, but the level of recurions in worse case is O(n) and in best case is O(log n). Running time analysis is a little more complex than looking at the number of recursions because another factor is how much work you are doing in each recursion. If you want a more in-depth analysis of quicksort and other sorting algoritms, look in Introduction to Algorithms 1st ed by Cormem, Leiserson, and Rivest.
 
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/    |