Skip to content

Commit

Permalink
Create Gnome_sort_cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
adityab1500 authored Oct 31, 2019
1 parent 4e8af4b commit 54ff751
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions C++/Gnome_sort_cpp
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);
}

0 comments on commit 54ff751

Please sign in to comment.