Posted on by Kalkicode
Code Sorting

# Gnome Sort

Gnome Sort, also known as Stupid Sort, is a simple sorting algorithm that works by repeatedly swapping adjacent elements if they are in the wrong order. It is similar to Insertion Sort, but it moves elements in a slightly different way.

The algorithm works by starting at the beginning of the array and comparing adjacent elements. If the elements are in the correct order, it moves on to the next pair of adjacent elements. If the elements are in the wrong order, it swaps them and moves back one position to check the previous pair of adjacent elements. This process is repeated until the entire array is sorted.

Here is a step-by-step example of how Gnome Sort works on an unsorted array:

1. Start at the first element of the array.
2. Compare the first and second elements. If they are in the wrong order, swap them and move back one position to compare the first and second elements again. If they are in the correct order, move on to the next pair of adjacent elements.
3. Continue comparing and swapping adjacent elements until you reach the end of the array.
4. Once you reach the end of the array, move back to the beginning and repeat the process until the entire array is sorted.

Gnome Sort has a time complexity of O(n^2) in the worst case, where n is the number of elements in the array. It is not very efficient for large arrays, but it is simple to understand and implement, and it can be useful for small or nearly sorted arrays.

Here given code implementation process.

``````// C Program
// Sort array elements by using gnome sort
#include <stdio.h>

//Function which is display arr elements
void display(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("  %d", arr[i]);
}
printf("\n");
}
// Swapping the array elements in given location
void swap_data(int arr[], int first, int second)
{
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
//Perform the gnome sort in given array
void gnome_sort(int arr[], int size)
{
int position = 1;
while (position < size)
{
if (arr[position] >= arr[position - 1])
{
position++;
}
else
{
swap_data(arr, position, position - 1);
if (position > 1)
{
position--;
}
}
}
}
int main()
{
//Define an array of integers
int arr[] = {
11,
8,
3,
8,
12,
-1,
-3,
1,
4,
7,
5
};
//Get the size of arr
int size = sizeof(arr) / sizeof(arr[0]);
//Before sort
printf("\n  Before Sort  :\n");
display(arr, size);
//Sort element
gnome_sort(arr, size);
//After sort
printf("\n  After Sort  :\n");
display(arr, size);
return 0;
}``````

#### Output

``````  Before Sort  :
11  8  3  8  12  -1  -3  1  4  7  5

After Sort  :
-3  -1  1  3  4  5  7  8  8  11  12``````
``````/*
Java Program
Sort array elements by using gnome sort
*/
class MySort
{
//Function which is display arr elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print(" " + arr[i]);
}
System.out.print("\n");
}
// Swapping the array elements in given location
public void swap_data(int[] arr, int first, int second)
{
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
//Perform the gnome sort in given array
public void gnome_sort(int[] arr, int size)
{
int position = 1;
while (position < size)
{
if (arr[position] >= arr[position - 1])
{
// When pairwise element already sorted
position++;
}
else
{
// When need to sort pairwise element
swap_data(arr, position, position - 1);
if (position > 1)
{
position--;
}
}
}
}
public static void main(String[] args)
{
MySort obj = new MySort();
//Define an  of integers
int[] arr = {
11,
8,
3,
8,
12,
-1,
-3,
1,
4,
7,
5
};
//Get the size of array
int size = arr.length;
System.out.print("\n Before Sort :\n");
obj.display(arr, size);
//Sort element
obj.gnome_sort(arr, size);
System.out.print("\n After Sort :\n");
obj.display(arr, size);
}
}``````

#### Output

`````` Before Sort :
11 8 3 8 12 -1 -3 1 4 7 5

After Sort :
-3 -1 1 3 4 5 7 8 8 11 12``````
``````//Include header file
#include <iostream>

using namespace std;

/*
C++ Program
Sort array elements by using gnome sort
*/
class MySort
{
public:
//Function which is display arr elements
void display(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << " " << arr[i];
}
cout << "\n";
}
// Swapping the array elements in given location
void swap_data(int arr[], int first, int second)
{
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
//Perform the gnome sort in given array
void gnome_sort(int arr[], int size)
{
int position = 1;
while (position < size)
{
if (arr[position] >= arr[position - 1])
{
// When pairwise element already sorted
position++;
}
else
{
// When need to sort pairwise element
this->swap_data(arr, position, position - 1);
if (position > 1)
{
position--;
}
}
}
}
};
int main()
{
MySort obj = MySort();
int arr[] = {
11 , 8 , 3 , 8 , 12 , -1 , -3 , 1 , 4 , 7 , 5
};
//Get the size of array
int size = sizeof(arr) / sizeof(arr[0]);
cout << "\n Before Sort :\n";
obj.display(arr, size);
//Sort element
obj.gnome_sort(arr, size);
cout << "\n After Sort :\n";
obj.display(arr, size);
return 0;
}``````

