59 lines
1.7 KiB
C++
Raw Normal View History

2020-06-12 11:54:50 +04:00
/**
* Copyright 2020 @author beqakd
* @file
* A basic implementation of gnome sort algorithm.
*/
2020-06-05 18:09:55 +04:00
2020-06-12 11:54:50 +04:00
#include <iostream> // for io operations
/**
* Copyright 2020 @author beqakd
* @file
* A basic implementation of gnome sort algorithm.
* Gnome sort algorithm is not the best one. But it is used widely
* it looks two elements prev one and next one. If they are on
* right order it forwards, otherwise it swaps elements.
* @param arr our array of elements.
* @param size size of given array
* @return it just changes array of pointer so it does not needs to return.
* time Complexity:
* O(n^2)
* Some cases it works on O(n), but overall time is O(n^2)
*/
2020-06-12 12:43:05 +04:00
template <class T>
void gnomeSort(T arr[], int size) {
2020-06-08 10:22:45 +04:00
// few easy cases
if (size <= 1) return;
2020-06-05 18:09:55 +04:00
2020-06-08 10:22:45 +04:00
int index = 0; // initialize some variables.
while (index < size) {
// check for swap
2020-06-12 12:43:05 +04:00
if ((index == 0) || (arr[index] >= arr[index - 1])) {
2020-06-08 10:22:45 +04:00
index++;
} else {
std::swap(arr[index], arr[index - 1]); // swap
index--;
}
2020-06-05 18:09:55 +04:00
}
}
2020-06-08 10:22:45 +04:00
2020-06-12 11:54:50 +04:00
/**
* Our main function with example of sort method.
*/
2020-06-05 18:09:55 +04:00
int main() {
2020-06-12 12:43:05 +04:00
// Example 1. Creating array of int,
2020-06-08 10:22:45 +04:00
int arr[] = {-22, 100, 150, 35, -10, 99};
int size = sizeof(arr) / sizeof(arr[0]);
2020-06-12 11:54:50 +04:00
gnomeSort(arr, size);
2020-06-12 12:43:05 +04:00
for (int i = 0; i < size; i++) std::printf("%d ", arr[i]);
std:: cout << "\n" << std:: endl;
// Example 2. Creating array of doubles.
double double_arr[6] = {-100.2, 10.2, 20.0, 9.0, 7.5, 7.2};
size = sizeof(double_arr) / sizeof(double_arr[0]);
gnomeSort(double_arr, size);
for (int i = 0; i < size; i++) std::cout << double_arr[i] << " ";
2020-06-08 10:22:45 +04:00
return 0;
2020-06-05 18:09:55 +04:00
}