MCPcopy Index your code
hub / github.com/ForeignGods/Sorting-Algorithms-Blender

github.com/ForeignGods/Sorting-Algorithms-Blender @main

Chat with this repo
repository ↗ · DeepWiki ↗ · + Follow
81 symbols 287 edges 20 files 0 documented · 0%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Sorting-Algorithms-Blender

Sorting algorithms visualized using the Blender Python API.

Table of contents

Introduction

Description

Running one of the scripts in this project generates primitive meshes in Blender, wich are animated to visualize various sorting algorithms.

Keyframes are incrementaly inserted according to the current position of the element in the array during the sorting.

The four folders (sort_circle, sort_color, sort_combined, sort_scale) contain four different types of visualization.

Type Sorting Criteria Representation of Index Extras
sort_circle hsv of material rotation of cuboid hsv = 360°
sort_color red + green of material location of plane custom color gradient
sort_combined red + green of material location of plane multiple 2d arrays arranged into a cube
sort_scale scale of cubiod location of cuboid array access and comparison counter

Getting Started

  1. Download, install and start Blender.
  2. Open the .py file in the Text Editor.
  3. Click the play button to run the script.

Limitations

These visualizations don't show the efficiency of the algorithms.

They only visualize movement of the elements within the array.

But the array access and comparison counters are indicators of the time complexity of the algorithms.

For more information about the time complexity, you can take a look at the Big O Table.

Possible Updates

Below I compiled a list of features that could be implemented in the future.

Contributions to this project with either ideas from the list or your own are welcome.

Sorting Algorithms

combined_sort_cube.py
cube_sort_opti

Bubble Sort

Bubble sort is one of the most straightforward sorting algorithms, it makes multiple passes through a list.

  • Starting with the first element, compare the current element with the next element of the array.
  • If the current element is greater than the next element of the array, swap them.
  • If the current element is less than the next element, move to the next element.

In essence, each item “bubbles” up to the location where it belongs.

bubble_sort_scale.py bubble_sort_color.py bubble_sort_circle.py
bubble_scale_opti bubble_color_opti bubble_circle_perf

Insertion Sort

Like bubble sort, the insertion sort algorithm is straightforward to implement and understand.

  • Iterate from arr[1] to arr[n] over the array.
  • Compare the current element (key) to its predecessor.
  • If the key element is smaller than its predecessor, compare its elements before.
  • Move the greater elements one position up to make space for the swapped element.

It splits the given array into sorted and unsorted parts, then the values from the unsorted parts are picked and placed at the correct position in the sorted part.

insertion_sort_scale.py insertion_sort_color.py
insertion_scale_opti insertion_color_opti

Selection Sort

The algorithm maintains two subarrays in a given array.

  • The subarray which is already sorted.
  • Remaining subarray which is unsorted.

In every iteration of selection sort, the minimum element (considering ascending order) from the unsorted subarray is picked and moved to the sorted subarray.

selection_sort_scale.py selection_sort_color.py selection_sort_circle.py
selection_sort_opti selection_color_opti selection_circle_perf

Heap Sort

Based on the binary heap data structure, heap sort is mainly considered as a comparison-based sorting algorithm.

In this sorting technique, at first, the minimum element is found out and then the minimum element gets placed at its right position at the beginning of the array.

For the rest of the elements, the same process gets repeated.

So, we can say that heap sort is quite similar to the selection sort technique.

Heap sort basically recursively performs two main operations:

  • Build a heap H, using the elements of array.
  • Repeatedly delete the root element of the heap formed in 1st phase.
heap_sort_scale.py heap_sort_color.py
heap_scale_opti heap_color_opti

Shell Sort

The shell sort algorithm extends the insertion sort algorithm and is very efficient in sorting widely unsorted arrays.

The array is divided into sub-arrays and then insertion sort is applied.

The algorithm is:

  • Calculate the value of the gap.
  • Divide the array into these sub-arrays.
  • Apply the insertion sort.
  • Repeat this process until the complete list is sorted.

This sorting technique works by sorting elements in pairs, far away from each other and subsequently reduces their gap.

The gap is known as the interval. We can calculate this gap/interval with the help of Knuth’s formula.

shell_sort_scale.py shell_sort_color.py
shell_scale_opti shell_color_opti

Merge Sort

Merge sort uses the divide and conquer approach to sort the elements.

It is one of the most popular and efficient sorting algorithms.

The sub-lists are divided again and again into halves until the list cannot be divided further.

Then we combine the pair of one element lists into two-element lists, sorting them in the process.

The sorted two-element pairs is merged into the four-element lists, and so on until we get the sorted list.

merge_sort_scale.py merge_sort_color.py merge_sort_circle.py
merge_scale_opti merge_color_opti merge_circle_perf

Quick Sort

Like Merge Sort, Quick Sort is a Divide and Conquer algorithm. It picks an element as pivot and partitions the given array around the picked pivot. There are many different versions of Quick Sort that pick pivot in different ways.

  • Always pick first element as pivot.
  • Always pick last element as pivot.
  • Pick a random element as pivot.
  • Pick median as pivot. (implemented below)

The key process in quickSort is partition(). Target of partitions is, given an array and an element x of array as pivot, put x at its correct position in sorted array and put all smaller elements (smaller than x) before x, and put all greater elements (greater than x) after x.

All this should be done in linear time.

quick_sort_scale.py quick_sort_color.py quick_sort_circle.py
quick_scale_opti quick_color_opti quick_circle_perf

Big O

What is Big O Notation?

Big O notation is the language we use for talking about how long an algorithm takes to run.

It's how we compare the efficiency of different approaches to a problem.

With Big O notation we express the runtime in terms of **how quickly it grows relative to the input, as the input gets arbitrarily larg

Core symbols most depended-on inside this repo

get_rg
called by 10
sort_combined/combined_sort_cube.py
mat_to_hsv
called by 8
sort_circle/quick_sort_circle.py
setup_array
called by 6
sort_combined/combined_sort_cube.py
mat_to_hsv
called by 4
sort_circle/heap_sort_circle.py
get_rg
called by 4
sort_color/quick_sort_color.py
mat_to_hsv
called by 2
sort_circle/selection_sort_circle.py
heapify
called by 2
sort_circle/heap_sort_circle.py
mat_to_hsv
called by 2
sort_circle/merge_sort_circle.py

Shape

Function 81

Languages

Python100%

Modules by API surface

sort_combined/combined_sort_cube.py10 symbols
sort_circle/quick_sort_circle.py5 symbols
sort_circle/merge_sort_circle.py5 symbols
sort_circle/heap_sort_circle.py5 symbols
sort_scale/quick_sort_scale.py4 symbols
sort_scale/merge_sort_scale.py4 symbols
sort_scale/heap_sort_scale.py4 symbols
sort_color/quick_sort_color.py4 symbols
sort_color/merge_sort_color.py4 symbols
sort_color/heap_sort_color.py4 symbols
sort_circle/selection_sort_circle.py4 symbols
sort_circle/bubble_sort_circle.py4 symbols

For agents

$ claude mcp add Sorting-Algorithms-Blender \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact