# 侏儒排序

## 解释

```procedure gnomeSort(a[]):
pos := 0
while pos < length(a):
if (pos == 0 or a[pos] >= a[pos-1]):
pos := pos + 1
else:
swap a[pos] and a[pos-1]
pos := pos - 1
```

### 样例

[5, 3, 2, 4]    a[pos] < a[pos-1]，交换
[3, 5, 2, 4]    a[pos] >= a[pos-1]，pos自增
[3, 5, 2, 4]    a[pos] < a[pos-1]，交换；pos > 1，pos自减
[3, 2, 5, 4]    a[pos] < a[pos-1]，交换；pos <= 1，pos自增
[2, 3, 5, 4]    a[pos] >= a[pos-1]，pos自增
[2, 3, 5, 4]    a[pos] < a[pos-1]，交换；pos > 1，pos自减
[2, 3, 4, 5] a[pos] >= a[pos-1]，pos自增
[2, 3, 4, 5] a[pos] >= a[pos-1]，pos自增
[2, 3, 4, 5]    pos == length(a)，完成

## 参考文献

1. ^ Sarbazi-Azad, Hamid. Stupid Sort: A new sorting algorithm (PDF). Newsletter (Computing Science Department, Univ. of Glasgow). 2 October 2000, (599): 4 [25 November 2014]. （原始内容存档 (PDF)于2012-03-07）.
2. ^ Gnome Sort - The Simplest Sort Algorithm. Dickgrune.com. 2000-10-02 [2017-07-20]. （原始内容存档于2017-08-31）.
3. ^ Paul E. Black. gnome sort. Dictionary of Algorithms and Data Structures. U.S. National Institute of Standards and Technology. [2011-08-20]. （原始内容存档于2011-08-11）.