I have recently watched some awesome visualizations of the sorting algorithms on YouTube. QuickSort was my favorite and I thought it would be fun to try to make that algorithm for Arduino. It proved to be very easy so I decided to make a visual representation on ESP8266 web site. Again, I have used Java example to write my code. I have tried to make sorting and web server part easily distinguishable so you can take it apart if you only need one part of it.
Sorting part of the code is quite simple.
int input[] = {60, 153, 12, 13, 116, 10, 199, 54, 108, 198, 103, 115, 180, 169, 1, 26, 170, 97, 130, 71, 193, 44, 7, 72, 152, 166, 81, 19};
int output[sizeof(input) / sizeof(input[0])];
void sort() {
quickSort(0, (sizeof(output) / sizeof(output[0])) - 1);
}
We prepare our input and output array. Output is needed so our input array in left for resetting of the process when numbers are sorted. After that quickSort is called with size of full array.
void quickSort(int lowerIndex, int higherIndex) {
int i = lowerIndex;
int j = higherIndex;
int pivot = output[lowerIndex + (higherIndex - lowerIndex) / 2];
while (i <= j) {
while (output[i] < pivot) {
i++;
}
while (output[j] > pivot) {
j--;
}
if (i <= j) {
exchangeNumbers(i, j);
i++;
j--;
}
}
if (lowerIndex < j)
quickSort(lowerIndex, j);
if (i < higherIndex)
quickSort(i, higherIndex);
}
void exchangeNumbers(int i, int j) {
int temp = output[i];
output[i] = output[j];
output[j] = temp;
}
We need to pick a pivot number and rearrange the numbers so all numbers on the left side of the pivot are lesser than the pivot and greater on the right side.
ESP8266 Web ServerWeb server visualization is muck more complicated that sorting.
void buildWebsite() {
buildJavascript();
webSite = "<!DOCTYPE HTML>\n";
webSite += " <head>\n";
webSite += " <title>QuickSort</title>\n";
webSite += javaScript;
webSite += " </head>\n";
webSite += " <body onload='process()'>\n";
webSite += " <div style='height:100px;'></div>\n";
webSite += " <div style='text-align:center;'>\n";
webSite += " <div id='runtime'></div>\n";
webSite += " </div>\n";
webSite += " <form action='/' method='post'>";
webSite += " <div style='text-align:center;'>\n";
webSite += " <div style='height:20px;'></div>\n";
webSite += " <input type='submit' name='Sort'>\n";
webSite += " </div>\n";
webSite += " </form>\n";
webSite += " </body>\n";
webSite += "</HTML>\n";
}
The web page itself is simple. It has only one button that is initiating the sorting procedure and resets if the process is already started. Main JavaScript function is called on page loading. JavaScript is created separately by buildJavascript().
void buildJavascript() {
javaScript = "<script>\n";
javaScript += "var xmlHttp=createXmlHttpObject();\n";
javaScript += "function createXmlHttpObject(){\n";
javaScript += " if(window.XMLHttpRequest){\n";
javaScript += " xmlHttp=new XMLHttpRequest();\n";
javaScript += " }else{\n";
javaScript += " xmlHttp=new ActiveXObject('Microsoft.XMLHTTP');\n";
javaScript += " }\n";
javaScript += " return xmlHttp;\n";
javaScript += "}\n";
javaScript += "function process(){\n";
javaScript += " if(xmlHttp.readyState==0 || xmlHttp.readyState==4){\n";
javaScript += " xmlHttp.open('PUT','xml',true);\n";
javaScript += " xmlHttp.onreadystatechange=handleServerResponse;\n";
javaScript += " xmlHttp.send(null);\n";
javaScript += " }\n";
javaScript += " setTimeout('process()',33);\n";
javaScript += "}\n";
javaScript += "function handleServerResponse(){\n";
javaScript += " if(xmlHttp.readyState==4 && xmlHttp.status==200){\n";
javaScript += " var elem = document.createElement('span');\n";
javaScript += " elem.setAttribute('id', 'runtime');\n";
javaScript += " elem.innerHTML = this.responseText;\n";
javaScript += " document.getElementById('runtime').replaceWith(elem);\n";
javaScript += " }\n";
javaScript += "}\n";
javaScript += "</script>\n";
}
process() is called every 33ms. It requests XML response and places it into the new element that is placed instead of the existing one.
Only that element is updated and not the whole page.
response = "";
for (int e = 0; e < sizeof(output) / sizeof(output[0]); e ++) {
String color = "";
if (e == i || e == j) {
color = "#00ff00"; // Green
} else if (e == pivot) {
color = "#0000ff"; // Blue
} else {
color = "#ff0000"; // Red
}
response += "<div style='display:inline-block;width:3px;height:";
response += output[e];
response += "px;border:1px solid #000;background-color:" + color + ";'> </div>";
}
XML response consists of one div element for every number in the array being sorted. Height is determined by values from the array. The green color is used for elements being exchanged and blue is used for pivot number.
Comments