Skip to content

Instantly share code, notes, and snippets.

@nataliefreed
Last active April 21, 2016 20:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nataliefreed/19376f81fba9172f19a907bafb6e77bf to your computer and use it in GitHub Desktop.
Save nataliefreed/19376f81fba9172f19a907bafb6e77bf to your computer and use it in GitHub Desktop.
/* Starting point for sorting algorithm implementation */
int[] array = new int[100000];
void setup()
{
//fill the array with random numbers
for(int i=0;i<array.length;i++)
{
array[i] = round(random(-1000, 1000));
}
//start a timer
int timer = millis();
int[] sortedArray = mySort(array);
//check if sorted and print time elapsed
if(isSorted(sortedArray))
{
println("Array sorted in " + (millis() - timer) / 1000.0 + " seconds. ");
}
else
{
println("Array not sorted.");
}
}
int[] swap(int[] array, int e1, int e2) {
if(e1 >= 0 && e2 >= 0 && e1 < array.length && e2 < array.length) { //if elements are within bounds of array
//do the swap
}
return array;
}
int[] insert(int[] array, int origin, int destination) {
if(origin >= 0 && destination >= 0 && origin < array.length && destination < array.length) { //if elements are within bounds of array
//do the insert
}
return array;
}
//change the name of this function to match your sorting algorithm
int[] mySort(int[] a)
{
//do the sort
return a;
}
//to check if sorted
boolean isSorted(int[] a)
{
boolean result = true;
for(int i=0;i<a.length-1;i++)
{
if(a[i] > a[i+1]) //if any element is greater than the one following it, the array is not sorted
{
result = false;
}
}
return result;
}
String toString(int[] array) {
String text = "";
for(int i=0;i<array.length;i++) {
text += "[" + i + "]:" + array[i] + " ";
}
return text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment