Brian Rashap
Published

Visualizing Sorting Algorithms

For my workforce development class, I show how a sorting algorithm works using NeoPixels (red larger, blue smaller).

BeginnerFull instructions provided1 hour152
Visualizing Sorting Algorithms

Things used in this project

Hardware components

Argon
Particle Argon
×1
LED Strip, NeoPixel Digital RGB
LED Strip, NeoPixel Digital RGB
×1

Story

Read more

Code

Sorting visualization code

C/C++
Implementing bubble sort and quick sort. Visualization via NeoPixels.
/*
 * Project SortVisualizer
 * Description: Visual various sort algorithms
 *    Currently implemented:
 *        BubbleSort
 *        QuickSort
 * Author: Brian Rashap
 * Date: 27-NOV-2022
 */

#include "neopixel.h"

SYSTEM_MODE(SEMI_AUTOMATIC);
SYSTEM_THREAD(ENABLED);

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D8
#define PIXEL_COUNT 36

Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, WS2812B);

// Constants
const int SPEED = 25;
const int colors[] = {0xFF0000, 0xE31C00, 0xC63900, 0xAA5500, 0x718E00, 0x55AA00, 0x39C600, 0x1CE300, 0x00FF00,
                      0x00E31C, 0x00C639, 0x00AA55, 0x00718E, 0x0055AA, 0x0039C6, 0x001CE3, 0x0000FF};

// Prototypes for local build, ok to leave in for Build IDE
void fillArray(int *clrArray, int n);
void bubbleSort(int *sortArray, int n);
void showArray(int *pixelArray, int n, int speed = SPEED);
int partition (int *arr, int low, int high); 
void quickSort(int *arr, int low, int high);
void showPivot(int *pixelArray, int low, int high);

int colorArray[PIXEL_COUNT];

void setup() {
  strip.begin();
  strip.setBrightness(32);
  strip.show(); // Initialize all pixels to 'off'
  randomSeed(analogRead(A0));
}

void loop() {
  fillArray(colorArray,PIXEL_COUNT);
  bubbleSort(colorArray,PIXEL_COUNT);
  delay(5000);
  fillArray(colorArray,PIXEL_COUNT);
  quickSort(colorArray, 0, PIXEL_COUNT);
  delay(5000);
}

void fillArray(int *clrArray, int n) {
  int i;

  for(i=0;i<n;i++) {
    clrArray[i] = colors[random(sizeof(colors)/4)];
  }
  showArray(clrArray, n);
  delay(3300);
  return;
}

void showArray(int *pixelArray, int n, int speed) {
  int i;

  for(i=0;i<n;i++) {
    strip.setPixelColor(i,pixelArray[i]);
  }
  strip.show();
  delay(speed);
  return;
}

void bubbleSort(int *sortArray, int n) {
  int i,j,swap;

  for(i=0;i<n;i++) {
    for(j=0;j<(n-1);j++) {
      if(sortArray[j] > sortArray[j+1]) {
        swap = sortArray[j];
        sortArray[j] = sortArray[j+1];
        sortArray[j+1] = swap;
      }
    showArray(sortArray,n);
    }
  }
}


int partition (int *arr, int low, int high) { 
    int pivot = arr[high];     
    int i = (low - 1);  
    int swap;
  
    for (int j = low; j <= high- 1; j++)     { 
        if (arr[j] <= pivot)         { 
            i++;
            swap = arr[i];
            arr[i] = arr[j];
            arr[j] = swap;    
        } 
      showArray(arr,PIXEL_COUNT, SPEED);
    }
    swap = arr[high];
    arr[high] = arr[i+1];
    arr[i+1] = swap;
    showArray(arr,PIXEL_COUNT,SPEED);
    return (i + 1); 
}

void quickSort(int *arr, int low, int high) { 
  int pi;

    if (low < high)     {
        pi = partition(arr, low, high); 
        quickSort(arr, low, pi - 1); 
        quickSort(arr, pi + 1, high); 
    } 
} 

Credits

Brian Rashap

Brian Rashap

12 projects • 107 followers
Former General Manager of US Facilities Operations at Intel Corporation. Currently loving my encore career as a teacher focused on IoT.

Comments