Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Need Help With Sorting
Index -> Programming, C -> C Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
AnubisProgrammer




PostPosted: Wed Sep 19, 2007 5:43 pm   Post subject: Need Help With Sorting

I know this is really basic, but for the life of me I can't get a sort function to work. I came from c++ and every sort function I find for C seems to be complex or it doesn't work. I'm only looking to sort an array of 25 ints. Is there a simple way to do this? I'm losing my mind trying to get this to work.
Thanks
Sponsor
Sponsor
Sponsor
sponsor
Mazer




PostPosted: Wed Sep 19, 2007 5:51 pm   Post subject: RE:Need Help With Sorting

It might help if you tell us more about the problems you've been having. If you just want an easy sort to implement just check out the bubble sort.
md




PostPosted: Wed Sep 19, 2007 9:50 pm   Post subject: RE:Need Help With Sorting

If your looking to implement a sort and your having trouble then show us your code and we'll see what we can do.

If your looking for an already implemented sort then I'd recommend std::sort(). To sort a list of 5 integers it'd look something like this:

c++:

 int array[] = { 23, -1, 9999, 0, 4 };
 unsigned int array_size = 5;           

 cout << "Before sorting: ";
 for( unsigned int i = 0; i < array_size; i++ ) {
   cout << array[i] << " ";
 }
 cout << endl;           

 sort( array, array + array_size );             

 cout << "After sorting: ";
 for( unsigned int i = 0; i < array_size; i++ ) {
   cout << array[i] << " ";
 }
 cout << endl; 
. (Shamelessly stolen from cppreference.com)
AnubisProgrammer




PostPosted: Wed Sep 19, 2007 10:06 pm   Post subject: Re: Need Help With Sorting

Ya, sorry for not being more specific, but I was immensely frustrated with my program. I'm making a deal or no deal program for class. I moved all the "cases" that the user hasn't chosen into an array so I can display what values they can still pick. So there's really no code, just an array that I have a few values in.
I have an array named "display" that's being filled with the same values from another array. One has to be sorted, one has to remain random.

the array named "suitcase" has the values already in it.

do{
display[counter]=suitcase[counter];
counter++;
}while(counter!=26);

Then I want it to sort the array named display.

I tried the code you left for me but I keep getting an "Error 11 error LNK2019: unresolved external symbol _sort referenced in function _main assign1b.obj"
Am I missing a library or something?
Mazer




PostPosted: Wed Sep 19, 2007 10:38 pm   Post subject: RE:Need Help With Sorting

You can't use the code md posted because it is for C++ and, as you have mentioned, you are using C.
Have you implemented a sorting algorithm before?
Did you look at the bubble sort article on Wikipedia?
md




PostPosted: Thu Sep 20, 2007 11:00 am   Post subject: RE:Need Help With Sorting

Whoops! I totally missed that; however IIRC there is a standard sort() function for C too

c:

/* qsort example */
#include <stdio.h>
#include <stdlib.h>

int values[] = { 40, 10, 100, 90, 20, 25 };

int compare (const void *elm1, const void *elm2)
{
  return ( *(int*)elm1 - *(int*)elm2 );
}

int main ()
{
  int n;
  qsort (values, 6, sizeof(int), compare);
  for (n=0; n<6; n++)
     printf ("%d ",values[n]);
  return 0;
}


The only weird part in that is compare, which is a function that takes too void pointers, casts them as integers, and then returns a positive number, zero, or negative number.

To quote http://www.cplusplus.com/reference/clibrary/cstdlib/qsort.html
Quote:
The return value of this function should represent whether elem1 is considered less than, equal, or grater than elem2 by returning, respectivelly, a negative value, zero or a positive value.


And yes, if you actually want to learn how to sort I too recommend looking up bubble sorts; wikipedia has a particulalry good page on them.
AnubisProgrammer




PostPosted: Thu Sep 20, 2007 12:31 pm   Post subject: Re: Need Help With Sorting

Thank you very much, it's up and working now. Now I can get to finally finishing this project up. Thanks a ton.

ps.
I checked out that wiki page on bubble sort and thanks for the tip, it helped me understand a bit more of what I was doing here.
md




PostPosted: Thu Sep 20, 2007 1:13 pm   Post subject: RE:Need Help With Sorting

The code I posted actually uses a sort called Quick Sort; it's significantly faster then a bubble sort in most cases. However it's also much more complicated which is why you should learn bubble sorts first.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Thu Sep 20, 2007 2:38 pm   Post subject: RE:Need Help With Sorting

Actually, md, it would be correct to state that it casts them to integer pointers, then dereferences those pointers.

But beware that you are getting into the hairy realm of void pointers. Smile
md




PostPosted: Thu Sep 20, 2007 7:36 pm   Post subject: RE:Need Help With Sorting

Yes, you're right; I just skipped the pointer dereferencing bits since (to me) they are obvious. It's too bad this isn't C++ since a compare function is really somewhere that templates are really quite useful.
wtd




PostPosted: Thu Sep 20, 2007 7:42 pm   Post subject: RE:Need Help With Sorting

History has proven at least one thing: pointers are really frickin' confusing for beginners.
Display posts from previous:   
   Index -> Programming, C -> C Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: