-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.cpp
57 lines (44 loc) · 1.48 KB
/
temp.cpp
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
#include<iostream>
using namespace std;
// Template function for selection sort
template<typename T>
void selectionSort(T arr[], int size) {
for (int i = 0; i < size - 1; ++i) {
int minIndex = i;
// Find the index of the minimum element in the unsorted part of the array
for (int j = i + 1; j < size; ++j) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the first element
swap(arr[i], arr[minIndex]);
}
}
// Template function to print array elements
template<typename T>
void printArray(const T arr[], int size) {
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}
int main() {
// Example with integers
int intArray[] = {64, 25, 12, 22, 11};
int intSize = sizeof(intArray) / sizeof(int);
cout << "Original array (integers): ";
printArray(intArray, intSize);
selectionSort(intArray, intSize);
cout << "Sorted array (integers): ";
printArray(intArray, intSize);
// Example with doubles
double doubleArray[] = {3.14, 2.71, 1.618, 0.0, -1.0};
int doubleSize = sizeof(doubleArray) / sizeof(double);
cout << "\nOriginal array (doubles): ";
printArray(doubleArray, doubleSize);
selectionSort(doubleArray, doubleSize);
cout << "Sorted array (doubles): ";
printArray(doubleArray, doubleSize);
return 0;
}