📜  侏儒排序

📅  最后修改于: 2021-04-27 20:10:59             🧑  作者: Mango

Gnome Sort也称为愚蠢排序,是基于Garden Gnome对其花盆进行排序的概念。花园侏儒通过以下方法对花盆进行分类:

  • 他看着旁边的花盆和前一个花盆。如果它们的顺序正确,他将向前推进一个底池,否则,他将它们交换并向后迈入一个底池。
  • 如果没有先前的底池(他在底池线的起点),则向前走;如果他旁边没有锅(他在底池线的尽头),那么他就完成了。

输入

Array- arr[]  
Total elements - n

算法步骤

  1. 如果您在数组的开头,请转到右边的元素(从arr [0]到arr [1])。
  2. 如果当前数组元素大于或等于前一个数组元素,则向右走一步
if (arr[i] >= arr[i-1])
                      i++;
  1. 如果当前数组元素小于上一个数组元素,则交换这两个元素并向后退一步
if (arr[i] < arr[i-1])
                       {
                           swap(arr[i], arr[i-1]);
                           i--;
                       }
  1. 重复步骤2)和3),直到“ i”到达数组的末尾(即-“ n-1”)
  2. 如果到达数组的末尾,则停止并对该数组进行排序。

例子-

34 2 10 -9
  • 带下划线的元素是正在考虑的对。
  • “红色”是需要交换的对。
  • 交换的结果显示为“蓝色”

下面是该算法的实现。

C++
// A C++ Program to implement Gnome Sort
#include 
using namespace std;
 
// A function to sort the algorithm using gnome sort
void gnomeSort(int arr[], int n)
{
    int index = 0;
 
    while (index < n) {
        if (index == 0)
            index++;
        if (arr[index] >= arr[index - 1])
            index++;
        else {
            swap(arr[index], arr[index - 1]);
            index--;
        }
    }
    return;
}
 
// A utility function ot print an array of size n
void printArray(int arr[], int n)
{
    cout << "Sorted sequence after Gnome sort: ";
    for (int i = 0; i < n; i++)
        cout << arr[i] << " ";
    cout << "\n";
}
 
// Driver program to test above functions.
int main()
{
    int arr[] = { 34, 2, 10, -9 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    gnomeSort(arr, n);
    printArray(arr, n);
 
    return (0);
}


Java
// Java Program to implement Gnome Sort
 
import java.util.Arrays;
public class GFG {
    static void gnomeSort(int arr[], int n)
    {
        int index = 0;
 
        while (index < n) {
            if (index == 0)
                index++;
            if (arr[index] >= arr[index - 1])
                index++;
            else {
                int temp = 0;
                temp = arr[index];
                arr[index] = arr[index - 1];
                arr[index - 1] = temp;
                index--;
            }
        }
        return;
    }
 
    // Driver program to test above functions.
    public static void main(String[] args)
    {
        int arr[] = { 34, 2, 10, -9 };
 
        gnomeSort(arr, arr.length);
 
        System.out.print("Sorted sequence after applying Gnome sort: ");
        System.out.println(Arrays.toString(arr));
    }
}
 
// Code Contributed by Mohit Gupta_OMG


Python
# Python program to implement Gnome Sort
 
# A function to sort the given list using Gnome sort
def gnomeSort( arr, n):
    index = 0
    while index < n:
        if index == 0:
            index = index + 1
        if arr[index] >= arr[index - 1]:
            index = index + 1
        else:
            arr[index], arr[index-1] = arr[index-1], arr[index]
            index = index - 1
 
    return arr
 
# Driver Code
arr = [ 34, 2, 10, -9]
n = len(arr)
 
arr = gnomeSort(arr, n)
print "Sorted seqquence after applying Gnome Sort :",
for i in arr:
    print i,
 
# Contributed By Harshit Agrawal


C#
// C# Program to implement Gnome Sort
using System;
 
class GFG {
     
    static void gnomeSort(int[] arr, int n)
    {
        int index = 0;
 
        while (index < n)
        {
            if (index == 0)
                index++;
            if (arr[index] >= arr[index - 1])
                index++;
            else {
                int temp = 0;
                temp = arr[index];
                arr[index] = arr[index - 1];
                arr[index - 1] = temp;
                index--;
            }
        }
        return;
    }
 
    // Driver program to test above functions.
    public static void Main()
    {
        int[] arr = { 34, 2, 10, -9 };
         
        // Function calling
        gnomeSort(arr, arr.Length);
 
        Console.Write("Sorted sequence after applying Gnome sort: ");
 
        for (int i = 0; i < arr.Length; i++)
            Console.Write(arr[i] + " ");
    }
}
 
// This code is contributed by Sam007


PHP
= $arr[$index - 1])
            $index++;
        else
        {
            $temp = 0;
            $temp = $arr[$index];
            $arr[$index] = $arr[$index - 1];
            $arr[$index - 1] = $temp;
            $index--;
        }
    }
    echo "Sorted sequence ",
         "after Gnome sort: ";
    for ($i = 0; $i < $n; $i++)
        echo $arr[$i] . " ";
    echo "\n";
}
 
// Driver Code
$arr = array(34, 2, 10, -9);
$n = count($arr);
 
gnomeSort($arr, $n);
 
// This code is contributed
// by Sam007
?>


Javascript


输出:

Sorted sequence after applying Gnome sort: -9 2 10 34