bubble sort + binary search

Run Settings
LanguageC
Language Version
Run Command
#include <stdio.h> #define TAM 10 void showList(int *l){ for(int i = 0; i < TAM; i++) { if(i != TAM-1) printf("%d - ", l[i]); else printf("%d\n", l[i]); } } void bubbleSort(int *vet, int tam) { int i, j, aux; for (i = 0 ; i < ( tam - 1 ); i++) { for (j = 0 ; j < tam - i - 1; j++) { if (vet[j] > vet[j+1]) { aux = vet[j]; vet[j] = vet[j+1]; vet[j+1] = aux; } } } } int binarySearch(int x, int n, int *v) { int e, m, d; e = -1; d = n; while (e < d - 1) { m = (e + d)/2; if (v[m] < x) e = m; else d = m; } return d; } int main(void) { int lista[TAM] = {9, 34, 46, 16, 48, 36, 37, 7, 4, 17}; /* lista test */ int localiza = 37, posi; printf("Lista utilizada:\n"); showList(lista); printf("\nLista ordenada:\n"); bubbleSort(lista, TAM); showList(lista); posi = binarySearch(localiza, TAM, lista); printf("\nvalor %d está na posição %d", localiza, posi); return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines