|
|
|
|
![]() ![]() |
Mar 7 2005, 05:55 PM
Post
#1
|
|
|
Techno-Necromancer Group: Members Posts: 1,018 Joined: 13-January 05 From: The Net Member No.: 2,127 |
Okay, I'm not looking for code here, just the steps the algorithem would go through. I am trying to find the fastest algorithem to sort a list of elements. So far the fastest I have come up with is as follows (demonstrated on a five element list).
Step List Procedure 0 dbace this is the starting list 1 bdace compare spots 1 and 2, switch if necessary 2 bdace compare spots 5 and 4, switch if necessary 3 adbce compare spots 1 and 3, switch if necessary 4 adbce compare spots 5 and 3, switch if necessary 5 adbce compare spots 1 and 4, switch if necessary 6 adbce compare spots 5 and 2, switch if necessary 7 abdce compare spots 3 and 2, switch if necessary 8 abcde compare spots 3 and 4, switch if necessary 9 abcde compare spots 3 and 2, switch if necessary (this last step is needed occasionally even though it appears redundant) If you have a faster method or comments on this one, I'd love to see it. The purpose here is to find an algorithem that can be aplied to any language. |
|
|
|
Mar 11 2005, 05:01 PM
Post
#2
|
|
|
Way Out Of Control - You need a life :) Group: Members Posts: 1,366 Joined: 14-September 04 From: Nottingham England Member No.: 570 |
Looks confusing, the nodes you compare seem random ?
it would be hard and ocnfusing to actually implement that. Why not use Bubble sort ? although you are not looking for code, its easyer to explain with code... i hole you understand. CODE // sort characters in a string into alphabetical order (must be lovercase) const int MAX=10; char array[MAX]; bool chaned=true; //TODO: Code to fill array with MAX-1 random characters while(changed) { // (if changed is false, the list is already in order) changed=false; // reset variable for(int n=0; n<MAX-1; n++) { if( array[ n ] > array[ n+1 ]) { // TODO: swap the 2 characters over changed = true; (we made a change, so we are not in order) } } } // String is sorted ! |
|
|
|
Mar 11 2005, 05:57 PM
Post
#3
|
|
|
Techno-Necromancer Group: Members Posts: 1,018 Joined: 13-January 05 From: The Net Member No.: 2,127 |
The problem with Bubble sort is that it has the potential to take twice as long. If we assume that each step takes up one unit of time, then all of the checks add up and make the time twice as long. I do admit that Bubble sort has the potential to be very fast, and may actually be the best option. I'm still looking for a method with a definite short amount of time. And my method isn't random, it is the same principle as sort spot 1, then spot 2, then spot 3, et.c, except running from both ends at the same time.
|
|
|
|
Apr 5 2005, 08:55 AM
Post
#4
|
|
|
Member [ Level 1 ] Group: Members Posts: 34 Joined: 12-December 04 Member No.: 1,718 |
so what happens when the list is already sorted to start with?
|
|
|
|
Apr 5 2005, 11:27 AM
Post
#5
|
|
|
Techno-Necromancer Group: Members Posts: 1,018 Joined: 13-January 05 From: The Net Member No.: 2,127 |
For Bubble sort it stops early, and for my method, it goes through the steps and just doesn't switch anything. But I took another look at BubbleSort and continued the experimentation with my method, and for large numbers of items, my method quits being as effective, and with a couple modifications done to the assembly code BubbleSort becomes really fast and can take at most about 1.5 times as long instead of 2 times as long.
|
|
|
|
Apr 9 2005, 03:28 PM
Post
#6
|
|
|
Member [ Level 1 ] Group: Members Posts: 45 Joined: 9-April 05 Member No.: 3,791 |
I would suggest that you look into a sorting algorithm like quicksort. Bubblesort is actually a very slow algorithm, its running time is N^2 (N is the number of elements in the list). The running time for quicksort, on the other hand, is N log N. The only drawback of quicksort is it is a little more complicated to write. Here are the basic steps:
1. Choose one element at random 2. Split remaining elements into two groups as follows: Compare each remaining element with chosen one Put smaller elements to left of chosen one Put larger elements to right of chosen one 3. Repeat step 2 for all subgroups 4. When all groups have one element, they are in ascending order I'm sure that if search for quicksort on the interent you can find examples of it (and probably a better explaination than the one I gave). I hope this helps. |
|
|
|
Apr 10 2005, 09:10 PM
Post
#7
|
|
|
Techno-Necromancer Group: Members Posts: 1,018 Joined: 13-January 05 From: The Net Member No.: 2,127 |
Thanks, I knew bubblesort was slow, but the more I looked at the code the less I could figure out why. Your instructions actually made sense, so I don't think I will have to do an internet search.
~Viz |
|
|
|
Apr 22 2005, 11:51 PM
Post
#8
|
|
|
Member [ Level 2 ] Group: Members Posts: 52 Joined: 18-April 05 Member No.: 4,132 |
QUOTE(FearfullyMade @ Apr 9 2005, 11:28 AM) I would suggest that you look into a sorting algorithm like quicksort. Bubblesort is actually a very slow algorithm, its running time is N^2 (N is the number of elements in the list). The running time for quicksort, on the other hand, is N log N. The only drawback of quicksort is it is a little more complicated to write. Here are the basic steps: 1. Choose one element at random 2. Split remaining elements into two groups as follows: Compare each remaining element with chosen one Put smaller elements to left of chosen one Put larger elements to right of chosen one 3. Repeat step 2 for all subgroups 4. When all groups have one element, they are in ascending order I'm sure that if search for quicksort on the interent you can find examples of it (and probably a better explaination than the one I gave). I hope this helps. Is this basically a binary search based sorting algorithm? if so, that puts it at a running time of O(ln N) which is definately better than O(N^2) |
|
|
|
Apr 23 2005, 04:45 AM
Post
#9
|
|
|
Member [ Level 1 ] Group: Members Posts: 45 Joined: 9-April 05 Member No.: 3,791 |
Quicksort does have a lot in common with the binary search algorithim. The basic idea of both is splitting the list in half multiple times.
I can't remember for sure why quicksort is O(N In N) while binary search is O(In N). I believe it has something to do with the fact that with quicksort you have to compare items at each step and then move them around. I don't learn why algorithims like quicksort work like they do until next semester, so that might not be right. BTW, I noticed in my earlier post I used log in the running time of quicksort. I meant log base 2 when I put log. |
|
|
|
Apr 23 2005, 04:53 AM
Post
#10
|
|
|
Techno-Necromancer Group: Members Posts: 1,018 Joined: 13-January 05 From: The Net Member No.: 2,127 |
I figured you meant log base 2, we are after all, essentially dealing with binary. Thanks for all the help with this algorithm, I will probably have another algorithm question soon, os keep your eyes open.
~Viz |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 8th September 2008 - 04:15 PM |