int b[] = {11, 7, 3, 99, 113};
const int sizeOFarray = sizeof(b) / sizeof(b[0]); // Calculating the size of the array
int C[5]; // Array to hold the sorted values
int indices[5]; // Array to hold the original indices
// Function to sort the array and keep track of original indices
void sortArray(int a[], int b[], int indices[], int size) {
// Copy original array to b and store original indices
for (int i = 0; i < size; i++) {
b[i] = a[i];
indices[i] = i; // Store original index
}
// Bubble Sort algorithm
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (b[j] > b[j + 1]) {
// Swap values in b
int tempValue = b[j];
b[j] = b[j + 1];
b[j + 1] = tempValue;
// Swap corresponding indices
int tempIndex = indices[j];
indices[j] = indices[j + 1];
indices[j + 1] = tempIndex;
}
}
}
}
void setup() {
Serial.begin(9600);
// Sort the array b and store the result in C
sortArray(b, C, indices, sizeOFarray);
// Printing sorted array with original indices
for (int i = 0; i < sizeOFarray; i++) {
Serial.print(i + 1); // Position from 1 to 5
Serial.print(") C[");
Serial.print(indices[i]); // Original index
Serial.print("] = ");
Serial.println(C[i]); // Sorted value
}
}
void loop() {}