-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselection.cpp
More file actions
50 lines (37 loc) · 969 Bytes
/
selection.cpp
File metadata and controls
50 lines (37 loc) · 969 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
47
48
49
// selectionSort() implementa o algoritmo de ordenação por seleção.
// arataca89@gmail.com
// Aulas de programação C++
#include <iostream>
using std::cout;
using std::endl;
void swap(int *const a, int *const b){
int box = *a;
*a = *b;
*b = box;
}
void selectionSort(int *const array, int size){
int indiceMenor;
for(int i = 0; i < size-1;i++){
indiceMenor = i;
for(int j = i+1; j < size; j++){
if(array[j] < array[indiceMenor]){
indiceMenor = j;
}
}
swap(&array[i], &array[indiceMenor]);
}
}
void printArray(int *const array, int size){
for(int i = 0; i < size; i++){
cout << array[i] << " ";
}
cout << endl;
}
int main(void){
int vetor[] = {14,2,10,33,-1,57,89,-13,0,72};
int size = sizeof(vetor) / sizeof(vetor[0]);
printArray(vetor,size);
selectionSort(vetor, size);
printArray(vetor,size);
return 0;
}