Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added visualizations to Insertion Sort #6

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions adaLovelace/InsertionSort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import numpy
import matplotlib.pyplot

'''
This code illustrates a bargraph representation of the the insertion sort in
each iteration.
>>>We have used 3 different colored bars in the graph.
>>>The green and red bars reperesent the 2 numbers currently being compared.
>>>The blue bars represent the numbers which are represent the other numbers.
'''


def insertionsort(alist):
for index in range(1, len(alist)):
currentvalue = alist[index]
position = index
bar(alist, index - 1, index, index)
while position > 0 and alist[position-1] > currentvalue:
bar(alist, position-1, position, index)
alist[position] = alist[position-1]
bar(alist, position-1, position, index)
position = position-1
alist[position] = currentvalue
if position != index:
bar(alist, position, position+1, index)
return alist


def bar(alist, r, g, label):
N = len(alist)
ind = numpy.arange(N)
width = 0.25
barlist = matplotlib.pyplot.bar(ind, alist, width, color='b')
barlist[r].set_color('r')
barlist[g].set_color('g')
matplotlib.pyplot.xlabel("Iteration number: %d" % (label))
matplotlib.pyplot.pause(2)
matplotlib.pyplot.show()
matplotlib.pyplot.gcf().clear()

if __name__ == '__main__':
alist = [
3, 1, 2, 5, 4
]
print alist
matplotlib.pyplot.ion()
insertionsort(alist)


11 changes: 11 additions & 0 deletions adaLovelace/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Team Ada Lovelace
**Saloni Dalal**
* [email protected]
* [@nonihd-codes](https://github.com/nonihd-codes)

**Birva Patel**
* [email protected]
* [@tangled2096](https://github.com/tangled2096)

# Contributions
1. Insertion Sort