### Question:
NormalizedKeySorter implements InMemorySorter<T> { @Override public void swap(int i, int j) { final int bufferNumI = i / this.indexEntriesPerSegment; final int segmentOffsetI = (i % this.indexEntriesPerSegment) * this.indexEntrySize; final int bufferNumJ = j / this.indexEntriesPerSegment; final int segmentOffsetJ = (j % this.indexEntriesPerSegment) * this.indexEntrySize; final MemorySegment segI = this.sortIndex.get(bufferNumI); final MemorySegment segJ = this.sortIndex.get(bufferNumJ); MemorySegment.swapBytes(segI, segJ, this.swapBuffer, segmentOffsetI, segmentOffsetJ, this.indexEntrySize); } NormalizedKeySorter(TypeSerializer<T> serializer, TypeComparator<T> comparator, List<MemorySegment> memory);  NormalizedKeySorter(TypeSerializer<T> serializer, TypeComparator<T> comparator, 
			List<MemorySegment> memory, int maxNormalizedKeyBytes); @Override void reset(); @Override boolean isEmpty(); @Override List<MemorySegment> dispose(); @Override long getCapacity(); @Override long getOccupancy(); @Override T getRecord(T reuse, int logicalPosition); @Override boolean write(T record); @Override int compare(int i, int j); @Override void swap(int i, int j); @Override int size(); @Override final MutableObjectIterator<T> getIterator(); @Override void writeToOutput(final ChannelWriterOutputView output); @Override void writeToOutput(final ChannelWriterOutputView output, final int start, int num);  }### Answer:
@Test public void testSwap() throws Exception { final int numSegments = MEMORY_SIZE / MEMORY_PAGE_SIZE; final List<MemorySegment> memory = this.memoryManager.allocatePages(new DummyInvokable(), numSegments); NormalizedKeySorter<Record> sorter = newSortBuffer(memory); TestData.Generator generator = new TestData.Generator(SEED, KEY_MAX, VALUE_LENGTH, KeyMode.RANDOM, ValueMode.RANDOM_LENGTH); Record record = new Record(); int num = -1; do { generator.next(record); num++; } while (sorter.write(record)); int start = 0, end = num - 1; while (start < end) { sorter.swap(start++, end--); } generator.reset(); Record readTarget = new Record(); int i = num - 1; while (i >= 0) { generator.next(record); readTarget = sorter.getRecord(readTarget, i--); Key rk = readTarget.getField(0, Key.class); Key gk = record.getField(0, Key.class); Value rv = readTarget.getField(1, Value.class); Value gv = record.getField(1, Value.class); Assert.assertEquals("The re-read key is wrong", gk, rk); Assert.assertEquals("The re-read value is wrong", gv, rv); } this.memoryManager.release(sorter.dispose()); }