Using quicksort to alphabetize names?

Quicksort a struct alphabetically

Page 1 of 1

5 Replies - 6981 Views - Last Post: 05 May 2010 - 11:25 AM Rate Topic: -----

#1 Guest_BB*


Reputation:

Using quicksort to alphabetize names?

Posted 05 May 2010 - 01:29 AM

I have a struct consisting of employee names and their data. I would like to sort each of their records by name in alphabetical order. How would I do this?

My struct for the employees:
typedef struct emprecord
{
char fullname[20];
float hours;
float deferred;
float payrate;
float reghrs;
float gross;
float fedtax;
float ssitax;
float reggross;
float netpay;
float statetax;
float ovthrs;
float ovtgross;
} emprecord;


Is This A Good Question/Topic? 0

Replies To: Using quicksort to alphabetize names?

#2 SigurdSuhm  Icon User is offline

  • D.I.C Head

Reputation: 18
  • View blog
  • Posts: 111
  • Joined: 05-August 08

Re: Using quicksort to alphabetize names?

Posted 05 May 2010 - 03:58 AM

In this case I would probably recommend that you take a look at the strcmp() function from the standard C library. It can be found in the header string.h or just cstring if you're using C++.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_BB*


Reputation:

Re: Using quicksort to alphabetize names?

Posted 05 May 2010 - 04:23 AM

Yeah, I was thinking about doing that, too, but I need to sort out all of the records with the employee data attached.

I.E.

Doe, John 50.00 100.00 999.00
25.00 1.00 0.00

etc etc...

I'm not sure how I'd go about formatting each string to exactly fit format specifications, using strcmp or strcpy.

Anyway, I've been searching about for the past 4 hours for any kind of sample code that could help me, or some way I can just substitute what I have into the code..

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "./employeerecord.h"

void quicksort(emprecord employee[], int *left, int *right)
{  
	int pivot=0;
	int *r = right, *l = left, t = *l;
	if (right > left) 
	{    
		pivot = left[(right-left)/2];        
		do 
		{      
			while (*l < pivot) l++;      
			while (*r > pivot) r--;      
			if (l <= r) 
			{                
				*l++ = *r;        
				*r-- = t;      
			}    
		} 
		while (l <= r);    
		quicksort(emprecord employee[], left, r);    
		quicksort(emprecord employee[], l, right);  
	}
		{  
			quicksort(emprecord employee, emprecord employee+i-1);
		}
}


So far, not looking too good...
Was This Post Helpful? 0

#4 Guest_BB*


Reputation:

Re: Using quicksort to alphabetize names?

Posted 05 May 2010 - 04:25 AM

Oh, and the formatting requires decimal places be aligned.
Was This Post Helpful? 0

#5 snoopy11  Icon User is online

  • Engineering ● Software
  • member icon

Reputation: 766
  • View blog
  • Posts: 2,228
  • Joined: 20-March 10

Re: Using quicksort to alphabetize names?

Posted 05 May 2010 - 11:20 AM

All you have is pseudo code.

I will give you a working example in c++
there is a function called qsort which is a quicksort function.

My example will be for strings not structs you will have to work that out for yourself

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace std;

//functions
void print_cstring_array(char **array, size_t len);
int cstring_cmp(const void *a, const void *B)/>;

//main program
int main()
{
   char *strings[] = { "Zorro", "Alex", "Celine", "Bill", "Forest", "Dexter" };
    size_t strings_len = sizeof(strings) / sizeof(char *);

    /** STRING */
    puts("*** String sorting...");

    /* print original string array */
    print_cstring_array(strings, strings_len);

    /* sort array using qsort functions */
    qsort(strings, strings_len, sizeof(char *), cstring_cmp);

    /* print sorted string array */
    print_cstring_array(strings, strings_len);

}
void print_cstring_array(char **array, size_t len)
{
    size_t i;

    for(i=0; i<len; i++)
        printf("%s | ", array[i]);

    putchar('\n');
}
/* qsort C-string comparison function */
int cstring_cmp(const void *a, const void *B)/>
{
    const char **ia = (const char **)a;
    const char **ib = (const char **)b;
    return strcmp(*ia, *ib);
	/* strcmp functions works exactly as expected from
	comparison function */
}


Was This Post Helpful? 0
  • +
  • -

#6 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 966
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Using quicksort to alphabetize names?

Posted 05 May 2010 - 11:25 AM

Quote

My example will be for strings not structs you will have to work that out for yourself

That is not your example, and you know it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1