University of Macau Faculty of Science and Technology Programming Languages Architecture SFTW 241 spring 2004 Class B Group 3
SFTW241 Programming Language Architecture Grouping Method Analysis by Program Presentation
Contents For this presentation, we have the following three parts: 1)A4 ’ s initial idea 2)The final version of B3 3)The conclusion
Part 1 A4 ’ s initial idea
A4 ’ s initial idea There are 21 students in class A and each student file structure include the following information: 1.Student ID 2.Student name 3.Firest, second and third choice 4.Choose = 0
A4 ’ s initial idea There is one array and four stacks in this program All the information of each student is stored in this array And each stacks store all the members’ information of the corresponding group In each student’s data structure, if the choose is equal to 0, it meant that this student has not been selected; if it is 1, it meant that this student has been selected
A4 ’ s initial idea First load all the students’ information into the array Then we choose four leaders random and load their information into the corresponding stack for each group And in this process there is a very important detail is that if the information is push into the stack, the choose in the array become 1 at once.
A4 ’ s initial idea …… captain …… captain …… captain …… captain 0… …
A4 ’ s initial idea Then load the captain ’ s first choice ’ s ID number and compare with the student ID number in the array If the ID number is not equal, compare with the next one If the ID number is equal and the choose is equal to 1, compare with the next one If the ID number is equal and the choose is equal to 0, push the student ’ s information into the corresponding stack
A4 ’ s initial idea THLYeddaUNWFZHLJQSUNZDFYYYEricWalter…… EricZDFYYYWalter Choose = 0
A4 ’ s initial idea If someone’s all three choices are selected, then the corresponding team will stop adding new member. And we use the randomly method to add new members
A4 ’ s initial idea Their idea is use stack to store each group ’ s information As we know, its advantage is that it is very easy to access data But its disadvantage is that we do not know who has been selected and who has not, and if you need to random select one person, you must do the selection from all the people, then the time you spend is very expensive
Improvement In the presentation between us and group A4, we got lots of good suggestions, and we also got some ideas from their algorithm.
Idea & Suggestion The most important suggestions: (a)One array is enough to carry out our algorithm (b)Swapping structures which contain huge data will cost lots of time (c)There is no need to swap or delete by using link-list
Part 2 F inal Version of B3
Suggestion Analysis I think our program should contain the following two advantages: (a)Efficient (b)Reusable (c)Informational
Reusable The data in the main memory can be used again even after the grouping. Compare with the memory operation, I/O operation (file processing) is very low. We have to do the grouping from the beginning if some special case happen.
Efficient It is necessary to divide the data into two parts: unused and used Just use one array to store the information The choice should be the integer instead of string for checking easy and less space
Informational Easy to get the information of a team: the number of the member; identity of the member
Data Structure Data is stored as the following form: class Record{ private: int id; string name; int first, second, third; }
Unit Student Unit Student ID Student Name First Choice Second Choice Third Choice
Improvements Id number is added, it is easy to get the corresponding student ’ s information Actually, id number is not the real ID of the student The favorites were stored as integer
Index array Index array, we also use the index array Advantages: (a) just swap the index when process a piece of information (b) protect the original data for the reuse
Length of the Index Array Let N = number of the students Let M = the number of the teams Let IA = index array Then IA.Length = int (N/M) *M If there are 18 students, and we will divide them into 4 groups, then the length of the index array should be 20.
Differences The length of the index array may be not the same as the number of the students IndexArray.Length >= S_R.Length
Processing of IA Our grouping just operate on the IA, S_R (student records) is just used to store the information
Processing of IA A count (integer) is used to divide the IA into two parts: used and unused When a new member was selected, it ’ s index will be moved to last position of the unused part, then the count will decrease one unit
THLLJQUNWFZHYedda…… ZDFYYYERICWalter Index i Counter used SUN
THLSUNUNWFZHYedda…… ZDFYYYERICWalter Index iCounter used LJQ
Searching
Searching
How to get the information of a group ID1ID2ID3ID6ID8ID5ID4ID7ID9ID10ID11ID12
Special case 1 If N/M == integer, it is the best case Else, the length of the IA will be longer than the length of the S_R The processing will be a little different at the end
Swapping
Member CounterIndex ( i ) Solution of the special case 1 10 students ID1ID2ID9ID3ID10ID6ID8ID5ID4ID7ID12ID11
Special case 2 At the beginning of the grouping, we will choose the leaders by chance. If one leader ’ s three choices are the leaders either, then it will choose the leaders again
Implementation
Implementation Show our program:
Part 3 Conclusion
Conclusion Both of two methods are base on the index array Advantages: a)Reusable b)Efficient c)Informational
Comparison Two versions 1.Pointer version (A4 ’ s algorithm) 2.Array version (Our algorithm)
Comparison Comparison a)It ’ s faster and easier to get the information of a group by using pointer b)Easy to add a new member or delete a wrong record by using pointer c)Pointer version have to use two pointer operations when a member is added into a group
Comparison d) Array version have to do some comparison when it want to get the information of a group e) Array version use less space
Space Comparison Pointer version: each element has to contain pointer, it meant that the pointer version must spend more space than the array version Array Version: It has the best and the worst case Best case: N / M == Integer Worst case: (N – 1)/ M == Integer
Best & Worst Case Array Version: Best Case: Each group has the same number of member Worst Case: There are one student more Pointer Version: There are not best and worst case in the pointer version
Conclusion Actually, both the two versions are efficient Running time are both O(N) Their have their own advantages and disadvantages
Conclusion What we done ?