Day 44 Of 100DayOfCode: More About Algorithm
Tags: womenintech, 100daysofcode, codenewbie, python
Published: 2021-02-06 15:48:25

This is my 44th day of #100DaysOfcode and #python. I continued to learn more about algorithm. Today I learned about bubble sort along with it's time complexity and space complexity.

Time complexity of an algorithm refers to the amount of time taken by an algorithm to run as a function of length of the input. Space complexity of an algorithm refers to the amount of space or memory taken by an algorithm to run as a function of the length of the input.

#Bubble Sort
Bubble sort is the simple comparison based algorithm. In which each pair of adjacent element is compared and the position of element are interchanged if they are in wrong order.

```python
def bubbleSort(A):
    for num in range(len(A)-1,0,-1):
        for i in range(num):
            if A[i] > A[i +1]:
                temp = A[i]
                A[i] = A[i +1]
                A[i + 1]  = temp
                print(f'Current array is {A}.')
                
A = [14, 33, 27, 35,10]
bubbleSort(A)
print(f'Sorted array is {A}.')
``` 
##Step 1
Starts with first two element 14, 33. Value 33 is greater than 14 so they are in right order. So it is already in sorted position. Now new array is
`14 33 27 35 10 `

##Step 2
Now we take two number 33 and 27. We find 33 is greater than 27 so their position interchange. New array look like
`14 27 33 35 10`
## Step 3
Here 33 and 35 are in right position. We don't need to interchange their position.New array is
`14 27 33 35 10`

##Step 4
We know that 10 is smaller 35. Hence they are not sorted. We interchange these values. We find that we have reached the end of the array. After one iteration, the array should look like 
`14 27 33 10 35 `
After second iteration it look like
`14 27 10 33 35`
After next iteration it look like
`14 10 27 33 35`
After one more iteration it look
`10 14 27 33 35`
Now our array is completely sorted.
 Output of above code is given below
```
Current array is [14, 27, 33, 35, 10].
Current array is [14, 27, 33, 10, 35].
Current array is [14, 27, 10, 33, 35].
Current array is [14, 10, 27, 33, 35].
Current array is [10, 14, 27, 33, 35].
Sorted array is [10, 14, 27, 33, 35].
```
<blockquote class="twitter-tweet"><p lang="en" dir="ltr">Day 44 Of <a href="https://twitter.com/hashtag/100DaysOfCode?src=hash&amp;ref_src=twsrc%5Etfw">#100DaysOfCode</a> of <a href="https://twitter.com/hashtag/Python?src=hash&amp;ref_src=twsrc%5Etfw">#Python</a> <br>* Worked more on bubble sort.<br>* Time complexity and Space complexity<br>* Best case Time Complexity O(n),Worst Case Time Complexity O(n*2)<a href="https://twitter.com/hashtag/womenintech?src=hash&amp;ref_src=twsrc%5Etfw">#womenintech</a> ,<a href="https://twitter.com/hashtag/100DaysOfCode?src=hash&amp;ref_src=twsrc%5Etfw">#100DaysOfCode</a> ,<a href="https://twitter.com/hashtag/WomenWhoCode?src=hash&amp;ref_src=twsrc%5Etfw">#WomenWhoCode</a> ,<a href="https://twitter.com/hashtag/Beginners?src=hash&amp;ref_src=twsrc%5Etfw">#Beginners</a> ,<a href="https://twitter.com/hashtag/CodeNewbies?src=hash&amp;ref_src=twsrc%5Etfw">#CodeNewbies</a> ,<a href="https://twitter.com/hashtag/pythonprogramming?src=hash&amp;ref_src=twsrc%5Etfw">#pythonprogramming</a> ,<a href="https://twitter.com/hashtag/Python3?src=hash&amp;ref_src=twsrc%5Etfw">#Python3</a> <a href="https://t.co/Bia5idQkw1">pic.twitter.com/Bia5idQkw1</a></p>&mdash; Durga Pokharel (@mathdurga) <a href="https://twitter.com/mathdurga/status/1358072260033015821?ref_src=twsrc%5Etfw">February 6, 2021</a></blockquote> <script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>