473,800 Members | 2,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sorting struct array

5 New Member
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 3795
willakawill
1,646 Top Contributor
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
vikerneso
5 New Member
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
4980
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 struct, and then I pass sort a pointer to the first array element and the number of elements. Everything compiles fine until I try to call std::sort() on the array. I'm trying to make a class that acts like a dealer to use in a card game, and I need...
3
7938
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
7191
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 available to achieve this? Thanks
2
2133
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 have an array of structs containing data which I'd like to output ordered based on the value of a single member. I was wondering if there is a relatively simple way of doing this without actually modifying the structure of the array? I had a...
25
11395
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
3542
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 3 Row3-> 3 9 8 7 Row4-> 8 2 4 8
4
2174
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
1925
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 on those grounds alone. """ I've challenged someone to beat the snippet of code below in C, C++, or assembler, for reading in one million pairs of random floats and
1
2428
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 into the wrong direction as compared to what was intended. I think I am working on this program from last 1 month and it is still not done, Here is the intent:
0
9691
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10507
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10255
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10036
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9092
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5473
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4150
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.