BubbleSort random numbers

1

The numbers are randomly generated in the vector, however when I call the bublle function to sort the result it returns 0. Can someone 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;
                    }
                }
            }
        }
    }
}
    
asked by anonymous 26.11.2016 / 18:24

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% w / o of% only the value 0 is shown, the array was created with 10 positions, but none was prepended with some value, that is, all the default values of for , which is 0.

Change the first int to:

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]);     
}

Attention

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

See working in .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;
                }
            }
        }
    }
}
    
26.11.2016 / 19:13
2

Good to fix your problem just make the following changes, in the first one is to already add the item in the vector, and instead of vector.Length put vector.Length -1 in it, in general its 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;
                }
            }
        }

    }
    
26.11.2016 / 19:07