<< Chapter < Page | Chapter >> Page > |
A flowchart is a visual representation of an algorithm's control flow. That representation illustrates statements that need to execute, decisions that need to be made, logic flow (for iteration and other purposes), and terminals that indicate start and end points.
To present that visual representation, a flowchart uses various symbols, which Figure 3.5.shows.
This review was essential because we will be using these building blocks quite often today.
3. In pseudocode
Pseudocode (derived from pseudo and code) is a compact and informal high-level description of a computer programming algorithm that uses the structural conventions of programming languages, but omits detailed subroutines, variable declarations or language-specific syntax. The programming language is augmented with natural language descriptions of the details, where convenient, or with compact mathematical notation.
Example
Present the algorithm of converting an integer from decimal to binary
a. By words
Step 1: Let x is the decimal integer you want to convert and let k=1
Step 2 : Divide x by 2, saving the quotient as Q, and the remainder (in binary) as
Step 3 : If Q is not zero, let X=Q, and go back to step 2. Otherwise go to step 4.
Step 4 : Assume step 1-3 were repeated n times. Arrange the remainders as string for digit .
b. As a flowchart
c. By pseudocode
BEGIN
input x.y=””
remainder=0,while (x>0)
beginquotient=x/2
remainder=x mod 2y=conc(remainder,y)
x=quotientend
print yEND.
Example
Bubble Sort
Bubble sort is a simple sorting algorithm. It works by repeatedly stepping through the list to be sorted, comparing two items at a time and swapping them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.
5 1 4 2 8 - unsorted array
1 4 2 5 8 - after one pass
1 2 4 5 8 - sorted array
The algorithm gets its name from the way smaller elements "bubble" to the top (i.e. the beginning) of the list via the swaps.Because it only uses comparisons to operate on elements, it is a comparison sort. This is the easiest comparison sort to implement.
Here are the presentations of bubble sort algorithm
a. By words
Step 1: Get the length of the list : N and the list: list[1],list[2],…,list[N]
Step 2: M ← N.
Step 3: If M<2 then print the list, stop.
Step 4: M ← M – 1, i ← 0.
Step 5: Increase i by 1
Step 6: If i>M then go to step 3.
Step 7: If list[i]>list[i+1] swap list[i]and list[i+1]
Step 8: Go to step 5.
b. As a flow chart
c. In pseudocode
A simple way to express bubble sort in pseudocode is as follows:
BEGIN get length (list) and list’s elements
for each M in length(list) down to 2 do:for each i in 1 to M-1 do:
if list[ i]>list[ i+1 ] thenswap( list[i+1], list[ i ])
end ifend for
end forend procedure
Comparing the three methods,especially pseudocode and flowchart we realized :
Notification Switch
Would you like to follow the 'Introduction to computer science' conversation and receive update notifications?