fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. template <typename T>
  5. void bubbleSort(T* arr, int n) {
  6. for (int i = 0; i < n - 1; i++) {
  7. for (int j = 0; j < n - i - 1; j++) {
  8. if (arr[j] > arr[j + 1]) {
  9. T temp = arr[j];
  10. arr[j] = arr[j + 1];
  11. arr[j + 1] = temp;
  12. }
  13. }
  14. }
  15. }
  16. template <typename T>
  17. void bubbleSort(vector<T>& v) {
  18. for (int i = 0; i < v.size() - 1; i++) {
  19. for (int j = 0; j < v.size() - i - 1; j++) {
  20. if (v[j] > v[j + 1]) {
  21. T temp = v[j];
  22. v[j] = v[j + 1];
  23. v[j + 1] = temp;
  24. }
  25. }
  26. }
  27. }
  28. template <typename T>
  29. void printArray(T* arr, int n) {
  30. for (int i = 0; i < n; i++) {
  31. cout << arr[i] << " ";
  32. }
  33. cout << endl;
  34. }
  35. template <typename T>
  36. void printVector(vector<T>& v) {
  37. for (int i = 0; i < v.size(); i++) {
  38. cout << v[i] << " ";
  39. }
  40. cout << endl;
  41. }
  42.  
  43. int main() {
  44. int n;
  45. cin >> n;
  46. int* arr = new int[n];
  47. for (int i = 0; i < n; i++) {
  48. cin >> arr[i];
  49. }
  50. bubbleSort(arr, n);
  51. printArray(arr, n);
  52. delete[] arr;
  53. vector<int> v = {5, 2, 8, 1, 3};
  54. bubbleSort(v);
  55. printVector(v);
  56. return 0;
  57. }
Success #stdin #stdout 0s 5320KB
stdin
5
1 9 7 4 2
stdout
1 2 4 7 9 
1 2 3 5 8