#### Output

`````` Before Sort :
11 8 3 8 12 -1 -3 1 4 7 5

After Sort :
-3 -1 1 3 4 5 7 8 8 11 12``````
``````//Include namespace system
using System;
/*
C# Program
Sort array elements by using gnome sort
*/
class MySort
{
//Function which is display arr elements
public void display(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write(" " + arr[i]);
}
Console.Write("\n");
}
// Swapping the array elements in given location
public void swap_data(int[] arr, int first, int second)
{
int temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
//Perform the gnome sort in given array
public void gnome_sort(int[] arr, int size)
{
int position = 1;
while (position < size)
{
if (arr[position] >= arr[position - 1])
{
// When pairwise element already sorted
position++;
}
else
{
// When need to sort pairwise element
swap_data(arr, position, position - 1);
if (position > 1)
{
position--;
}
}
}
}
public static void Main(String[] args)
{
MySort obj = new MySort();
int[] arr = {
11 , 8 , 3 , 8 , 12 , -1 , -3 , 1 , 4 , 7 , 5
};
//Get the size of array
int size = arr.Length;
Console.Write("\n Before Sort :\n");
obj.display(arr, size);
//Sort element
obj.gnome_sort(arr, size);
Console.Write("\n After Sort :\n");
obj.display(arr, size);
}
}``````

#### Output

`````` Before Sort :
11 8 3 8 12 -1 -3 1 4 7 5

After Sort :
-3 -1 1 3 4 5 7 8 8 11 12``````
``````<?php
/*
Php Program
Sort array elements by using gnome sort
*/
class MySort
{
//Function which is display arr elements
public  function display( \$arr, \$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
echo " ". \$arr[\$i];
}
echo "\n";
}
// Swapping the array elements in given location
public  function swap_data( & \$arr, \$first, \$second)
{
\$temp = \$arr[\$first];
\$arr[\$first] = \$arr[\$second];
\$arr[\$second] = \$temp;
}
//Perform the gnome sort in given array
public  function gnome_sort( & \$arr, \$size)
{
\$position = 1;
while (\$position < \$size)
{
if (\$arr[\$position] >= \$arr[\$position - 1])
{
// When pairwise element already sorted
\$position++;
}
else
{
// When need to sort pairwise element
\$this->swap_data(\$arr, \$position, \$position - 1);
if (\$position > 1)
{
\$position--;
}
}
}
}
}

function main()
{
\$obj = new MySort();
//Define an  of integers
\$arr = array(11, 8, 3, 8, 12, -1, -3, 1, 4, 7, 5);
//Get the size of array
\$size = count(\$arr);
echo "\n Before Sort :\n";
\$obj->display(\$arr, \$size);
//Sort element
\$obj->gnome_sort(\$arr, \$size);
echo "\n After Sort :\n";
\$obj->display(\$arr, \$size);
}
main();``````

#### Output

`````` Before Sort :
11 8 3 8 12 -1 -3 1 4 7 5

After Sort :
-3 -1 1 3 4 5 7 8 8 11 12``````
``````/*
Node Js Program
Sort array elements by using gnome sort
*/
class MySort
{
//Function which is display arr elements
display(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write(" " + arr[i]);
}
process.stdout.write("\n");
}
// Swapping the array elements in given location
swap_data(arr, first, second)
{
var temp = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
//Perform the gnome sort in given array
gnome_sort(arr, size)
{
var position = 1;
while (position < size)
{
if (arr[position] >= arr[position - 1])
{
// When pairwise element already sorted
position++;
}
else
{
// When need to sort pairwise element
this.swap_data(arr, position, position - 1);
if (position > 1)
{
position--;
}
}
}
}
}

function main()
{
var obj = new MySort();
//Define an  of integers
var arr = [11, 8, 3, 8, 12, -1, -3, 1, 4, 7, 5];
//Get the size of array
var size = arr.length;
process.stdout.write("\n Before Sort :\n");
obj.display(arr, size);
//Sort element
obj.gnome_sort(arr, size);
process.stdout.write("\n After Sort :\n");
obj.display(arr, size);
}
main();``````

#### Output

`````` Before Sort :
11 8 3 8 12 -1 -3 1 4 7 5

After Sort :
-3 -1 1 3 4 5 7 8 8 11 12``````
``````#   Python 3 Program
#   Sort array elements by using gnome sort

class MySort :
# Function which is display arr elements
def display(self, arr, size) :
i = 0
while (i < size) :
print(" ", arr[i], end = "")
i += 1

print("\n", end = "")

#  Swapping the array elements in given location
def swap_data(self, arr, first, second) :
temp = arr[first]
arr[first] = arr[second]
arr[second] = temp

