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));
        }
Esempio n. 2
0
        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;
            }
        }
        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;
            }
        }