mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
change name as in contribute.md
This commit is contained in:
parent
36a88310e2
commit
8f4f8cdd7a
29
sorting/gnome_sort.cpp
Normal file
29
sorting/gnome_sort.cpp
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
#include <iostream>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
void gnomesort(int arr[], int size) {
|
||||||
|
// few easy cases
|
||||||
|
if (size <= 1) return;
|
||||||
|
|
||||||
|
int index = 0; // initialize some variables.
|
||||||
|
while (index < size) {
|
||||||
|
// check for swap
|
||||||
|
if ((index == 0) || (arr[index] >= arr[index - 1])) {
|
||||||
|
index++;
|
||||||
|
} else {
|
||||||
|
swap(arr[index], arr[index - 1]); // swap
|
||||||
|
index--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Our main function
|
||||||
|
int main() {
|
||||||
|
int arr[] = {-2, -10, 100, 35, 34, 99};
|
||||||
|
int size = sizeof(arr) / sizeof(arr[0]);
|
||||||
|
|
||||||
|
gnomesort(arr, size);
|
||||||
|
|
||||||
|
for (int i = 0; i < size; i++) printf("%d ", arr[i]);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user