Modify a text file: $/practice-array-2.pde
$/practice-array-2.pde
//// practice-array-2 // Program to demonstrate arrays and algorithms int[] a= new int[] { 55, 22, 33, 77, 11, 99, 88, 66, 44 }; int many= a.length; int x=10, y=100; void setup() { size(500,300); background(127,255,255); text( "practice-array-2 // Program to demonstrate arrays and algorithms", 10, height-20 ); fill(0); text("Press the 's' key to sort the array.", 10,10); showArray(a,many); //// int big; // 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); } void draw() { } void keyPressed() { if (key=='s') { background(255,127,255); text("SORTED ARRAY:", 10,10); y= 50; //// 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 // Display the array. for (int j=1; j