I found a bunch of java files online and there is one function that appears all over the place but i cannot find the definition of on google: compex
. google keeps on sending me to complex, no matter how i use symbols to make compex
an important search term. it doesnt seem to be imported from anywhere. all i have managed to find out about it is that it takes 2 single integers as inputs.
i am not a java programmer. im just trying to figure out what in the world the code means
/*
* PermSortAlgorithm.java
* Patrick Morin takes no responsibility for anything. So there.
*
*/
/**
* A PermSort Demonstration algorithm. The PermSort algorithm is due
* to Patrick Morin <http:www.scs.carleton.ca/~morin>. The algorithm
* works by trying every permutation until it finds one that's
* sorted. That's right, there are n! permutations and it takes O(n)
* time to test each one, yielding an O(nn!) algorithm. No hate mail
* please.
*
* @author Patrick Morin
*/
class PermSortAlgorithm extends SortAlgorithm {
/**
* Check if the input is sorted. Do it in a weird way so it looks
* good for the sort demo.
*/
boolean issorted(int a[], int i) throws Exception {
for (int j = a.length-1; j > 0; j--) {
compex(j, j-1);
pause();
if(a[j] < a[j-1]) {
return false;
}
}
return true;
}
/**
* Privately sort the array开发者_如何学编程 using the PermSort algorithm.
*/
boolean sort(int a[], int i) throws Exception {
int j;
// Check if array is already sorted
if (issorted(a, i)) {
return true;
}
// Array wasn't sorted so start trying permutations until we
// get the right one.
for(j = i+1; j < a.length; j++) {
compex(i, j);
pause();
int T = a[i];
a[i] = a[j];
a[j] = T;
if(sort(a, i+1)) {
return true;
}
T = a[i];
a[i] = a[j];
a[j] = T;
}
return false;
}
/**
* Sort the input using the PermSort algorithm.
*/
void sort(int a[]) throws Exception {
sort(a, 0);
}
}
It's short for compare-and-exchange.
精彩评论