Bubblesort random numbers

Asked

Viewed 438 times

1

The numbers are generated randomly in the vector, but when I call the function Bublle to sort the result returns 0. Someone can help me????

using System;

namespace BubbleSort_CSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] vetor = new int[10];
            Random rnd = new Random();
            Console.WriteLine("Vetor Desordenado\n");
            for (int i = 0; i <= vetor.Length; i++)
            {
                Console.WriteLine("Elemento: {0}", rnd.Next(1, 100));     
            }

            bubbleSort(vetor, vetor.Length);            
            Console.WriteLine("\nVetor Ordenado\n");

            for (int i = 0; i <= vetor.Length; i++)
            {
                Console.WriteLine("Elemento: {0} ", vetor[i]);
            }
            Console.ReadLine();
        }

        static void bubbleSort(int[] vetor, int length)
        {
            int trocas = 0;
            for (int i = 0; i < length - 1; i++)
            {
                for (int j = 0; j < length - (i + 1); j++)
                {
                    if (vetor[j] > vetor[j + 1])
                    {
                        trocas = vetor[j];
                        vetor[j] = vetor[j + 1];
                        vetor[j + 1] = trocas;
                    }
                }
            }
        }
    }
}

2 answers

5

Your sorting algorithm is working perfectly. The problem is that there are no elements in the array vetor. In the first for the generated values are only shown in the console, they should be inserted in the array. So in the second for is only shown the value 0, the array was created with 10 positions, but none was preset with any value, that is, all values default of int, that is 0.

Change the first for for:

for (int i = 0; i < vetor.Length; i++)
{
    vetor[i] = rnd.Next(1, 100); // Aqui adiciona o número gerado à posição i de vetor
    Console.WriteLine("Elemento: {0}", vetor[i]);     
}

Heed

The condition of the two ties for is wrong, they must be i < vetor.Length and not i <= vetor.Length. In the current form, in the last loop the value of i will be 10 and the last index of the array is 9, this will cause a IndexOutOfRangeException.

See working on . NET Fiddle


Complete code

using System;   

public class Program
{
    public static void Main(string[] args)
    {
        int[] vetor = new int[10];
        Random rnd = new Random();
        Console.WriteLine("Vetor Desordenado\n");

        for (int i = 0; i < vetor.Length; i++)
        {
            vetor[i] = rnd.Next(1, 100);
            Console.WriteLine("Elemento: {0}", vetor[i]);     
        }

        bubbleSort(vetor, vetor.Length);            
        Console.WriteLine("\nVetor Ordenado-- \n");

        for (int i = 0; i < vetor.Length; i++)
        {
            Console.WriteLine("Elemento: {0} ", vetor[i]);
        }
        Console.ReadLine();
    }

    static void bubbleSort(int[] vetor, int length)
    {
        int trocas = 0;
        for (int i = 0; i < length - 1; i++)
        {           
            for (int j = 0; j < length - 1; j++)
            {
                if (vetor[j] > vetor[j + 1])
                {
                    trocas = vetor[j];
                    vetor[j] = vetor[j + 1];
                    vetor[j + 1] = trocas;
                }
            }
        }
    }
}

2

Good to fix your problem just make the following changes, in the first is already add the item in the vector, and instead of vector. Length put vector.Length -1 nos for, generally your code would look like this:

static void Main(string[] args)
    {
        int[] vetor = new int[10];
        Random rnd = new Random();
        Console.WriteLine("Vetor Desordenado\n");
        for (int i = 0; i <= vetor.Length-1; i++)
        {
            int aleatorio = rnd.Next(1, 100);
            Console.WriteLine("Elemento: {0}", aleatorio);
            vetor[i] = aleatorio;
        }

    bubbleSort(vetor, vetor.Length);

    Console.WriteLine("\nVetor Ordenado\n");
    for (int i = 0; i <= vetor.Length-1; i++)
    {
        Console.WriteLine("Elemento: {0} ", vetor[i]);
    }
         Console.ReadLine();
    }
    static void bubbleSort(int[] vetor, int length)
    {
        int trocas = 0;
        for (int i = 0; i < length - 1; i++)
        {
            for (int j = 0; j < length - (i + 1); j++)
            {
                if (vetor[j] > vetor[j + 1])
                {
                    trocas = vetor[j];
                    vetor[j] = vetor[j + 1];
                    vetor[j + 1] = trocas;
                }
            }
        }

    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.