Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8175513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:48:34+00:00 2026-06-06T22:48:34+00:00

I am doing a bubble sort function and encounter a variable operator problem. There

  • 0

I am doing a bubble sort function and encounter a variable operator problem. There is a switch block at the beginning to determine whether it should be sorted in ascending or descending order.
The $operator is intended to be used in the following if condition.


<?php
//bubble sort in ascending/descending order
function bubbleSort($arr, $operation="ascending"){
    switch ($operation){
        case "ascending":
            $operator = ">";
            break;
        case "descending":
            $operator = "<";
            break;
    }
    //each loop put the largest number to the top
    for ($i=0; $i<count($arr)-1; $i++){

        //compare adjacent numbers
        for ($j=0; $j<count($arr)-1-$i; $j++){

            //exchange the adjacent numbers that are arranged in undesired order
            if ($arr[$j]>$arr[$j+1]){
                $temp = $arr[$j];
                $arr[$j] = $arr[$j+1];
                $arr[$j+1] = $temp;
            }
        }
    }
    return $arr;
}
$arr1 = array(1000,10,2,20,-1,-6,-8,0,101);
$arr1 = bubbleSort($arr1, "ascending");
print_r($arr1);
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:48:38+00:00Added an answer on June 6, 2026 at 10:48 pm

    While technically it is possible to put the operator (< or >) in a string and compile an expression out of it (using eval()), most of the time you neither need nor want this. Simply assigning a boolean that decides whether to sort ascending, and then evaluating that boolean is a more common way to go.

    Your code then comes down to something like this:

    function bubbleSort($arr, $operation="ascending"){
    
        $ascending = ($operation == "ascending");
    
        //each loop put the largest number to the top
        for ($i=0; $i<count($arr)-1; $i++){
    
            //compare adjacent numbers
            for ($j=0; $j<count($arr)-1-$i; $j++){
    
                //exchange the adjacent numbers that are arranged in undesired order
                if (($ascending && ($arr[$j] > $arr[$j+1])) 
                || (!$ascending && ($arr[$j] < $arr[$j+1])))
    
                {           
                    $temp = $arr[$j];
                    $arr[$j] = $arr[$j+1];
                    $arr[$j+1] = $temp;
                }
            }
        }
        return $arr;
    }
    

    Of course you can skip the string evaluation and change the $operation="ascending" parameter to $ascending = true, leaving out the first line in the function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Doing a mysqli query and COUNTing the results, is there a preferred comparison operator
Doing the below will reproduce my problem: New WPF Project Add ListView Name the
this is my code about bubble sort. #include stdafx.h #include <iostream> #include <cstdlib> using
I have to time how long a bubble sort takes and print how long
For some reason this function is doing something weird just in Firefox function fadeOUT_sect(id)
I am implementing the bubble sort algorithm and I want it to be able
I am trying to write a Recursive Bubble sort in Java and I'm getting
Doing the getting started of Sinatra. I get this error: ./sinatra.rb:5: undefined method `get'
Doing an ajax get request works as expected using the following code: $.ajax({ type:
Doing my first SL4 MVVM RIA based application and i ran into the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.