// This snippet of code does time a swap sort // which is very slow and not optimized. // Change one character to speed it up! // Then modify it to halt when sorted // The surprize is not so much in // magnitude, but in direction ! int size, last, temp; long first, second, duration; //Timer size = 1000; //whatever last = size - 1; int a[] = new int [size]; // Fill the array with random int percents 0 to 100 for (int i = 0; i < size; i++ ) { a[ i ] = (int) (Math.random() * 101); }//end for i first = System.currentTimeMillis(); //Begin timer // Sort the array for (int i = 0; i < size; i++) { for (int j = 0; j < last; j++) { if (a[j] <= a[j+1]) { // swap adjacent items temp = a[ j ]; a[ j ] = a[j+1]; a[j+1] = temp; }//end if swap }//end for j }//end for i second = System.currentTimeMillis(); //stop timer System.out.print ("The duration is "); duration = second - first; System.out.println (duration);