Lecture#2 (Design Algo Week 2)
Lecture#2 (Design Algo Week 2)
Algorithm
WEEK 3
What is Logic?
How to improve your Logic?
What are your goals?
Examples
Student got 2.9 cgpa in three semester, now he is enrol in 5
subjects in 4th semester. How he can maintain overall 3.2 cgpa
in ADP.
Example
One student of BSIT 6 has two absentees in first two lectures of
Design and Analysis of Algorithm Lecture. According to the
instructor 80% attendance is compulsory for Mid term exam. The
student has to take one more leave before mid term paper. So it is
possible or not!
Example
The last submission date of SRS was Monday 6th February 2023,
but one group of ADP CS 4th do not submit there SRS. But a
logic, how they can submit there documentation.
Parse Tree
A parse tree or parsing tree or derivation tree or concrete syntax tree is an
ordered, rooted tree that represents the syntactic structure of a string
according to some context-free grammar.
Parse trees are an in-memory representation of the input with a structure that
conforms to the grammar.
The advantages of using parse trees instead of semantic actions:
You can make multiple passes over the data without having to re-parse the
input.
You can perform transformations on the tree.
Draw a parse tree for Computer Science?
Now draw a parse tree for your future
goal?
What is Sorting?
In computer science, arranging in an ordered sequence is called "sorting".
Sorting is a common operation in many applications, and efficient
algorithms to perform it have been developed.
The most common uses of sorted sequences are: making lookup or search
efficient; making merging of sequences efficient.
For example, you might want to order sales data by calendar month so that
you can produce a graph of sales performance.
Sorting types
Bubble sort
Selection sort
Insertion sort
Merge sort
Quick sort
Heap sort
Counting sort
Radix sort
Bubble Sort
Bubble sort is a type of sorting algorithm you can use to arrange a set of
values in ascending order.
If you want, you can also implement bubble sort to sort the values in
descending order.
A real-world example of a bubble sort algorithm is how the contact list on
your phone is sorted in alphabetical order.
Bubble sort may require (n/2) passes and O(n) comparisons for each pass
in the average case.
Continue…
As a result, the average case time complexity of bubble sort is:
O(n/2 x n) = O(n/2 x n) = O(n/2 x n) = O(n/2 x n) = O (n2).
Worst complexity: n^2
Average complexity: n^2
Best complexity: n
Space complexity: 1
Method: Exchanging
Stable: Yes
Class: Comparison sort
Now sort the following according to
bubble sort:
16 54 41 1 19 21 41
Python Code
def bubble_sort(arr):
arr_len = len(arr)
for i in range(arr_len-1):
flag = 0
for j in range(0, arr_len-i-1):
if arr[j] > arr[j+1]:
arr[j+1], arr[j] = arr[j], arr[j+1]
flag = 1
if flag == 0:
break
return arr
arr = [5, 3, 4, 1, 2]
print("List sorted with bubble sort in ascending order: ", bubble_sort(arr))
# Output: List sorted with bubble sort in ascending order: [1, 2, 3, 4, 5]
Do what ever you what to do but
Remember them!