forked from argonautica/sorting-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4e8af4b
commit 54ff751
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <iostream> | ||
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); | ||
} |