Trouble Understanding Sorting Algorithm

Hi, part of the solution in the Silver Problem “Rental Service” includes the following sorting algorithm that I didn’t really understand.

public static void sort(int[] l) {
		Arrays.sort(l);
		for(int i = 0; i < l.length-1-i; i++) {
			l[i] ^= l[l.length-1-i];
			l[l.length-1-i] ^= l[i];
			l[i] ^= l[l.length-1-i];
		}
	}

Could someone help me make sense of what this does and/or how it works?

			l[i] ^= l[l.length-1-i];
			l[l.length-1-i] ^= l[i];
			l[i] ^= l[l.length-1-i];

This code just swaps the values of l[i] and l[l.length-1-i] in a rather convoluted way. See the module on Bitwise operators for more information :slight_smile:

An equivalent statement for those three lines would be:

int tmp = l[i];
l[i] = l[l.length-1-i];
l[l.length-1-i] = tmp;

Thanks, so is this code essentially just sorting an array in descending order?