• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

Sorting an Array

In VC++ 6.0 if I have a large (approx 62k element) Carray and I wish to sort it, what is the most efficient way?  (Either a built in funcion of C++ I've missed or some sort of sorting algorith)

At the moent the program just goes through, gets teh smallest element, goes though, gets the next smallest etc... which was fine for only a few record but really slows things down where there are lots.

0
syzygy
Asked:
syzygy
  • 6
  • 4
  • 2
  • +2
1 Solution
 
Bill NolanOwner, Lead Technology ProgrammerCommented:
See what you can find on algorithms like Quicksort or Bubblesort.  Sorry I don't have the time at the moment to paste some example code here, but hopefully you can go from there.
0
 
_lychee_Commented:
C has this generic purpose quicksort routine... it's qsort declared in stdlib.h
0
 
nietodCommented:
>> what is the most efficient way?
Then a bubble sort would not be a good answer.  A quick sort would be good.

Slimfinger, do you really think that is a helpful answer?  Basically "There are ways to do it, go look for them."  If you don't have time to answer properly, post a comment, not an answer.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
SteveGTRCommented:
Here's some code I got from a class on Codeguru and modified for my own use in a static function:

#define STRIDE_FACTOR         3

void SortStrArray(CStringArray* pArray)
{
  BOOL bFound;
  int iInner, iOuter, iStride = 1;
  CString str1, str2;
      
  while (iStride <= pArray->GetSize())
    iStride = iStride * STRIDE_FACTOR + 1;
      
  while (iStride > (STRIDE_FACTOR - 1))
    {
    iStride = iStride / STRIDE_FACTOR;

    for (iOuter = iStride; iOuter < pArray->GetSize(); iOuter++)
      {
      bFound = FALSE;
      iInner = iOuter - iStride;

      while ((iInner >= 0) && !bFound)
        {
        str1 = pArray->GetAt(iInner);
        str2 = pArray->GetAt(iInner+iStride);

        if (str2 < str1)
          {
          pArray->SetAt(iInner+iStride, str1);
          pArray->SetAt(iInner, str2);
          iInner -= iStride;
          }
        else
          bFound = TRUE;
        }
      }
    }
}

I hope this helps you.

Steve
0
 
Bill NolanOwner, Lead Technology ProgrammerCommented:
NIETOD,
Yes, I DO think that is a proper answer.  Considering he seemed to have no idea what to look for, and those algorithms can be found pretty easily.  It certainly was A LOT more help than what you provided him.  I'm new here, and I only tried to help.  Note: SteveGTR left a rather qualified "answer", and labeled it as a comment.  Go after him!  You need a life.
0
 
syzygyAuthor Commented:
I'm going to give lychee the points, since his is the solution I used.

SLim: Steve had to post his response as a comment, there can only be one proposed answer at a time.
0
 
nietodCommented:
Slimfinger, if you are new to EE I recommend you spend a few days monitoring questions and refrain from answering.  I don't think anyone could argue that was not an appropriate answer for this site.  After reading some questions, I think you'll agree.

And the reason I didn't provide more information was that it wasn't needed, _lychee_, had done so.  Although I did point out that a bubble sort was a poor choice compared to the q-sort, which was not explicitly stated in _lychee_'s comments.
0
 
Bill NolanOwner, Lead Technology ProgrammerCommented:
a) ONE click on the search button in Vis Studio and entering "Quicksort" leads you DIRECTLY to the qsort algorithm (the accepted solution).  Just *ONE* click.  Think about it. So, unless I'm blind, merely suggesting quicksort doesn't seem too far out of bounds.
Shame on me.

b) From what I can see, syzygy can just as easily award points to a contributed comment.

c) NIETOD - "refrain from answering"??? Just what is your point in here, anyway?  If the style doesn't quite fit, I will see that soon enough with no harm done.  In the meantime, I prefer to learn and to teach, not whatever you are doing.
0
 
nietodCommented:
a) syzygy, didn't seem to think that "quicksort" helped.  Perhaps he doesn't have enough experience with VC to do the search?  

>> b) From what I can see, syzygy can just as
>> easily award points
Note really.  When the question is locked that option is unavailble.

When you are locking a question, you are making a commitment to the client to help them to the best of your abilities.  If you know you can't help them sufficiently--due to inexperience, time constraints, or any other reason, you should not answer.  You can post a comment in that case.  That's why they are there.  And if the comment is actually sufficient to help the client, they can then award the points to you.  but if not, someone else can answer.

>> I will see that soon
>> enough with no harm done
You will see what?  If you don't see a problem in this question, what will it take?

>> not whatever you are doing
What is that?

Take a look around here!  Answer if you want, but please read!
0
 
Bill NolanOwner, Lead Technology ProgrammerCommented:
Well, anyway, thank you for explaining a bit of the SOP...
0
 
nietodCommented:
SOP?

It took me 6 months to figure out "AFAIK" , what's "SOP"?
0
 
Bill NolanOwner, Lead Technology ProgrammerCommented:
"Standard operating procedure."  Never heard "AFAIK" myself.  (At least, not as far as I know.)
0
 
_lychee_Commented:
lol... was that supposed to be corny?=P

AFAIK = as far as i know, which u used in the same post...
0
 
Bill NolanOwner, Lead Technology ProgrammerCommented:
Maybe I should have included a drumroll.
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 6
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now