Skip to content

Instantly share code, notes, and snippets.

@trolley813
Created October 29, 2020 06:29
Show Gist options
  • Select an option

  • Save trolley813/c983de217e69f7a3387cf7c7a77ceb7e to your computer and use it in GitHub Desktop.

Select an option

Save trolley813/c983de217e69f7a3387cf7c7a77ceb7e to your computer and use it in GitHub Desktop.
#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