-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection-Sort.cpp
More file actions
46 lines (32 loc) · 808 Bytes
/
Copy pathSelection-Sort.cpp
File metadata and controls
46 lines (32 loc) · 808 Bytes
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
#include <iostream>
void Selection_Sort(int arr[], int n);
void swap(int* a, int* b);
int main(){
int arr[50], n;
std::cout<< "Enter the size of your array : " ;
std::cin >> n;
std::cout<< "Enter the elements of your array : " ;
for(int i=0; i<n; ++i)
std::cin >>arr[i];
Selection_Sort(arr,n);
std::cout<< "Sorted array : " << std::endl;
for(int i=0; i<n; ++i)
std::cout << arr[i] << ",";
return 0;
}
void Selection_Sort(int arr[], int n){
for(int i=0; i<n; ++i){
int min_idx = i;
for(int j=i+1; j<n; ++j){
if (arr[min_idx] > arr[j])
min_idx = j;
}
swap(&arr[i], &arr[min_idx]);
}
}
void swap(int* a, int* b){
int temp;
temp = *a;
*a = *b;
*b = temp;
}