localvars2 - .k-1] <= b[k. .n] for ( int k= 0; k...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
1 CS101J, Cornel 0 Declare a local variable where it logically belongs /** Sort array segment b[0. .n] */ public void selectionSort( int[] b, int n) { int temp; int j; // inv: b[0. .k-1] <= b[k. .n] for ( int k= 0; k < n; k= k+1) { // Set j to the index of the minimum of b[k. .n] j= k; for ( int p= k+1; p<= n; p= p+1) { if (b[p] < b[j]) j= p; } // Swap b[j] and b[k] temp= b[j]; b[j]= b[k]; b[k]= temp; } } inv : b sorted <= >= 0 k n Generally, close to its first use CS101J, Cornel 1 Declare local variables where they logically belong /** Sort array segment b[0. .n] */ public void selectionSort( int[] b, int n) { int j; // inv: b[0.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: .k-1] &lt;= b[k. .n] for ( int k= 0; k &lt; n; k= k+1) { // Set j to the index of the minimum of b[k. .n] j= k; for ( int p= k+1; p&lt;= n; p= p+1) { if (b[p] &lt; b[j]) j= p; } // Swap b[j] and b[k] int temp= b[j]; b[j]= b[k]; b[k]= temp; } } inv : b sorted &lt;= &gt;= 0 k n CS101J, Cornel 2 Declare local variables where they logically belong /** Sort array segment b[0. .n] */ public void selectionSort( int b, int n) { // inv: b[0. .k-1 is sorted &amp; b[0. .k-1] &lt;= b[k. .n] for ( int k= 0; k &lt; n; k= k+1) { // Set j to the index of the minimum of b[k. .n] int j= k; for ( int p= k+1; p&lt;= n; p= p+1) { if (b[p] &lt; b[j]) j= p; } // Swap b[j] and b[k] int temp= b[j]; b[j]= b[k]; b[k]= temp; } } inv : b sorted &lt;= &gt;= 0 k n...
View Full Document

This note was uploaded on 07/07/2008 for the course CS 101 taught by Professor Gries during the Spring '08 term at Cornell University (Engineering School).

Ask a homework question - tutors are online