diff --git a/C++/Gnome_sort_cpp b/C++/Gnome_sort_cpp new file mode 100644 index 0000000..8897768 --- /dev/null +++ b/C++/Gnome_sort_cpp @@ -0,0 +1,41 @@ +#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); +}