# Perform the gnome sort in given array
def gnome_sort(self, arr, size) :
position = 1
while (position < size) :
if (arr[position] >= arr[position - 1]) :
#  When pairwise element already sorted
position += 1
else :
#  When need to sort pairwise element
self.swap_data(arr, position, position - 1)
if (position > 1) :
position -= 1

def main() :
obj = MySort()
# Define an  of integers
arr = [11, 8, 3, 8, 12, -1, -3, 1, 4, 7, 5]
# Get the size of array
size = len(arr)
print("\n Before Sort :")
obj.display(arr, size)
# Sort element
obj.gnome_sort(arr, size)
print("\n After Sort :")
obj.display(arr, size)

if __name__ == "__main__": main()``````

#### Output

`````` Before Sort :
11  8  3  8  12  -1  -3  1  4  7  5

After Sort :
-3  -1  1  3  4  5  7  8  8  11  12``````
``````#   Ruby Program
#   Sort array elements by using gnome sort

class MySort

# Function which is display arr elements
def display(arr, size)

i = 0
while (i < size)

print(" ", arr[i])
i += 1
end
print("\n")
end
#  Swapping the array elements in given location
def swap_data(arr, first, second)

temp = arr[first]
arr[first] = arr[second]
arr[second] = temp
end
# Perform the gnome sort in given array
def gnome_sort(arr, size)

position = 1
while (position < size)

if (arr[position] >= arr[position - 1])

#  When pairwise element already sorted
position += 1
else

#  When need to sort pairwise element
self.swap_data(arr, position, position - 1)
if (position > 1)

position -= 1
end
end
end
end
end
def main()

obj = MySort.new()
# Define an  of integers
arr = [11, 8, 3, 8, 12, -1, -3, 1, 4, 7, 5]
# Get the size of array
size = arr.length
print("\n Before Sort :\n")
obj.display(arr, size)
# Sort element
obj.gnome_sort(arr, size)
print("\n After Sort :\n")
obj.display(arr, size)
end
main()``````

#### Output

`````` Before Sort :
11 8 3 8 12 -1 -3 1 4 7 5

After Sort :
-3 -1 1 3 4 5 7 8 8 11 12
``````
``````/*
Scala Program
Sort array elements by using gnome sort
*/
class MySort
{
//Function which is display arr elements
def display(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print(" " + arr(i));
i += 1;
}
print("\n");
}
// Swapping the array elements in given location
def swap_data(arr: Array[Int], first: Int, second: Int): Unit = {
var temp: Int = arr(first);
arr(first) = arr(second);
arr(second) = temp;
}
//Perform the gnome sort in given array
def gnome_sort(arr: Array[Int], size: Int): Unit = {
var position: Int = 1;
while (position < size)
{
if (arr(position) >= arr(position - 1))
{
// When pairwise element already sorted
position += 1;
}
else
{
// When need to sort pairwise element
swap_data(arr, position, position - 1);
if (position > 1)
{
position -= 1;
}
}
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MySort = new MySort();
//Define an  of integers
var arr: Array[Int] = Array(11, 8, 3, 8, 12, -1, -3, 1, 4, 7, 5);
//Get the size of array
var size: Int = arr.length;
print("\n Before Sort :\n");
obj.display(arr, size);
//Sort element
obj.gnome_sort(arr, size);
print("\n After Sort :\n");
obj.display(arr, size);
}
}``````

#### Output

`````` Before Sort :
11 8 3 8 12 -1 -3 1 4 7 5

After Sort :
-3 -1 1 3 4 5 7 8 8 11 12``````
``````/*
Swift Program
Sort array elements by using gnome sort
*/
class MySort
{
//Function which is display arr elements
func display(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print(" ", arr[i], terminator: "");
i += 1;
}
print("\n", terminator: "");
}
// Swapping the array elements in given location
func swap_data(_ arr: inout[Int], _ first: Int, _ second: Int)
{
let temp: Int = arr[first];
arr[first] = arr[second];
arr[second] = temp;
}
//Perform the gnome sort in given array
func gnome_sort(_ arr: inout[Int], _ size: Int)
{
var position: Int = 1;
while (position < size)
{
if (arr[position] >= arr[position - 1])
{
// When pairwise element already sorted
position += 1;
}
else
{
// When need to sort pairwise element
self.swap_data(&arr, position, position - 1);
if (position > 1)
{
position -= 1;
}
}
}
}
}
func main()
{
let obj: MySort = MySort();
//Define an of integers
var arr: [Int] = [11, 8, 3, 8, 12, -1, -3, 1, 4, 7, 5];
//Get the size of array
let size: Int = arr.count;
print("\n Before Sort :");
obj.display(arr, size);
//Sort element
obj.gnome_sort(&arr, size);
print("\n After Sort :");
obj.display(arr, size);
}
main();``````

#### Output

`````` Before Sort :
11  8  3  8  12  -1  -3  1  4  7  5

After Sort :
-3  -1  1  3  4  5  7  8  8  11  12``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post