Upcoming SlideShare
×

# Swapping ball - Nguyễn Đình Nhật - Nguyễn Như Hải Triều

1,190 views

Published on

Published in: Education
0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

• Be the first to like this

Views
Total views
1,190
On SlideShare
0
From Embeds
0
Number of Embeds
24
Actions
Shares
0
25
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Swapping ball - Nguyễn Đình Nhật - Nguyễn Như Hải Triều

1. 1. Computer Science for Practicing Engineering DUY TAN UNIVERSITY DUY TAN UNIVERSITY INTERNATIONAL SCHOOL ASSIGNMENT Computer Science for Practicing Engineering ********** Faculty: HUỲNH BÁ DIỆU Students name: NGUYỄN ĐÌNH NHẬT NGUYỄN NHƯ HẢI TRIỀU Class: K15CMU-TCD1 Da Nang, 28/05/2011Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 1 of 14
2. 2. Computer Science for Practicing Engineering DUY TAN UNIVERSITY ASSIGNMENTReqest: Swap N number of blue balls and red ball.Example: Enter N is 3.Total ball: 3 red balls + 3 blue balls = 6 ballsTotal cells: 6 balls + 1 empty = 7 cells Describe project: In this project, we use Double Link List to keep balls. We use Node to keepinformation. Information of Node include Data (Red, Blue or Empty), Next ( Next Node), andPrevious (Previous Node). Additionally, We use HEAD node and TAIL node to define the firstnode and the last node. Describe the way of sort: Enter any N “number”. We create Double Link List to include 2*N+1 Node. It incude Nred balls, N blue balls and 1 Empty Node. Move by one blue ball from left to right. Then move the box from right to left (blue ballnext to last). Continue to moving all blue ball to the right. Move the empty cell into positionbetween red and green balls. However, in this algorithm, we have two case to comeback. If N is even number or N isodd number. With N is even number, we swap empty cell for pre-pre-emptycell, and continueto moving Blue ball to an adjacent empty square. With N is odd number, we use a notherway. We need two steps to move the ball on the desired location.Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 2 of 14
3. 3. Computer Science for Practicing Engineering DUY TAN UNIVERSITY Describe algorithm: Create Node: Node Describe Type Data Red, Blue or Empty Char Next Next Node Node Prev Previous Node Node Enter N number: 2 Create Double Link List:Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 3 of 14
4. 4. Computer Science for Practicing Engineering DUY TAN UNIVERSITY Describe algorithm by chartAuthors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 4 of 14
5. 5. Computer Science for Practicing Engineering DUY TAN UNIVERSITY Describe algorithm by Pseudocodeint checkList = 0; while(checkList!=n) nodeRef = Head; while(nodeRef.next.data!=*) nodeRef = nodeRef.next; recheck condition. // nodeRef == REDball while (REDball != Tail && Next-REDball == EMPTYcell) change REDball for EMPTYcell in the next. if(REDball !=Tail && Next-REDball == BLUEball) change EMPTYcell (behind REDball) for BLUEball (in front of it); if(++checkList==n) Finish algorithm and print result. nullNode = nodeRef.prew; // nodeRef ==EMPTYcell while (Prev-EMPTYcell != BLUEball && Prev-EMPTYcell != Head) if ( Prev-Prev-EMPTYcell != BLUEball) change EMPTYcell for Prev-Prev-EMPTYcell. else change EMPTYcell for Prev-Prev-EMPTYcell. change EMPTYcell for Next-EMPTYcell change EMPTYcell for Next-Next-EMPTYcellAuthors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 5 of 14
6. 6. Computer Science for Practicing Engineering DUY TAN UNIVERSITY Describe by modelWith N = 2 (even number)Total steps: 11Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 6 of 14
7. 7. Computer Science for Practicing Engineering DUY TAN UNIVERSITYWith N = 3 (odd number)Total steps: 21Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 7 of 14
8. 8. Computer Science for Practicing Engineering DUY TAN UNIVERSITYAuthors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 8 of 14
9. 9. Computer Science for Practicing Engineering DUY TAN UNIVERSITY CODE: LinkList class/* * Object: Computer Science for Practicing Engineering * Faculty Huynh Ba Dieu * Authors: * 1. Nguyen Dinh Nhat * 2. Nguyen Nhu Hai Trieu * Class: K15CMU-TCD1 -- International School -- Duy Tan University * 05/2011 */import java.util.*;public class LinkList { // Node class class Node { char data; Node next,prew; //Method: create a node Node(char x) { data=x; next=prew=null; } Node(char x, Node t) { data = x; next = prew = null; if(t!=null) { next = t; t.prew = this; } } } Node Head, Tail; Node nullNode; Node nodeRef; static int count=0; static int n; //Method: create null Link List LinkList(){Authors: Nguyễn Đình Nhật – Nguyễn Như Hải Triều --- Class: K15CMU-TCD1 Page 9 of 14