w3resource

PHP Searching and Sorting Algorithm: Gnome sort

PHP Searching and Sorting Algorithm: Exercise-9 with Solution

Write a PHP program to sort a list of elements using Gnome sort.

Gnome sort is a sorting algorithm originally proposed by Dr. Hamid Sarbazi-Azad (Professor of Computer Engineering at Sharif University of Technology) in 2000 and called "stupid sort" (not to be confused with bogosort), and then later on described by Dick Grune and named "gnome sort".
The algorithm always finds the first place where two adjacent elements are in the wrong order and swaps them. It takes advantage of the fact that performing a swap can introduce a new out-of-order adjacent pair only next to the two swapped elements.

A visualization of the gnome sort :

javaScript: gnome sort animation

Sample Solution :

PHP Code :

<?php
// Function to perform Gnome Sort on an array
function gnome_Sort($my_array){
	$i = 1; // Initialize the index
	$j = 2; // Initialize the second index
	while($i < count($my_array)){ // Loop until the index is less than the array length
		if ($my_array[$i-1] <= $my_array[$i]){ // If the previous element is less than or equal to the current element
			$i = $j; // Move to the next index
			$j++; // Increment the second index
		}else{
			// Swap the current and previous elements
			list($my_array[$i],$my_array[$i-1]) = array($my_array[$i-1],$my_array[$i]);
			$i--; // Move to the previous index
			if($i == 0){ // If at the beginning of the array
				$i = $j; // Move to the next index
				$j++; // Increment the second index
			}
		}
	}
	return $my_array; // Return the sorted array
}
$test_array = array(3, 0, 2, 5, -1, 4, 1); // Input array to be sorted
echo "Original Array :\n"; // Print original array label
echo implode(', ',$test_array ); // Print original array elements
echo "\nSorted Array :\n"; // Print sorted array label
echo implode(', ',gnome_Sort($test_array)). PHP_EOL; // Print sorted array
?>

Output:

3, 0, 2, 5, -1, 4, 1                                                
Sorted Array :                                                      
-1, 0, 1, 2, 3, 4, 5 

Explanation:

In the exercise above,

  • The "gnome_Sort()" function takes an array '$my_array' as input and sorts it using the Gnome Sort algorithm.
  • Inside the function, two indices '$i' and '$j' are initialized to 1 and 2, respectively.
  • The "while" loop continues until '$i' is less than the length of the array.
  • In each iteration, it checks if the previous element ($my_array[$i-1]) is less than or equal to the current element ($my_array[$i]).
  • If true, it moves to the next index by setting '$i' to '$j' and incrementing '$j'.
  • If false, it swaps the current and previous elements, decrements '$i', and checks if it's at the beginning of the array. If yes, it moves to the next index.
  • Finally, it returns the sorted array.

Flowchart :

Flowchart: PHP - program of Gnome sort

PHP Code Editor:


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP program to sort a list of elements using Comb sort.
Next: Write a PHP program to sort a list of elements using Bucket sort.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.