














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!















Qualification BTEC Level 5 HND Diploma in Computing
Unit number and title Unit 1: Programming
Submission date Date Received 1st submission
Re-submission Date Date Received 2nd submission
Student Name Phan Minh Tiến Student ID GCD
Class GCD1001^ Assessor name Đỗ^ Duy Thảo
Student declaration
I certify that the assignment submission is entirely my own work and I fully understand the consequences of plagiarism. I understand that making a false declaration is a form of malpractice.
Student’s signature Tiến
Grading grid
P1 M1 D
Grade: Assessor Signature: Date:
Lecturer Signature:
An algorithm is a process or set of rules that are followed in calculations or other problem-solving operations. Algorithms are considered the cornerstone of data programming. It includes rules, directives, or methods for completing the initial state given. When the requirements are carefully arranged, the algorithm will show exact results
An algorithm should have the following characteristics:
Unambiguous - The algorithm should be clear and unambiguous. Each of its processes (or stages), as well as their inputs and outputs, should be obvious and lead to only one meaning.
Input - Each Algorithm must have 0 or more inputs that are well-defined.
Output - A well-defined algorithm must have one or more outputs that match the desired output.
Figure 2: Example of using flowchart
The purpose of this report is to get a brief description of the algorithm as well as an attempt to create an application to address simple business problems. In this research, I will introduce the algorithm and use it to come up with measures to solve the problem: Sorting elements in an array in ascending and descending order (by bubble sort algorithm)
The Problem: Sorting elements in an array in ascending and descending order.
When it comes to sorting algorithms, there are many sorting methods with varying degrees of difficulty and speed. Based on those levels, people divide sorting algorithms into 4 main types including: selection sort, insertion sort, bubble sort, and quicksort.
But for this problem, I will use bubble sort algorithm to solve the problem. Because bubble sort is considered the simplest algorithm, but has the slowest speed. This algorithm will work similarly to bubbles rising. Through each loop, the smaller elements will "float" to the beginning of the array, the larger elements will be at the end of the array if it is ascending sort and vice versa if it is descending sort.
-The idea of this algorithm is to use two for loops. The purpose of using these two loops is to browse the adjacent pairs of elements in the entered array and then compare the following elements with the previous element:
For ascending sort: If the following element is smaller than the previous element, proceed to swap them. The small element will gradually float to the top.
For descending sort: If the following element is larger than the previous element, proceed to swap them. The large element will gradually float to the top.
Repeat the action until no pair is satisfied. The maximum is n-1 times (n is the number of elements of the array).
Finally, Compare the Results Before Sorting with the Results After sorting, the problem has been solved.
b) Flowchart of Sort Descending
(*) Flowchart of Sort Descending similar to Figure 3. Only condition change is from arr[j- 1 ] > arr[j] to arr[j-1] < arr[j].
c) Flowchart of Enter Array
Figure 4: Flowchart of Enter Array
d) Flowchart of Appear Array
Figure 5: Flowchart of Appear Array
e) Flowchart of Main
Figure 7: Diagram of problem
To solve this problem, I wrote a small program in the C# programming language. The program will be based on the flowchart, simple algorithm, and solution to the problem presented above. And I'll be writing code in the Visual Studio 2019 IDE.
In the Main function, I declare the integer type n, and declare the array int[] arr. Enter n number of elements. Next I call the functions that I have created including EnterArray function, AppearArray function, SortAscending function, SortDescending function. The purpose I use the method here is to keep the Main function clear, the second reason is that I want to use the method to easily divide and conquer other functions.
Figure 8: Source code of void Main
Figure 10: Source code of void SortAscending
In Descending Sort function, I do the same thing as SortAscending function. First, I declare an intermediate variable to do the swapping between the elements in the array arr[j- 1 ] and arr[j]. I use 2 nested for loops to browse pairs of adjacent elements. If arr[j- 1 ] < arr[j] (the following element is larger than the previous elements), then proceed to swap places. That will make the largest elements gradually float to the top of the array, and the smallest elements drift to the end of the array (That's a sign of bubbling). That action will repeat up to n-1 times. Finally, I used one more for loop, to print the elements after they were sorted in descending order.
Figure 11: Source code of void SortDescending
Input: n=6; then enter the elements including (3, 2, 5, 7, 4, 8).
Output: + Results before sorting : 3 2 5 7 8 4
The elements in ascending order: 2 3 4 5 7 8. The elements in descending order: 8 7 5 4 3 2.
Test Case
Test Content Expected Results
Actual Results Passed/ Failed 1 Enter the number of elements of the array: a
Request that the user enters again(Please enter a positive integer greater than 0)
Failed
2 Enter the number of elements of the array: 6
Requires enter 6 elements starting from the element 0.
Passed
3 Enter the number of elements of the array:
Then Enter the element 0: c
Please enter the elements as integer data type!
Failed
4 Enter the number of elements of the array:
And then Enter the element 0: 3 Enter the element 1: 5 Enter the element 2: 9 Enter the element 3: 22 Enter the element 4: 12 Enter the element 5: 7
Output the elements in the entered array including: 3 5 9 22 12 7. Sort the elements in the array in ascending order: 3 5 7 9 12 22. And Sort the elements in the array in descending order: 22 12 9 7 5 3.
Passed
5 Enter the number of elements of the array:
Output the elements in the entered array including: - 1 - 3 4 5 2 8. Sort the elements in the array in ascending order: - 3 - 1 2 4 5 8. And Sort the elements in the array in descending order: 8 5 4 2 - 1
Passed