// Bubble sort in C
#include <stdio.h>
// perform the bubble sort
void bubbleSort(int array[], int size) {
// loop to access each array element
for (int step = 0; step < size - 1; ++step) {
// loop to compare array elements
for (int i = 0; i < size - step - 1; ++i) {
// compare two adjacent elements
// change > to < to sort in descending order
if (array[i] > array[i + 1]) {
// swapping occurs if elements
// are not in the intended order
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
}
}
}
// print array
void printArray(int array[], int size) {
for (int i = 0; i < size; ++i) {
}
}
int main() {
int data[] = {-2, 45, 0, 11, -9};
// find the array's length
int size = sizeof(data) / sizeof(data[0]);
bubbleSort(data, size);
printf("Sorted Array in Ascending Order:\n"); printArray(data, size);
}
Ly8gQnViYmxlIHNvcnQgaW4gQwoKI2luY2x1ZGUgPHN0ZGlvLmg+CgovLyBwZXJmb3JtIHRoZSBidWJibGUgc29ydAp2b2lkIGJ1YmJsZVNvcnQoaW50IGFycmF5W10sIGludCBzaXplKSB7CgogIC8vIGxvb3AgdG8gYWNjZXNzIGVhY2ggYXJyYXkgZWxlbWVudAogIGZvciAoaW50IHN0ZXAgPSAwOyBzdGVwIDwgc2l6ZSAtIDE7ICsrc3RlcCkgewogICAgICAKICAgIC8vIGxvb3AgdG8gY29tcGFyZSBhcnJheSBlbGVtZW50cwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBzaXplIC0gc3RlcCAtIDE7ICsraSkgewogICAgICAKICAgICAgLy8gY29tcGFyZSB0d28gYWRqYWNlbnQgZWxlbWVudHMKICAgICAgLy8gY2hhbmdlID4gdG8gPCB0byBzb3J0IGluIGRlc2NlbmRpbmcgb3JkZXIKICAgICAgaWYgKGFycmF5W2ldID4gYXJyYXlbaSArIDFdKSB7CiAgICAgICAgCiAgICAgICAgLy8gc3dhcHBpbmcgb2NjdXJzIGlmIGVsZW1lbnRzCiAgICAgICAgLy8gYXJlIG5vdCBpbiB0aGUgaW50ZW5kZWQgb3JkZXIKICAgICAgICBpbnQgdGVtcCA9IGFycmF5W2ldOwogIC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