|
-
Jan 24th, 2003, 12:01 AM
#1
Thread Starter
Junior Member
Best way to sort an array from least to greatest?
Whats the best way I could go about doing this? I know I'll have to go thru a loop to test whether each element is less than another, but Im kinda confused about how the algorithm would go. Thanks.
-
Jan 24th, 2003, 06:51 AM
#2
transcendental analytic
the generally fastest way is quicksort, insertion sort is useful for short arrays, heapsort, mergesort, radixsort, buckestsort and shellsort can be useful for certain things.
quicksort is a recursive algorithm, you start by choosing a pivot, usually the median of 3 (the leftmost, rightmost and middle elements), and then move all elements larger than the pivot right of the pivot and all elements smaller than the pivot left of it, then quicksort each of these unsorted partitions, as small arrays insertion sort is more efficient, you should use a cutoff at 10 elements when you switch over to insertionsort.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jan 24th, 2003, 06:58 AM
#3
Monday Morning Lunatic
In C++, easiest way is just to use the supplied tools:
Code:
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
int timmah[] = { 4, 3, 7, 2, 1, 4, 5, 8, 3 };
sort(timmah, timmah + 9);
}
std::sort works with any array-style STL container, or indeed a raw C-style array.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Jan 24th, 2003, 07:37 AM
#4
Frenzied Member
The qsort() function is used in C, std::sort() in C++.
Usually, because of the flexibility of these two, you do not have to write your own sort algorithm - unless you're in Prgoamming 101.
If that's the case, do a search here for bubble sort
-
Jan 24th, 2003, 08:21 AM
#5
What's insertion-sort? qsort() uses bubble-sort for the small remainders.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 24th, 2003, 08:30 AM
#6
transcendental analytic
I think you mean insertion sort, bubble sort is crazy.
insertion sort iterates from left to right, keeping the left partition sorted by inserting the next element at the right place:
6,3,7,9,1,2
6 | 3,7,9,1,2
3,6 | 7,9,1,2
3,6,7 | 9,1,2
3,6,7,9 | 1,2
1,3,6,7,9 | 2
1,2,3,6,7,9
the algorithm runs at quadratic time
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jan 24th, 2003, 01:58 PM
#7
Thread Starter
Junior Member
Thanks guys
I just used sort, contained in algorithm. It worked well enough for the simple project I was doing, so thanks again.
-
Jan 25th, 2003, 09:18 AM
#8
Originally posted by kedaman
the algorithm runs at quadratic time
Doesn't bubble do that too?
Anyway, VC++7's qsort uses insertion sort for small arrays, alright.
VC++6's too (is the same).
Hmm, I was quite sure they used bubble, don't know why.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 25th, 2003, 05:04 PM
#9
transcendental analytic
yep, but insertion sort is fastest of those and bubblesort needs tons of swaps to get anything done
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jan 25th, 2003, 07:05 PM
#10
Ok, so both have O(n^2) but insertion sort has the smalles O?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Jan 26th, 2003, 09:12 AM
#11
transcendental analytic
O() only tells you the type of proportionality so all terms with lower growth rate and all coefficients are discarded. I don't know the exact analysis for those sorts though..
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jan 26th, 2003, 11:43 PM
#12
Fanatic Member
Actually, the radiax sort has the best all round speed with a O(n). The actual efficiency, however, is dependent on computer speed and programming skill.
Insertion, Bubble, and selection sorts all have a O(N^2) which are great for simple problems.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|