-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectsort1.cpp
80 lines (72 loc) · 2.01 KB
/
Selectsort1.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/////////////////////////////////////////////////////////////////
// name : jppark
/////////////////////////////////////////////////////////////////
#include <iostream>
using namespace std;
template<class T>
void swap_values( T& variable1, T& variable2 )
{
T temp;
temp = variable1;
variable1 = variable2;
variable2 = temp;
}
// 가장작은 숫자를 검색하여 반환
template<class BaseType>
int index_of_smailest( const BaseType a[], int start_index, int number_used )
{
BaseType min = a[start_index];
int index_of_min = start_index;
for( int index = start_index+1; index < number_used; index++ )
{
if( a[index] < min )
{
min = a[index];
index_of_min = index;
}
}
return index_of_min;
}
template<class BaseType>
void selection_sort( BaseType a[], int number_used )
{
int index_of_next_smallest;
for( int index=0; index<number_used-1; index++ )
{
index_of_next_smallest = index_of_smailest( a, index, number_used );
swap_values( a[index], a[index_of_next_smallest]);
}
}
int main()
{
//////////////////////////////////////////////////////////////////////////
int arr1[10] = { 10, 4, 11, 6, 7, 9, 1, 32, 9, 64 };
for (int i=0;i<10;i++)
cout << arr1[i] << " ";
cout << endl;
selection_sort(arr1, 10);
for (i=0;i<10;i++)
cout << arr1[i] << " ";
cout << endl;
//////////////////////////////////////////////////////////////////////////
double arr2[20] =
{ 10., 14.3, 11.23, 6.65, 777., 90.1, 1.111, 32.5, 9.12, 64.,
1.11, 2.33, 50., 34.11, 15.22, 3.4, 11.1, 218., 999., 21. };
for ( i=0;i<20;i++)
cout << arr2[i] << " ";
cout << endl;
selection_sort(arr2, 20);
for ( i=0;i<20;i++)
cout << arr2[i] << " ";
cout << endl;
//////////////////////////////////////////////////////////////////////////
char arr3[7] = { 'D', 'o', 'N', 'g', 'S', 'e', 'O' };
for ( i=0;i<7;i++)
cout << arr3[i] << " ";
cout << endl;
selection_sort(arr3, 7);
for ( i=0;i<7;i++)
cout << arr3[i] << " ";
cout << endl;
return 0;
}