First of all, this is a homework assignment. I’m trying to make some bubbleSort function in C++ with an offset. The function must take an array input with an offset with count elements.
For example: a[5] = {90, 9, 2, 10, 5} -> bubbleSort(a, 1, 4) -> {90, 2, 5, 9, 10}
Currently here is what my code looks like :
void bubbleSort(int arr[], int offset, int count) {
bool swapped = true;
int pivot = offset;
while (swapped) {
swapped = false;
pivot++;
for (offset ; offset < count - pivot; offset ++) {
if (arr[offset] > arr[offset+1]) {
arr[offset] ^= arr[offset+1];
arr[offset+1] ^= arr[offset];
arr[offset] ^= arr[offset+1];
swapped = true;
}
}
}
}
I think my offset checking is off the index, could you please tell me where I’ve gone wrong?
Rather than getting all tangled up with the offset, think of the problem like this: