Quicksorting

closed account (ETf1hbRD)
Here is a snippet of my code, it'll compile, but when I run the program, it just get stuck after I entered all the employee's names. So I want to know what's wrong with this code and am I taking the right approach to sort the last names?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <string>

typedef struct EmpRecord
{
	char lastname[15];
	char firstname[15];
	char fullname[30];
}EmpRecord;

void qsort(EmpRecord theEmps[],int left,int right);
void swap(EmpRecord theEmps[],int i, int j);

int main(void)
{
  EmpRecord theEmps[5];
  qsort(theEmps,0,4);
  return 0;
}
  void qsort(EmpRecord theEmps[5],int left,int right)
{
 int i,last;
 void swap(EmpRecord theEmps[5],int i, int j);
 
 if (left >= right)
    return;
 swap(theEmps,left, (left + right)/2);
 last = left;
 for(i = left+1; i <= right; i++)
       if(strcmp(theEmps[i].lastname, theEmps[left].lastname < 0))
          swap (theEmps, ++last, i);
 swap (theEmps,left,last);
 qsort(theEmps,left,last-1);
 qsort(theEmps,last+1,right);
}

void swap(EmpRecord theEmps[5],int i, int j)
{
     EmpRecord temp;
     temp = theEmps[i];
     theEmps[i] = theEmps[j];
     theEmps[j] = temp;
}
Topic archived. No new replies allowed.