Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template <typename T>
- void sort(T array[], size_t size) {
- for (int k = 0; k < size; k++) {
- for (int j = 0; j < size - 1; j++) {
- if (array[j] > array[j + 1]) {
- T temp = array[j];
- array[j] = array[j + 1];
- array[j + 1] = temp;
- }
- }
- }
- }
- template<typename T>
- void display(T array[], size_t size) {
- for (int i = 0; i < size; i++) {
- cout << array[i] << " ";
- }
- cout << endl;
- }
- int main()
- {
- cout << "Sort Template" << endl << endl;
- int intArray[]{ 1, 3, 7, -4, -2, 4 };
- int size = sizeof(intArray) / sizeof(int);
- cout << "Original int Array : ";
- display(intArray, size);
- sort(intArray, size);
- cout << "Sorted int Array : ";
- display(intArray, size);
- char chrArray[]{ 'o', 't', 't', 'f', 'f', 's', 's', 'e', 'n' };
- size = sizeof(chrArray) / sizeof(char);
- cout << "Original chr Array : ";
- display(chrArray, size);
- sort(chrArray, size);
- cout << "Sorted chr Array : ";
- display(chrArray, size);
- string strArray[]{ "one", "two", "three", "four", "five" };
- size = sizeof(strArray) / sizeof(string);
- cout << "Original str Array : ";
- display(strArray, size);
- sort(strArray, size);
- cout << "Sorted str Array : ";
- display(strArray, size);
- }
Add Comment
Please, Sign In to add comment