简体   繁体   中英

What is the name of my sorting algorithm?

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;
    }
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM