473,405 Members | 2,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Sorting struct array

I'm trying to sort an array of structs. My code looks something like this:

Expand|Select|Wrap|Line Numbers
  1. struct Foo
  2. {
  3.     int number;
  4. };
  5.  
  6. int main()
  7. {
  8.     int i = 14;
  9.     Foo *floo = new Foo[i];
  10. }
  11.  
Are there any functions for this kind of sorting in the standard library? I've found a code for bubble sort, but I'm not sure how to pass a struct array to it.

Expand|Select|Wrap|Line Numbers
  1. void bubble_sort(apvector <int> &array)
  2. {
  3.       int i, j, flag = 1;
  4.       int temp;
  5.       int arrayLength = array.length( ); 
  6.       for(i = 1; (i <= arrayLength) && flag; i++)
  7.      {
  8.           flag = 0;
  9.           for (j=0; j < (arrayLength -1); j++)
  10.          {
  11.                if (array[j+1] > array[j])
  12.               { 
  13.                     temp = array[j];
  14.                     array[j] = array[j+1];
  15.                     array[j+1] = temp;
  16.                     flag = 1;
  17.                }
  18.           }
  19.      }
  20. }
  21.  
This is the code I found, but as I said, I don't know how to pass a struct array to it. If I try floo[j+1].number I get an error.
Also, I don't want to rearrange them sorted (like in the code above) but put them sorted into another array.
Feb 5 '07 #1
2 3768
willakawill
1,646 1GB
Hi. You should copy to the array that you want to be sorted and pass that to your sort algorithm. This is a version of heap sort that I have adjusted to fit the struct you used as an example. This will work for any array of any struct. You just need to replace the type, Foo, with the struct type and the element, number, with the struct element that you wish to sort with.
Expand|Select|Wrap|Line Numbers
  1. void HeapSort(Foo* list)
  2. {
  3.    long size = sizeof(list) / sizeof(list[0]);
  4.    Foo tmp;
  5.  
  6.    for(long i = size/2; i > 0;) Heapify(list, --i, size);
  7.  
  8.    for(long i = size-1; i>0; --i)
  9.    {
  10.       tmp = list[0];
  11.       list[0] = list[i];
  12.       list[i] = tmp;
  13.       Heapify(list, 0, i);
  14.    }
  15. }
  16.  
  17. void Heapify(Foo* A, long i, long heapsize)
  18. {
  19.    while(true)
  20.    {
  21.       long l = 2*i+1;
  22.       long r = 2*i+2;
  23.  
  24.       long largest = (l < heapsize && A[i]->number < A[l]->number) ? l : i;
  25.  
  26.       if(r < heapsize && A[largest]->number < A[r]->number)
  27.          largest = r;
  28.  
  29.       if(largest != i)
  30.       {
  31.          foo tmp;
  32.          tmp = A[i];
  33.          A[i] = A[largest];
  34.          A[largest] = tmp;
  35.          i = largest;
  36.       }
  37.       else break;
  38.     }
  39. }
Feb 5 '07 #2
Sorry for such a late reply. Just wanted to say thanks, it worked.
Feb 8 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: J. Campbell | last post by:
I'm having a problem sorting an array of structs that is a class member. After doing a bit of research I thought I had my hands around the problem. I've overloaded the '<' operator for the...
3
by: darth | last post by:
Does anyone have heap sorting with queue(doublely linked list) in C? I have heap sort with array but with queue, it's somewhat hard.
3
by: b83503104 | last post by:
In matlab, the sort function returns two things: =sort() a = 5 7 8 b = 1 3 2 where a is the sorted result, and b is the corresponding index. Is there C or C++ code...
2
by: Simon Morgan | last post by:
I hope this isn't OT, I looked for a newsgroup dealing purely with algorithms but none were to be found and seeing as I'm trying to implement this in C I thought this would be the best place. I...
25
by: Allie | last post by:
How would I go about sorting this structure by title? typedef struct { char author; char title; char code; int hold; int loan; } LIBRARY;
6
by: Index | last post by:
Hi, I have a 4 dimensional array and I want to sort it by the first two columns emulating an ORDER BY clause to some extent.Any suggestion? The original Array is: Row1-> 4 8 2 9 Row2-> 4 5 5...
4
by: rushik | last post by:
Hello all, I am using structure in my program, and my aim is to sort this structure based on some optimized sorting algo. structure is struct data { int account;
7
by: Steve Bergman | last post by:
I'm involved in a discussion thread in which it has been stated that: """ Anything written in a language that is 20x slower (Perl, Python, PHP) than C/C++ should be instantly rejected by users...
1
by: arnuld | last post by:
On Mon, 29 Sep 2008 15:16:56 +0100, Ben Bacarisse wrote: An abstract overview of my own program by Ben had helped me focus on the design issue first. I came to know that whole problem went...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.