Skip to content

Commit 297162b

Browse files
authored
Merge pull request #22 from gastonche/sorting/gnomesort
Added gnome sort
2 parents af36094 + 8572ffa commit 297162b

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

sorting/README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<div align="center">
2+
<img width="400" height="270" src="http://konpa.github.io/devicon/devicon.git/icons/javascript/javascript-original.svg">
3+
<br>
4+
<br>
5+
<img src="https://cdn.abranhe.com/projects/algorithms/algorithms.svg" width="400px">
6+
<br>
7+
<br>
8+
<p>Sorting ▲lgorithms implemented in Javascript</p>
9+
</div>
10+
11+
## Contents
12+
13+
- [Gnome Sort]()
14+
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"[1] (not to be confused with bogosort), and then later on described by Dick Grune and named "gnome sort".
15+
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..
16+
17+
18+
19+
## License
20+
21+
This work is licensed under a [MIT License](https://github.com/abranhe/algorithms/blob/master/LICENSE)
22+
23+
[![MIT IMG][mit-license]]((https://github.com/abranhe/algorithms/blob/master/LICENSE))
24+
25+
To the extent possible under law, [Carlos Abraham](https://go.abranhe.com/github) has waived all copyright and related or neighboring rights to this work.
26+
27+
28+
[mit-license]: https://cdn.abraham.gq/projects/algorithms/mit-license.png

sorting/gnomesort.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* 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"[1] (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.
3+
*/
4+
5+
function gnomeSort(arr)
6+
{
7+
function moveBack(i)
8+
{
9+
for( ; i > 0 && arr[i-1] > arr[i]; i--)
10+
{
11+
var t = arr[i];
12+
arr[i] = arr[i-1];
13+
arr[i-1] = t;
14+
}
15+
}
16+
for (var i = 1; i < arr.length; i++)
17+
{
18+
if (arr[i-1] > arr[i]) moveBack(i);
19+
}
20+
return arr;
21+
}
22+
23+
var arra = [3, 0, 2, 5, -1, 4, 1];
24+
console.log("Original Array Elements");
25+
console.log(arra);
26+
console.log("Sorted Array Elements");
27+
console.log(gnomeSort(arra));

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy