Created
October 29, 2020 06:29
-
-
Save trolley813/c983de217e69f7a3387cf7c7a77ceb7e to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #include <iostream> | |
| #include <ctime> | |
| #include <string> | |
| #include <fstream> | |
| using namespace std; | |
| template <typename T> | |
| void bubble_sort(T* array, int size) | |
| { | |
| for (int i = size; i >= 0; i--) { | |
| for (int j = 0; j < i - 1; j++) { | |
| if (array[j] > array[j + 1]) { | |
| T temp = array[j]; | |
| array[j] = array[j + 1]; | |
| array[j + 1] = temp; | |
| } | |
| } | |
| } | |
| } | |
| template <> | |
| void bubble_sort<char*>(char** array, int size) { | |
| for (int i = size; i >= 0; i--) { | |
| for (int j = 0; j < i - 1; j++) { | |
| if (strcmp(array[j], array[j + 1]) > 0) { | |
| char* temp = array[j]; | |
| array[j] = array[j + 1]; | |
| array[j + 1] = temp; | |
| } | |
| } | |
| } | |
| } | |
| template <typename T> | |
| void print_array(T* array, int size) { | |
| for (int i = 0; i < size; i++) { | |
| cout << array[i] << " "; | |
| } | |
| cout << endl; | |
| } | |
| int main() { | |
| int a[] = { 1, 8, 2, 4, 5, 3, 7, 6 }; | |
| bubble_sort(a, 8); | |
| print_array(a, 8); | |
| double b[] = { 6, 4, 2, 10, 3, 7, 8, 1, 5, 9 }; | |
| bubble_sort(b, 10); | |
| print_array(b, 10); | |
| char* s[] = { | |
| (char*)"this", (char*)"is", (char*)"a", (char*)"test" | |
| }; | |
| bubble_sort(s, 4); | |
| print_array(s, 4); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment