int busca_binaria(mainTwittPeek.Tweets[] oTweets, mainTwittPeek.Tweets item, int low, int high, string sChave) { if (high <= low) { return(((int)item.getField(sChave) > (int)oTweets[low].getField(sChave)) ? (low + 1) : low); } int mid = (low + high) / 2; if (item.getField(sChave) == oTweets[mid].getField(sChave)) { return(mid + 1); } if ((int)item.getField(sChave) > (int)oTweets[mid].getField(sChave)) { return(busca_binaria(oTweets, item, mid + 1, high, sChave)); } return(busca_binaria(oTweets, item, low, mid - 1, sChave)); }
static void RadixSort(mainTwittPeek.Tweets[] oTweets, string sChave) { int i; mainTwittPeek.Tweets maior = oTweets[0]; //assume que o maior é o primeiro int exp = 1; mainTwittPeek.Tweets[] b = new mainTwittPeek.Tweets[oTweets.Length]; for (i = 0; i < oTweets.Length; i++) { if ((int)oTweets[i].getField(sChave) > (int)maior.getField(sChave)) // tenta achar um novo maior no vetor { maior = oTweets[i]; } } while ((int)maior.getField(sChave) / exp > 0) { int[] bucket = new int[oTweets.Length]; for (i = 0; i < oTweets.Length; i++) { bucket[((int)oTweets[i].getField(sChave) / exp) % 10]++; //contagem o simbolo % é tipo um limitador se a divisão for maior que 10 subtrai 10, ex 55/5 = 11, (55/5)%10 = 1 } for (i = 1; i < 10; i++) { bucket[i] += bucket[i - 1]; } for (i = oTweets.Length - 1; i >= 0; i--) { b[--bucket[((int)oTweets[i].getField(sChave) / exp) % 10]] = oTweets[i]; } for (i = 0; i < oTweets.Length; i++) { oTweets[i] = b[i]; } exp *= 10; } }
private void Swap(mainTwittPeek.Tweets[] oTweets, int x, int y)//troca dois caras de lugar no array { mainTwittPeek.Tweets temp = oTweets[x]; oTweets[x] = oTweets[y]; oTweets[y] = temp; }
void mergeSort(mainTwittPeek.Tweets[] oTweets, int posicaoInicio, int posicaoFim, string sChave) { int i, j, k, metadeTamanho; mainTwittPeek.Tweets[] oTweetsTemp; if (posicaoInicio == posicaoFim) { return; } // ordenacao recursiva das duas metades metadeTamanho = (posicaoInicio + posicaoFim) / 2; mergeSort(oTweets, posicaoInicio, metadeTamanho, sChave); mergeSort(oTweets, metadeTamanho + 1, posicaoFim, sChave); // intercalacao no vetor temporario t i = posicaoInicio; j = metadeTamanho + 1; k = 0; oTweetsTemp = new mainTwittPeek.Tweets[posicaoFim - posicaoInicio + 1]; while (i < metadeTamanho + 1 || j < posicaoFim + 1) { if (i == metadeTamanho + 1) { // i passou do final da primeira metade, pegar v[j] oTweetsTemp[k] = oTweets[j]; j++; k++; } else { if (j == posicaoFim + 1) { // j passou do final da segunda metade, pegar v[i] oTweetsTemp[k] = oTweets[i]; i++; k++; } else { if ((int)oTweets[i].getField(sChave) < (int)oTweets[j].getField(sChave)) { oTweetsTemp[k] = oTweets[i]; i++; k++; } else { oTweetsTemp[k] = oTweets[j]; j++; k++; } } } } // copia vetor intercalado para o vetor original for (i = posicaoInicio; i <= posicaoFim; i++) { oTweets[i] = oTweetsTemp[i - posicaoInicio]; } }
void BucketSort(mainTwittPeek.Tweets[] oTweets, string sChave, bool crescente) { int i; mainTwittPeek.Tweets maior = oTweets[0]; //assume que o maior é o primeiro int exp = 1; mainTwittPeek.Tweets[] b = new mainTwittPeek.Tweets[oTweets.Length]; //percorre todo arrei procurando o maior valor for (i = 0; i < oTweets.Length; i++) { if (true) { if ((int)oTweets[i].getField(sChave) > (int)maior.getField(sChave)) // tenta achar um novo maior no vetor { maior = oTweets[i]; } } else { if ((int)oTweets[i].getField(sChave) < (int)maior.getField(sChave)) // tenta achar um novo maior no vetor { maior = oTweets[i]; } } } int bucketSize = 10; while ((int)maior.getField(sChave) / exp > 0) { int[] bucket = new int[bucketSize]; if (crescente) { for (i = 0; i < oTweets.Length; i++) { bucket[((int)oTweets[i].getField(sChave) / exp) % bucketSize]++; //contagem o simbolo % é tipo um limitador se a divisão for maior que 10 subtrai 10, ex 55/5 = 11, (55/5)%10 = 1 } for (i = 1; i < bucketSize; i++) { bucket[i] += bucket[i - 1]; //somando o valor dos buckets } for (i = oTweets.Length - 1; i >= 0; i--) { b[--bucket[((int)oTweets[i].getField(sChave) / exp) % bucketSize]] = oTweets[i]; } for (i = 0; i < oTweets.Length; i++) { oTweets[i] = b[i]; } } else { for (i = 0; i < oTweets.Length; i++) { bucket[((int)oTweets[i].getField(sChave) / exp) % bucketSize]++; //contagem o simbolo % é tipo um limitador se a divisão for maior que 10 subtrai 10, ex 55/5 = 11, (55/5)%10 = 1 } for (i = 1; i < bucketSize; i++) { bucket[i] += bucket[i - 1]; } for (i = oTweets.Length - 1; i >= 0; i--) { b[--bucket[((int)oTweets[i].getField(sChave) / exp) % bucketSize]] = oTweets[i]; } for (i = 0; i < oTweets.Length; i++) { oTweets[oTweets.Length - 1 - i] = b[i]; } } exp *= bucketSize; } }
void RadixSort(mainTwittPeek.Tweets[] oTweets, string sChave, bool crescente) { int i; mainTwittPeek.Tweets maior = oTweets[0]; //assume que o maior é o primeiro int bucketSize = 10; int[] contadorEscaninhos = new int[bucketSize]; //uma lista com o numero de elementos em cada escaninho List <mainTwittPeek.Tweets[]> escaninhos = new List <mainTwittPeek.Tweets[]>(); for (i = 0; i < bucketSize; i++) { mainTwittPeek.Tweets[] escaninho = new mainTwittPeek.Tweets[oTweets.Length]; escaninhos.Add(escaninho); } //pega o maior valor dessa chave, para saber quantos caracteres vamos precisar verificar int maiorNumero = 0; for (i = 0; i < oTweets.Length; i++) { if (maiorNumero < (int)oTweets[i].getField(sChave)) { maiorNumero = (int)oTweets[i].getField(sChave); } } int tamanhoMaiorNumero = maiorNumero.ToString().Length; //verificar digito a digito até o tamanho do maior, ex roda 4x se o maior valor for 8734 for (int indexDigito = 0; indexDigito < tamanhoMaiorNumero; indexDigito++) { //para cada elemento ordenar nos buckets pelo digito de index for (i = 0; i < oTweets.Length; i++) { int chave = (int)oTweets[i].getField(sChave); int digito = (chave / (int)Math.Pow(10, indexDigito)) % 10; //coloca o tweet no escaninho do digito, na posição da pilha marcada pelo contador de digitos do escaninho escaninhos[digito][contadorEscaninhos[digito]] = oTweets[i]; contadorEscaninhos[digito]++; } int indexArrayPrincipal = crescente ? 0 : oTweets.Length - 1; int indexContadorEscaninhos = 0; //reordenar o array removendo os dados dos buckets em ordem foreach (mainTwittPeek.Tweets[] escaninho in escaninhos) { int indexEscaninho = crescente ? 0 : contadorEscaninhos[indexContadorEscaninhos] - 1; while (contadorEscaninhos[indexContadorEscaninhos] > 0) { oTweets[indexArrayPrincipal] = escaninho[indexEscaninho]; contadorEscaninhos[indexContadorEscaninhos]--; indexEscaninho = crescente ? indexEscaninho + 1 : indexEscaninho - 1; indexArrayPrincipal = crescente ? indexArrayPrincipal + 1 : indexArrayPrincipal - 1; } indexContadorEscaninhos++; indexEscaninho = 0; } } }