-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprog8.cpp
More file actions
75 lines (58 loc) · 1.39 KB
/
prog8.cpp
File metadata and controls
75 lines (58 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*
Write a C++ program to apply bubble sort on an array of integers and float using
the concept of function template
*/
#include <iostream>
using namespace std;
template <typename T>
void bsort(T arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
T temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
cout << "Integer array sort" << endl << "Enter the number of elements: ";
int n;
cin >> n;
int ia[n];
cout << "Enter the array elements" << endl;
for (int i = 0; i < n; i++) {
cin >> ia[i];
}
cout << "Array elements before sorting" << endl;
for (int i = 0; i < n; i++) {
cout << ia[i] << "\t";
}
cout << endl;
bsort(ia, n);
cout << "Array elements after sorting" << endl;
for (int i = 0; i < n; i++) {
cout << ia[i] << "\t";
}
cout << endl;
cout << "Float array sort" << endl << "Enter the number of elements: ";
cin >> n;
float fa[n];
cout << "Enter the array elements" << endl;
for (int i = 0; i < n; i++) {
cin >> fa[i];
}
cout << "Array elements before sorting" << endl;
for (int i = 0; i < n; i++) {
cout << fa[i] << "\t";
}
cout << endl;
bsort(fa, n);
cout << "Array elements after sorting" << endl;
for (int i = 0; i < n; i++) {
cout << fa[i] << "\t";
}
cout << endl;
return 0;
}