Results 1 to 22 of 22

Thread: Multi-Dimensional Array Sorting

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Burlington, ON, Canada
    Posts
    99

    Multi-Dimensional Array Sorting

    I'm tryring to sort a [300][2] array based on the data held in positon 2 of the array.

    This is the routine I have come up with but it's a no go. Any help would be great.

    PHP Code:
    for (i=1;i>=MAXARRAY-1;i++) {
              for(
    i2=0;i2<MAXARRAY;i2++) {
                   if(
    iTF[i2][1] > iTF[i2 1][1]) {
                        
    iTemp iTF[i2][0];
                        
    iTemp1 iTF[i2][1];
                        
    iTF[i2][0] = iTF[i2 1][0];
                        
    iTF[i2][1] = iTF[i2 1][1];
                        
    iTF[i2 1][0] = iTemp;
                        
    iTF[i2 1][1] = iTemp1;
                   }
              }


  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I assume this array is of type int
    C:
    Code:
    #include <stdlib.h>
    
    intl qshelp(void *p1, void *p2)
    {
      int i1 = *(((int*)p1)+1);
      int i2 = *(((int*)p2)+1);
      if(i1 < i2) return -1;
      if(i1 == i2) return 0;
      return 1;
    }
    
    // sort the array:
    qsort(array, 300, qshelp);
    C++:
    Code:
    #include <algorithm>
    using std::sort;
    
    class pred_s2dar {
      bool operator()(int *ar1, int*ar2) {
        return (ar1[1] < ar2[1]);
      }
    };
    
    // sort the array:
    sort(array, array+300, pred_s2dar());
    The advantage is that both methods use the quicksort algorithm, which is much faster than bubble sort.
    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.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Location
    Burlington, ON, Canada
    Posts
    99
    That's all fine and dandy, but I want to know what is actually wrong with my bubble sort code. My program win't even go into the routine, it first line is wrong in someway i believe.

    Thx,
    Steve

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    i=1;i>=MAXARRAY-1;i++

    You need a < here.

    And C/C++ arrays begin idexing with 0, so you have to change your code to watch for that.
    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.

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I thought std::sort used a merge sort?
    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

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I don't know. MSDN says nothing about theirs and I don't have access to a C++ standard. It might depend on the implementation. VC++7's sort takes O(N log N), maybe you can figure it out...
    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.

  7. #7
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Code:
    // sort the array:
    qsort(array, 300,sizeof(int), qshelp);
    qsort has 4 arguments.

  8. #8
    Frenzied Member
    Join Date
    Jul 2002
    Posts
    1,370
    Oh FWIW - consider using builtin routines for sorting. Like the ones CB mentioned.

    Bubble sorts are great for classwork, but get you nowhere in the real world. We have to sort our 'ad hoc' data extractions that routinely have 30-60 million records in each of 12 files. If we used the bubble sort, the ad hoc for January 2002 would just now be finishing....



    We use the HPUX sort package. Even then, it takes quite a while and uses 20GB+ of disk. When we have yearend files, they get sorted on the mainframe.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Yes, bubble sort is very easy to understand and implement, but it has the worst performance of all sorting algorithms I know.
    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.

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    It beats quick sort on mostly-sorted data. For example, you have a dataset that's unsorted, use quick sort or something to get it into order, then you can use a bubble sort once you've made any changes you want to keep it sorted.
    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

  11. #11
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192
    You are right, Parksie... It also depends on how many items you have to sort. That is why there is even the knut rule for the Shell Sort.

    Anyway... I was here again searching for code for translating the Automaton program. (Yes, I have translated the class State so far and now I am translating the class Automaton) and I need to sort a vector of strings. And yes, I found "algorithm.h" but the example i found was using the size of the string to sort it. How can I sort it alphabetically?

    I tried the following:

    PHP Code:
    #include <string>
    #include <vector>
    #include <algorithm>

    // compare strings by length, descending order
    bool  compare(string const& xstring const& y)  {
        return 
    y//x.size() > y.size();
    }

    void SortVector(vector<stringV){
        
    sort(V.begin(), V.end(), compare);

    What am I doing wrong? I don't want to program the Flagged-Bubble Sort... I already programmed MID, InStr and all those functions I would use on VB...
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  12. #12
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192
    Oops.. I found one post by Parksie that showed me that...

    But now my question is... Why if I make it a procedure it won't sort it but when I do it just like that it sorts it?

    Example:


    PHP Code:
    #include <string>
    #include <vector>
    #include <algorithm>

    void SortVector(vector<stringV){
        
    sort(V.begin(), V.end());
    }

    void main(){
        
    vector<stringStates;
        
    States.insert(States.end(), "s5");
        
    States.insert(States.end(), "s4");
        
    States.insert(States.end(), "s3");
        
    States.insert(States.end(), "s2");
        
    //This is an example, so don't think I had it this way.
       //I just did not want to add the GetArrows code.
       
    SortVector(States); //This won't sort it
       
    sort(States.begin(), States.end()); //This will work


    So why doesn't it work if it is called like I did? I mean, it is easier for me to just give the vector I want to sort it and then sort it with a procedure like that. But I had to do it the other way... I am just curious why wouldn't this approach work...

    PS: Which is the most comfortable way for you guys to see the code?
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Look very carefully at your argument list for SortVector

    Also, why add another function just to "rename" sort() ?
    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

  14. #14
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Actually, less<> with strings should sort lexicographically...
    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.

  15. #15
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192
    True, Parksie it should be:
    Code:
    void SortVector(std::vector<string> V){
    
        sort(V.begin(), V.end());
    
    }
    But I am not using it... Thank you anyway..
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  16. #16
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Heh, nope. Still wrong. Try:
    Code:
    void SortVector(std::vector<string> &V){
    
        sort(V.begin(), V.end());
    
    }
    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

  17. #17
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192
    I will, but why...? Is it like "ByRef" and "ByVal" that is in VB?
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  18. #18
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Basically, yes.

    The & tells it to pass that argument by reference, hence when you do something to V, it acts on the original V. If you had it as const, then you would not be able to change it, and the effect would be of pass-by-value, except without the copying overhead (hence why const references are used so much in C++).

    You can use * to pass by pointer (references do not exist in C, so pointers are the only way to achieve this same effect). This requires "dereferencing" that pointer before use, with the * operator that I'm sure you're familiar with. You can use const in C++ with this as well.

    No modifiers passes by value, and will copy the entire class/structure/type before the function starts. Alterations are permitted, but are fairly useless most of the time because they don't affect the caller.
    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

  19. #19
    Frenzied Member Tec-Nico's Avatar
    Join Date
    Jun 2002
    Location
    México
    Posts
    1,192
    Oh... Well, thank you.

    Now I understand. I thought something like that might be happening. Please be patient to me... My C++ teacher was an ass and he never taught us anything. So all I know is because of practice. Not like on VB that I could take the book and grasp it quickly (It is because I used to work with Basic when I was in junior High and they did not teach us C nor C++ )
    We miss you, friend... Rest in Peace, we will take care of the rest of it.

    [vbcode]
    On Error Me.Fault = False
    [/vbcode]
    - Silence is the human way to share ignorance
    Tec-Nico

  20. #20
    Addicted Member
    Join Date
    Oct 2002
    Posts
    241
    what does lexicographically mean?

  21. #21
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Dictionary-style, basically.

    Lexicon == dictionary. The 'lex' prefix is what determines it, I think.
    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

  22. #22
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    latin lex, legis (f.)
    1) a set form of words , contract, covenant, agreement
    2) a law, rule
    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.

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