How to sort names in alphabetical order?

October 28, 2010 at 06:07:05
Specs: Windows 7, 8g
 I am currently using the bubbleSort method in order to sort my arrays and I have never done sorting before so I am trying to play it by ear.case 7: insertionSort(employees) break;void insertionSort(int size, Employee * employees){ for(int i =1; i < size; i++) { int t = employees[i]; int j; for( j = i-1; j >= 0 && t < employees[j]; j--) employees[j+1] = employees[j]; employees[j+1] = t; }}Basically there are a ton of errors I have been programming for hours so any help would be appreciated. Thx abignold

See More: How to sort names in alphabetical order?

#1
October 28, 2010 at 07:48:59
 They are many sort methods. Bubble sort is fine for a smaller lists, As long as you do the swaps properly and keep track of the indexes it will work.

Report •

#2
October 28, 2010 at 09:30:50
 You say you're using bubble sort but you give the insertion sort algorithm. Do you mean you are trying to migrate from bubble sort to the more efficient insertion sort?One of your problems is you're using the wrong types, i.e. setting an Employee to an int and vice versa.

Report •

#3
October 28, 2010 at 09:37:29
 I'm not sure what this is, but it's not a bubble sort. It looks like it attempts to insert into a pre-sorted list, but what it attempts to insert is unclear, because no new node is specified. 1) insertionSort(employees) / void insertionSort(int size, Employee * employees)You're calling insertionSort() with just "employees", but the function expects an int and a pointer to an employee collection.2) int t = employees[i];You're trying to save an Employee object/struct in an int. 3) You have what looks like you have the start of a bubble sort, but you just sort of stuffed an inset element in the middle. The end result is some nodes will be repeated, and others will be lost.How To Ask Questions The Smart Way

Report •

Related Solutions

#4
October 28, 2010 at 13:37:16
 Hey guys.Well I have been working on this current project(Not just this section) for the past 18 hours straight and I am still soldiering on.I still can't get this bubbleSort to work or an insertionSort for that matter. I have tried many methods and I just can't get it right. I can sort most arrays fine generally but at the moment I am really struggling to sort alphabetically using objects.I am storing my objects like this Employee( string &, string &, string &); void setFirstName( string & ); string getFirstName(); void setLastName( string & ); string getLastName(); void settaxFileNumber( string & ); string gettaxFileNumber();My current spinoff of bubbleSort now looks like this:void bubbleSort(Employee * employees[], int size){ int temp = 0; int swaps = 0; int left = 0; int right = 1; while(swaps < 0); { while(rightgetLastName() in this code but I figured a simplified version of my current code my help you help me.I apologize that I suck at life right now... No sleep does that to a person.

Report •

#5
October 28, 2010 at 14:01:25
 You're still using int for temp, and not Employee temp.Also, you should really be using two for loops.And you should take a break. It'll help.

Report •

#6
October 28, 2010 at 14:09:30
 What do you mean by Employee Temp? Im pretty brain dead at the moment. Obviously you don't want me to add another attribute to the object so how do I do this. Im relatively new to C++. I have been learning C++ for about a week or two but I have learnt a fair amount of C#, Python and I started on visual basic. Some of the syntax changes and handling in C++ is taking a while to adapt to and C++ also uses objects slightly different to all those programs but in alot of areas.. If that makes sense.Honestly anything to get this done I will do I am sick of wasting my time on it. I just cant get the association with objects.Im going to go to bed for an hour or two.

Report •

#7
October 28, 2010 at 14:30:55
 This is what I'm talking about:int temp = 0;temp = employees[left];Unless your class can devolve into an int (it can't), you can't use an int to hold an Employee.employees[right] = temp;Unless your class is derived from an int (it isn't) (or you have an Employee& operator=(int) function (you shouldn't))you cannot assign an int to an Employee.

Report •