Results 1 to 12 of 12

Thread: Best way to sort an array from least to greatest?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2002
    Location
    Kentucky
    Posts
    29

    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.

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  4. #4
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    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

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Sep 2002
    Location
    Kentucky
    Posts
    29

    Thanks guys

    I just used sort, contained in algorithm. It worked well enough for the simple project I was doing, so thanks again.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  11. #11
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  12. #12
    Fanatic Member
    Join Date
    Jan 2003
    Posts
    1,004
    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
  •  



Click Here to Expand Forum to Full Width