int ordenarValores(int *a, int *b, int *c) { if(*a >= *b) { trocarValores(a, b); } if(*b >= *c) {; trocarValores(b, c); } if(*c >= *a && *a >= *b) { trocarValores(a, b); } return *a == *b && *b == *c; }
long selectionSort(int * a, int tam) { int i, j, menor; // declaração de variáveis unsigned long trocas = 0; // percorrer todas as posições do vetor, um a um for (i = 0; i < tam; i++) { menor = i; // assumir que o primeiro no intervalo é menor // percorrer o vetor para buscar alguem menor, caso exista for (j = i+1; j < tam; j++) { // se for menor que o menor encontrado if (a[j] < a[menor]) { // anote-o menor = j; } }// se alguem menor que o menor for encontrado if (menor != i) { // trocar as posições dos dois trocarValores(a, menor, i); trocas++; } exibirPorcentagem(i, tam); } return trocas; }
long bubbleSort(int *a, int tam) { int i, j; unsigned long trocas = 0; for(i=0; i<tam; i++){ for(j=0; j<tam-1; j++){ if(a[j]>a[j+1]) { trocarValores(a, j, j+1); trocas++; } } exibirPorcentagem(i, tam); } return trocas; }