Modify a text file: $/practice-array-sort.pde
$/practice-array-sort.pde
//// practice-array-sort // Program to demonstrate arrays and algorithms void setup() { size(500,300); background(127,255,255); fill(0); text("Sort an array.", 10,10); text( "practice-array-sort // Program to demonstrate arrays and algorithms", 10, height-20 ); //// int[] a= new int[] { 55, 22, 33, 77, 11, 99, 88, 66, 44 }; int big; int many= a.length; // Find the biggest number. big= a[0]; for (int j=0; j
big) big= a[j]; } // Output the result. println("Biggest: "+big); fill(0,0,0); text("Biggest value in array: ", 10,50); text(big, 10,62); //// Now, sort the array. sortArray(a, many); //// Display array, showArray(a, many); } void sortArray( int a[], int many) { //// Sort the array int n=many; while( n>1 ) { //// Find biggest and move it to the end. int k= whereBig( a, n ); swap( a, k, n-1 ); // Move biggest to end. n--; // Now, shrink the array. } } int whereBig( int a[], int n ) { //// Return index of biggest. int w= 0; //// Start with first element. for (int j=1; j
a[w]) w= j; } return w; } void swap( int a[], int i, int j ) { //// Swap 2 elements of an array. int tmp; tmp= a[i]; a[i]= a[j]; a[j]= tmp; } void showArray( int a[], int many) { //// Show the array int x=10, y=100; // Print and display for (int j=1; j