Quick search / Bubble sort

Run Settings
LanguageC
Language Version
Run Command
#include <stdio.h> // For the quick search works it needs that the list is sorted // I choosed to use bubble sort, but you can use wich serves you better void bubbleSort(int arr[], int size) { int i, j, aux; for (i = 1; i < size; i++) { for (j = 0; j < size - i; j++) { if (arr[j] > arr[j + 1]) // if j+1 is lower then j goes to left { aux = arr[j]; arr[j] = arr[j+1]; arr[j+1] = aux; } } } } int finder(int posi, int size, int arr[]) { int left = -1, middle, right = size; while(left < right - 1) { middle = (left + right) / 2; if(arr[middle] < posi) left = middle; else right = middle; } return right; } int main() { int i, val, find, TAM; printf("Tamanho da lista: "); scanf("%d", &TAM); printf("%d", TAM); int vetor[TAM]; printf("\nvalores:\t\t"); for(i = 0; i < TAM; i++) { scanf("%d", &val); vetor[i] = val; if(i == TAM - 1) printf("%d", vetor[i]); else printf("%d, ", vetor[i]); } printf("\n\nLista organizada:\t"); bubbleSort(vetor, TAM); for(i = 0; i < TAM; i++) { if(i == TAM - 1) printf("%d", vetor[i]); else printf("%d, ", vetor[i]); } printf("\n\nPosição do valor: "); scanf("%d", &find); printf("%d", find); printf("\nIndice %d.", finder(find, TAM, vetor)); return 0; }
Editor Settings
Theme
Key bindings
Full width
Lines