华南理工大学《算法设计》实验报告
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
华南理工大学《算法设计》实验报告
【Experiment requirement】
Use many sorting algorithm to sort the array that has n items. These items should be generated randomly. Use template to specify their types.
Given a binary tree T which has n elements, and a number called a, search if a exists in T or not.
Given a set of vertex and edges with weight in a directed graph G=(E,V), find the shortest path between two vertex in this graph.
Given some things with weight and profit, to finish the program to solve it using Greedy method and Search tree method.
Given a undirected Graph G=(V, E), to calculate the minimum spanning tree.
Initial the 9 grids with number 1 2 3 4 5 6 7 8, they are located randomly at these grids. Rearrange them such that they distributed like this {1 2 3 ; 8 4 ; 7 6 5}. Use MFC or C# to implement it.
【Experiment Implementation】
1. Quick Sort
2. Selection Sort
3. Bubble Sort
4. Insertion Sort
5. Shell Sort
Also, every sorting function are declared as a template. So the input of these functions can be int, double or other comparable type.
The size and elements are generated randomly.
Result:
All the sorting algorithm successfully sort the elements.
Binary search tree is a tree structure that the element of parent node is always larger than its left children and smaller than its right children. To define such a structure, we first define a node structure:
Every node has an element and two children. There are some functions to get or set their value.
Then we define the binary search tree structure:
The tree has a root and node count. Functions in this class include:
Insert: insert an element to a proper position into the tree. When we insert an element, we will first point to the root. Then compare with it. If it’s larger than this node, pass it to the right children, otherwise to left children. Compare recursively until add it to the leaf.
isLeaf: determine whether a given node is leaf node or no.Just find that whether it has children. It’s not a leaf if so.
getRoot: get the root node of the tree.
Find: find that whether a given element x exist in this tree or not. It’s implemented by a recursio n. Start from the root. If the element of this node equals to the given element,
return true. If the element of this node is larger than the given element, pass it to the left children. Pass it to right children otherwise. If the leaf node is also not equal to the given elements, return false;
Result:
We can set the size to the tree, and all the elements are generated randomly. Now it’s 93 68 84 90 60. When we search 60, it shows that 60 exist. For 1, it shows that 1 does not exist.