In Java, I have a list of positive different numbers.
Each number is used as a key in hashsets IntIntHashSet fs and cs used in code below to retrieve some condition values.
Then I check the condition (if statement) and if true, swap the elements.
int[] list = // given list of positive different ints like [14, 2, 7, 19, 20, 3]
int l = list.length;
if (l > 1) {
int elI, elJ, fI, fJ, swap;
for (int i = 0; i < l; i++) {
boolean swapped = false;
for (int j = 1; j < l; j++) {
elI = list[j - 1];
elJ = list[j];
fI = fs.get(elI);
fJ = fs.get(elJ);
if (fI > fJ || (fI == fJ && cs.get(elI) > cs.get(elJ))) {
swap = list[j];
list[j] = list[j - 1];
list[j - 1] = swap;
swapped = true;
}
}
if (!swapped) break;
}
}
It looks like a Bubble sort, though I’m not very sure. I’m writing a time-consuming program and this part should be optimized as well as possible.
The main question: will it be faster to use another sorting approach like QuickSort?
Second question: will it be faster to use xor swap method without a temporary variable swap?
[EDIT]: I might have a very long list containing thousands of numbers. The one above is simple due to example.
for partial sorted array and small set of data, bubble sort would be efficient way. quicksort would be efficient only for large(huge) sets of data. you seem to be implementing bubble sort, which would be sufficient for small sets of data.
check here for efficiency for different sorting algorithms.