Selection Sort Animation with Python and Turtle (Source Code Included)

Generate 50 random numbers and draw them as bars. Following the steps of selection sort algorithm and redraw the bar every single step to show how bubble sort works.

Compare against Bubble Sort and Insertion Sort. Which is the fastest? Which is the slowest?

Source Code:

import turtle
import random
import time

screen = turtle.Screen()
screen.setup(1000,1000)
screen.tracer(0,0)
screen.title('Selection Sort Animation - PythonTurtle.Academy')
turtle.speed(0)
turtle.hideturtle()

def draw_bar(x,y,w,h):
    turtle.up()
    turtle.goto(x,y)
    turtle.seth(0)
    turtle.down()
    turtle.begin_fill()
    turtle.fd(w)
    turtle.left(90)
    turtle.fd(h)
    turtle.left(90)
    turtle.fd(w)
    turtle.left(90)
    turtle.fd(h)
    turtle.left(90)
    turtle.end_fill()

def draw_bars(v,n,current):
    x = -400
    w = 800/n
    for  i in range(n):
        if i == current: turtle.fillcolor('red')
        else: turtle.fillcolor('gray')
        draw_bar(x,-400,w,v[i])
        x += w

def select_smallest(v,i,k):
    smallest = v[i]
    smallest_index = i
    for j in range(i+1,k):
        if v[j] < smallest:
            smallest = v[j]
            smallest_index = j
        turtle.clear()
        draw_bars(v,n,j)
        turtle.update()
    return smallest_index
        
def selection_sort(v,k):
    for i in range(k-1):
        j = select_smallest(v,i,k) # smallest index from ith to last
        v[i], v[j] = v[j], v[i] # swap with the smallest
        turtle.clear()
        draw_bars(v,n,i)
        turtle.update()
        
n = 50
v = [0] * n
for i in range(n):
    v[i] = random.randint(1,800)

t1 = time.time()
selection_sort(v,n)
turtle.clear()
draw_bars(v,n,-1)
turtle.update()
t2 = time.time()
print('elapsed time=', t2-t1)

Insertion Sort Animation with Python and Turtle (Source Code Included)

Generate 50 random numbers and draw them as bars. Following the steps of insertion sort algorithm and redraw the bar every single step to show how bubble sort works.

Compare against Bubble Sort. Which is faster?

Source Code:

import turtle
import random
import time

screen = turtle.Screen()
screen.setup(1000,1000)
screen.tracer(0,0)
screen.title('Insertion Sort Animation - PythonTurtle.Academy')
turtle.speed(0)
turtle.hideturtle()

def draw_bar(x,y,w,h):
    turtle.up()
    turtle.goto(x,y)
    turtle.seth(0)
    turtle.down()
    turtle.fillcolor('gray')
    turtle.begin_fill()
    turtle.fd(w)
    turtle.left(90)
    turtle.fd(h)
    turtle.left(90)
    turtle.fd(w)
    turtle.left(90)
    turtle.fd(h)
    turtle.left(90)
    turtle.end_fill()

def draw_bars(v,n):
    x = -400
    w = 800/n
    for  i in range(n):
        draw_bar(x,-400,w,v[i])
        x += w

def insert(v,k):
    x = v[k]
    for i in range(k-1,-1,-1):
        if v[i] > x: v[i+1] = v[i]
        else:
            v[i+1] = x
            return
        turtle.clear()
        draw_bars(v,n)
        screen.update()
    v[0] = x
    
def insertion_sort(v,k):
    if k == 1: return
    insertion_sort(v,k-1)
    insert(v,k-1)
    
n = 50
v = [0] * n
for i in range(n):
    v[i] = random.randint(1,800)

t1 = time.time()
insertion_sort(v,n)
t2 = time.time()
print('elapsed time=', t2-t1)

Asteroids Game with Python Turtle

Develop the Asteroids Game with Python Turtle. Because this project is fairly large, you may want to use Object Oriented Programming by defining several classes and put them in separate files. You may also need to know how to detect collisions. These projects will help you develop this game:

You can also add music and sound effects to this game with PyGame’s sound library.

Closest Point On a Line Segment to Another Point

Write a program that finds the closest point of a line segment to another point. Randomly pick a point and color it in blue. Also, randomly generate several lines. Find the closest points on these lines to the blue point and mark these closest points in red as show in the picture below:

Closest Point On a Line Segment to the Blue Point

What’s next?
Intersecting Lines with a Circle

Detecting a Point in a Polygon with Python Turtle

Generate a random polygon with many sides and randomly drop point on the screen. If the point falls inside the polygon color it in red, otherwise color it in blue.

You can use Ray Casting Algorithm to determine if a point is inside a polygon. You may also want to know how to detect if two lines intersect with these two projects: Random Intersecting Lines and Two Randomly Moving Intersecting Lines.

Two Randomly Moving Intersecting Lines

Continue from a previous project with one moving line, and knowing how to detect if two lines intersect, animate two randomly moving lines. When these two lines intersect, color the the line in green. Knowing the orientation of triangles may help you solve this problem.

Triangle Orientation with Python Turtle

Given three ordered points (red, green, blue) of a triangle, the orientation of the triangle is clockwise if a right turn happens from the first line (red to green) to the second line (green to blue). Similarly the orientation of the triangle is counterclockwise if a left turn happens from the first line (red to green) to the second line (green to blue).

Draw many random triangles and color triangle in yellow if the triangle is clockwise, color triangle in cyan if the triangle is counterclockwise.

Triangle Orientation with Python Turtle

What’s next?
Random Intersecting Lines with Python Turtle
Two Randomly Moving Intersecting Lines