Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizing bubble sort #43

Open
make-github-pseudonymous-again opened this issue Jun 25, 2015 · 2 comments
Open

Optimizing bubble sort #43

make-github-pseudonymous-again opened this issue Jun 25, 2015 · 2 comments
Assignees

Comments

@make-github-pseudonymous-again
Copy link
Owner

@make-github-pseudonymous-again
Copy link
Owner Author

Example in PHP

function combinedBubbleSort(&$a) {
    $n = count($a);
    // bubble sorting
    for ($j = 0; $j < ($n - 1); $j++) {
        //echo "Iteration #".$j.EOL;
        $flag = false;
        $min = $j;
        for ($i = $j; $i < ($n - $j - 1); $i++) {
            //echo T.'#'.$i.T.'v'.$a[$i].EOL;
            //echo "Comparation: ";
            //printArray($a, array($i, $i + 1));
            if ($a[$i] > $a[$i + 1]) {
                //echo "Swap: ";
                list($a[$i], $a[$i + 1]) = array($a[$i + 1], $a[$i]);
                //printArray($a, array($i, $i + 1), 'blue');
                if (!$flag) $flag = true;
            }
            if ($a[$i] < $a[$min]) $min = $i;
        }
        if (!$flag) break;
        if ($min != $j) {
            //echo "Swap: ";
            list($a[$min], $a[$j]) = array($a[$j], $a[$min]);
            //printArray($a, array($min, $j), 'yellow');
        }
    }
}

@make-github-pseudonymous-again
Copy link
Owner Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant