+/EE422C
software design
git clone https://git.julia.blue/+/EE422C
ead272f
master
1public final class SelectionSort { 2/** 3* Implementation of selection sort algorithm. 4* 5* @param x The integer array to sort in place 6*/ 7public static void sort (int []x ) { 8for (int i =0 ;i <x .length -1 ;i ++) { 9 10// Assume i is index of smallest value in array 11int min =i ; 12 13// Check all other values in array to find smallest value 14for (int j =i +1 ;j <x .length ;j ++) {// FIX 1: need to check last value 15if (x [j ] <x [min ]) { 16min =j ; 17 } 18 } 19 20// Swap smallest value to its correct location 21if (i !=min ) {// FIX 2: don't swap a value with itself or the xor will kill it 22swap (x ,i ,min ); 23 } 24 } 25 } 26 27/** 28* Swap two values in place using XOR algorithm. 29* 30* @param x The array of values 31* @param a Index of first value in x 32* @param b Index of second value in x 33*/ 34static void swap (int []x ,int a ,int b ) { 35x [a ] ^=x [b ]; 36x [b ] ^=x [a ]; 37x [a ] ^=x [b ];// FIX 3: XORing a with itself clears it 38 } 39}