Question

Can someone explain what is happening in each line?: function HW1a($array1 ) { do { $sw...

Can someone explain what is happening in each line?:


function HW1a($array1 )
{
do
{
$sw = false;
for( $i = 0, $c = count( $array1 ) - 1; $i < $c; $i++ )
{
if( $array1[$i] > $array1[$i + 1] )
{
list( $array1[$i + 1], $array1[$i] ) =
array( $array1[$i], $array1[$i + 1] );
$sw = true;
}
}
}
while( $sw );
return $array1;
}
$array2 = array(3, 0, 2, 5, -1, 4, 1);
echo "Original Array :\n";
echo implode(', ',$array2 );
echo "\nOther Array\n:";
echo implode(', ',HW1a($array2)). PHP_EOL;

0 0
Add a comment Improve this question Transcribed image text
Answer #1

<?php
// in this method we are soring the array from lowest to highest
function HW1a($array1 )
{
do
{
$sw = false;
for( $i = 0, $c = count( $array1 ) - 1; $i < $c; $i++ )
{
// if current element is greater than next element than swapping

// using bubble sort to sort the values
if( $array1[$i] > $array1[$i + 1] )
{
list( $array1[$i + 1], $array1[$i] ) =
array( $array1[$i], $array1[$i + 1] );

// making a flue to ensure swapping is occured
$sw = true;
}
}
}
while( $sw );
return $array1;
}
// delcared the array with elements
$array2 = array(3, 0, 2, 5, -1, 4, 1);
// printing the original array
echo "Original Array :";
echo implode(', ',$array2 );
// calling HW1a method to sort the array and printing the values
echo "\nanOther Arrayn:";
echo implode(', ',HW1a($array2)). PHP_EOL;
?>

Add a comment
Know the answer?
Add Answer to:
Can someone explain what is happening in each line?: function HW1a($array1 ) { do { $sw...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Last assignment: C programming Submission must include: a function, an array, and a menu system Goal...

    Last assignment: C programming Submission must include: a function, an array, and a menu system Goal is to create a program that will generate a random exercise for the target area selected and the number of times it will be done (all of the options except cardio) or the amount of time doing the task.(the cardio option), but I'm having trouble figuring out how to generate a random option using an array. This is what I have so far... #include...

  • Can someone please explain this piece of java code line by line as to what they...

    Can someone please explain this piece of java code line by line as to what they are doing and the purpose of each line (you can put it in the code comments). Code: import java.util.*; public class Array { private Integer[] array; // NOTE: Integer is an Object. Array() {     super();     array = new Integer[0]; } Array(Array other) {     super();     array = other.array.clone(); // NOTE: All arrays can be cloned. } void add(int value) {    ...

  • Lanuage C++ (Beginner level) Please...I need help with this assignment If I still have question, can...

    Lanuage C++ (Beginner level) Please...I need help with this assignment If I still have question, can i contact you? -------------------------- Program 1 - WRITE ALL THE CODE in ONE FILE...   MyArrayPtrArith1.cpp Step 1 - Define the class Write a class, myArrayClass, that creates an integer array. * Add an 'arraySize' variable, initialize to zero ( default constructor function ) * Create int * ptrArray ( default constructor set to NULL ) * Add a default constructor ( see above for...

  • Can someone let me know what to do for this? In C Programming. HERE ARE INSTRUCTIONS:...

    Can someone let me know what to do for this? In C Programming. HERE ARE INSTRUCTIONS: it is for a yahtzee game program. Write function sumChance to do the following a. Return type int b. Parameter list array of dice c. Declare int variable sum d. Declare int variable die e. Loop through the dice array adding the value of each die to the variable sum f. Return variable sum Write function checkLgStraight to do the following a. Return type...

  • Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In...

    Introduction: One of the most important uses of pointers is for dynamic allocation of memory. In C++ there are commands that let the user request a chunk of memory from the operating system, and use this memory to store data. There are also commands to return memory back to the O/S when the program is finished using the data. In this lab, we will explore some of the things that can go wrong when using dynamic memory and discuss how...

  • So. I'm sick and I need help figuring out whatever is going on with this. #include...

    So. I'm sick and I need help figuring out whatever is going on with this. #include using namespace std; //prototypes int getExchangesInBubbleSort(int[], int); int getExchangesInSelectionSort(int[], int); int main() {    char choice;       cout << "1. Search Benchmarks\n";    cout << "2. Sorting Benchmarks\n";    do    {        int input;        cout << "Please enter the program you would like to run. \n";        cin >> input;               if (input == 1)...

  • 18.12 What does the following program do? (Please explain briefly) 1 // Exercise 18.12: MysteryClass.java 2...

    18.12 What does the following program do? (Please explain briefly) 1 // Exercise 18.12: MysteryClass.java 2 public class MysteryClass { 3 public static int mystery(int[] array2, int size) 4 if (size == 1) { 5 return array2[0]; 6 } 7 else { 8 return array2[size - 1] + mystery(array2, size - 1); 9 } 10 } 11 12 public static void main(String[] args) { 13 int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 14 15...

  • Can someone fix this python program? I'm trying to do three things: Create a function that...

    Can someone fix this python program? I'm trying to do three things: Create a function that takes in a string as a parameter. Then create a dictionary of characters to integers, where the integer represents how many times the number of times the character appears in the word. Create a function that takes two lists as parameters and returns the elements they have in common of the two lists. Ask the user to create a list of numbers and keep...

  • Lab 10A Measure the program execution time One easy way to measure the program execution time is ...

    Lab 10A Measure the program execution time One easy way to measure the program execution time is encapsulate the useful timing functionality of C++ chrono library, This is illustrated inhttps://www.learncpp.com/cpp-tutorial/8-16-timing-your-code/ The example usingChronoTimer.cpp (Github/m10) is an example program using this chrono Timer class object to measure the Sorting on on an integer array of 10000 random integers based on the Bubble Sort vs. the C++ built-in Sort (an https://www.quora.com/Which-sorting-algorithm-does-STL-standard-template-library-use-in-c++. ) Please measure the performance of sorting the same array used...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT