Hi im trying to get a sort function working in a program im writing, basically ive got an array of structures of

struct person 
{
   char name[20];
   char address[20];
   char address2[20];
   char tphneno[12];
   char email[30];
};

and i want a function to sort them alphabetically by name in the form similar to this

void sort_person (struct person *p, int dcount)

where p is the pointer to the array of records and dcount is the total number of records...
Anyone get what im on about or any got any ideas to help me... ive tried bubble sorting but i think i did it wrong cos when i call the function it sorts but then just quits out of the program for no reason... if anyone can show me how to bubble sort it properly i would be grateful

Recommended Answers

All 7 Replies

>>it sorts but then just quits out of the program for no reason..
Nope. It quits because there are problems with the code you wrote. Please post the code so that we can help you.

The basic idea of sorting structures is the same as sorting an array of integers. The only difference is swapping structures instead of integers.

struct person temp = p[i];
p[i] = p[j];
p[j] = temp;

well here's one of the versions of bubble sort ive tried...

void sort_person(struct person *p, int dcount)
{
	struct person *tempo;
	int u, j;
	int min;
	
	for(u = 0; u < dcount; u++)
	{
		min=u;
		for(j = u+1; j < dcount; j++)
		{
			if(strcmp(p[min].name,p[j].name)>0)
			{		
				min = j;
				*tempo = p[u];	
				p[u] = p[min];
				p[min]= *tempo;
			}	
		}	
	}
}

well here's one of the versions of bubble sort ive tried...

void sort_person(struct person *p, int dcount)
{
	struct person *tempo;
	int u, j;
	int min;
	
	for(u = 0; u < dcount; u++)
	{
		min=u;
		for(j = u+1; j < dcount; j++)
		{
			if(strcmp(p[min].name,p[j].name)>0)
			{		
				min = j;
				*tempo = p[u];	
				p[u] = p[min];
				p[min]= *tempo;
			}	
		}	
	}
}

I wouldn't suggest using bubble sort either, O(N^2) is not what I'd call friendly :). Although if this is for school, I'm sure it (might) be fine. And that doesn't appear to be bubble sort, instead it's an almost working version of selection sort.

Any reason you can't/don't want to use the built-in qsort(), and just write a comparison function?

Or, just use good old plain selection sort:

algorithm sort(P)
   for each person in P
      min = findMin(person)
      swap(person, min)
   end

P findMin(From P)
   min = P     
   for each person from P + 1
      if(person < min)
        min = person
   end

   return min

Of course I'm not giving you the answer ;)

your program crashes because line 3 is a pointer that is not allocated. Remove the stars on lines 3, 15 aned 17.

>>your program crashes because line 3 is a pointer that is not allocated. Remove the stars on lines 3, 15 aned 17.

jees i cant believe that i didnt try that, it must have been the only combination of stars i didnt try... well it seems to be working. sorry if that was obvious, ive only been c porgramming for bout 2 weeks and this is for a uni project lol. Thanks for your quick responses btw, life savers :)

Not to worry. Been there and done that. It often helps to have another pair of eyes look at a program.

hmm now only thing thats bugging me is when i call free(p); at the end of the program it just crashes, it works fine for everything else in the prog, only after ive used the sort function does it crash. There isnt any need for a change of memory allocation after a sort is there?

EDIT:
Scratch that, its crashing after my load data function.. not sure why though... ill get back to ya ^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.