Home > front end >  What is the name of my sorting algorithm?
What is the name of my sorting algorithm?

Time:08-05

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 = 1;
        for (int i = 0, j = 1; i < len - 1; i  , j  ) {
            if (arr[i] > arr[j]) {
                flag = 2;
                temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }
        if (flag != 2) break;
    }
}

CodePudding user response:

This is bubble sort: repeatedly loop over an array swapping adjacent elements and stop when nothing is swapped.

  • Related