Docsity

Bubble Sort Algorithm: A Practical Guide with C# Implementation - Prof. Pham, Assignments of Programming for Engineers

Toggle sidebar
bg1
ASSIGNMENT 1 FRONT SHEET
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
GCD201914
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
M1
D1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

Download Bubble Sort Algorithm: A Practical Guide with C# Implementation - Prof. Pham and more Assignments Programming for Engineers in PDF only on Docsity!

ASSIGNMENT 1 FRONT SHEET

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

 Summative Feedback:  Resubmission Feedback:

Grade: Assessor Signature: Date:

Lecturer Signature:

  • CHAPTER I. STATE SIMPLE BUSINESS PROBLEMS TO BE SOLVED
    • 1.1 OVER REVIEW ABOUT THE ALGORITHM
      • a) Definition
      • b) Character of algorithm
      • c) For example algorithm
    • 1.2 Represent a small and simple problem
  • CHAPTER II. ANALYSE THE PROBLEM AND DESIGN THE SOLUTIONS
    • 2.1 Analyze the problem............................................................................................................................................
      1. 2 Flowchart and Diagram
      • 2.2.1 Flowchart
      • 2.2.2 Diagram
  • CHAPTER III. DEMONSTRATE THE COMPILATION AND RUNNING A PROGRAM
    • 3.1 Introduce how the problem is solved.................................................................................................................
    • 3.2 Source code and screenshots of the final result
      • a) Main function
      • b) EnterArray and AppearArray function
      • c) SortAscending Function
      • d) SortDescending Function
      • e) Result of the test
    • 3.3 Explain briefly what is Software Development Life Cycle
    • 3.4 Explain how the source code is compiled
  • PROGRAM WRITTEN BY A SPECIFIC PROGRAMMING LANGUAGE CHAPTER IV. EVALUATE HOW THE PROBLEMS HAS BEEN SOLVED TO THE EXECUTION OF THE
    • 4.1 Include Test cases
    • specific programming language 4.2 Evaluate how the problem is solved from the designed algorithm to the execution program written by a
  • Bibliography.................................................................................................................................................................
  • Figure 1: Example of description of the steps Table of Figure
  • Figure 2: Example of using flowchart
  • Figure 3: Flowchart of Sort Ascending
  • Figure 4: Flowchart of Enter Array
  • Figure 5: Flowchart of Appear Array
  • Figure 6: Flowchart of Main........................................................................................................................................
  • Figure 7: Diagram of problem
  • Figure 8: Source code of void Main
  • Figure 9: Soure code of void EnterArray and AppearArray
  • Figure 10: Source code of void SortAscending
  • Figure 11: Source code of void SortDescending
  • Figure 12: Final of results............................................................................................................................................

CHAPTER I. STATE SIMPLE BUSINESS

PROBLEMS TO BE SOLVED

1.1 OVER REVIEW ABOUT THE ALGORITHM

a) Definition

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

b) Character of algorithm

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

1.2 Represent a small and simple problem

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)

CHAPTER II. ANALYSE THE PROBLEM AND

DESIGN THE SOLUTIONS

2.1 Analyze the problem

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

2.2.2 Diagram

Figure 7: Diagram of problem

CHAPTER III. DEMONSTRATE THE

COMPILATION AND RUNNING A PROGRAM

3.1 Introduce how the problem is solved

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.

3.2 Source code and screenshots of the final result

a) Main function

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

b) EnterArray and AppearArray function

  • In the EnterArray Function, I run a for loop to limit the input elements. In the for loop, I proceed to enter the correct elements with n number of elements to enter.
  • In the Appear Array Function, I also run a for loop to print the elements entered in the array input function. The main purpose of this function is to be able to see more clearly the Result Before Sorting and the Result After Sorting

Figure 10: Source code of void SortAscending

d) SortDescending Function

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

e) Result of the test

Input: n=6; then enter the elements including (3, 2, 5, 7, 4, 8).

Output: + Results before sorting : 3 2 5 7 8 4

  • Results after sorting:

 The elements in ascending order: 2 3 4 5 7 8.  The elements in descending order: 8 7 5 4 3 2.

CHAPTER IV. EVALUATE HOW THE

PROBLEMS HAS BEEN SOLVED TO THE

EXECUTION OF THE PROGRAM WRITTEN

BY A SPECIFIC PROGRAMMING

LANGUAGE

4.1 Include Test cases

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:

  1. And then Enter the element 0: - 1 Enter the element 1: - 3 Enter the element 2: 4 Enter the element 3: 5 Enter the element 4: 2 Enter the element 5: 8

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