コード例 #1
0
        /// <summary>
        /// The btnPesquisaSequencial_Click.
        /// </summary>
        /// <param name="sender">The sender<see cref="object"/>.</param>
        /// <param name="e">The e<see cref="EventArgs"/>.</param>
        private void btnPesquisaSequencial_Click(object sender, EventArgs e)
        {
            DateTime TempoI, TempoF;
            TimeSpan Tempo;

            TempoI = DateTime.Now;

            int valor;

            bool sucesso = int.TryParse(txtValorProcurar.Text, out valor);

            if (valor <= 0)
            {
                MessageBox.Show("O número deve ser válido!", "Erro!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            if (sucesso)
            {
                int pos = ClassPesquisaSelecao.PesquisaSequencial(arrayNumeros, valor);

                if (pos == -1)
                {
                    txtResultado.Text = $"O Número {valor} não existe no array";
                }
                else
                {
                    txtResultado.Text = $"O Número {valor} está na posição {pos}";
                }
            }

            TempoF = DateTime.Now;
            Tempo  = TempoF.Subtract(TempoI);

            lblTempoPesquisas.Text = Tempo.TotalSeconds.ToString("0.0000") + " segundos";
        }
コード例 #2
0
        /// <summary>
        /// The btnPesquisaBinaria_Click.
        /// </summary>
        /// <param name="sender">The sender<see cref="object"/>.</param>
        /// <param name="e">The e<see cref="EventArgs"/>.</param>
        private void btnPesquisaBinaria_Click(object sender, EventArgs e)
        {
            DateTime TempoI, TempoF;
            TimeSpan Tempo;

            TempoI = DateTime.Now;

            int valor;

            bool sucesso = int.TryParse(txtValorProcurar.Text, out valor);

            if (valor <= 0)
            {
                MessageBox.Show("O número deve ser válido!", "Erro!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            int[] sortedArray = new int[arrayNumeros.Length];
            Array.Copy(arrayNumeros, sortedArray, arrayNumeros.Length);

            Array.Sort(sortedArray); // The binary search needs the arary to be sorted to work

            if (sucesso)
            {
                ClassPesquisaSelecao.PesquisaBinaria(sortedArray, valor);

                TempoF = DateTime.Now;
                Tempo  = TempoF.Subtract(TempoI);

                lblTempoPesquisas.Text = Tempo.TotalSeconds.ToString("0.0000") + " segundos";

                /*
                 *  First get the time for sorting and searching with binary searching to get the accurate time
                 *  Then use the built in function to get the indexOf the pos in the original array to get the
                 *  correct answer.
                 */

                int pos = Array.IndexOf(arrayNumeros, valor);

                if (pos == -1)
                {
                    txtResultado.Text = $"O Número {valor} não existe no array";
                }
                else
                {
                    txtResultado.Text = $"O Número {valor} está na posição {pos}";
                }
            }
        }