Reputation: 31
I have written a sorting algorithm. What is the name of this algorithm?
void sort(int *arr, size_t len) {
int flag = 1, temp;
while (flag != 0) {
flag = 0;
for (int i = 0, j = 1; i < len - 1; i++, j++) {
if (arr[i] > arr[j]) {
flag = 1;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
Upvotes: 0
Views: 88
Reputation: 361615
This is bubble sort: repeatedly loop over an array swapping adjacent elements and stop when nothing is swapped.
Upvotes: 4