Hello,

I need help!!! I have a program that uses an array for the user to enter in 5 integers. After those integers are added I want to sort them using a bubble sort. My problem is I'm really confused on where I place my bubble sort? Any help would be appreciated.Thank you!

 static void Main(string[] args)
        {
            string myName = "";
            int[] integers = new int[5];
            int Sum = 0;
            int temp;
            Console.WriteLine("What is your name?");
            myName = Console.ReadLine();

            for (int i = 0; i < integers.Length; i++)
            {
                while (Sum < 10 || Sum > 50)
                {    
                    Console.WriteLine("Please enter {0}  integers between 10 and 50"
                        , integers.Length - i);
                    Sum = Convert.ToInt32(Console.ReadLine());
                }
                integers[i] = Sum;
                Sum = 0;
            }               
                for (int i = 0; i < integers.Length; i++)          
            {    
                for (int pass = 1; pass <= integers.Length - 2; pass++)
                {
                    for (int j = 0; j <= integers.Length - 2; j++)
                    {
                        if (integers[j] > integers[j = 1])
                        {
                            temp = integers[j + 1];
                            integers[j + 1] = integers[j];
                            integers[j] = temp;
                        }
                    }
                }
                           }
            Console.WriteLine("Your name is {0}.", myName);

            Console.ReadKey();

Dani AI

Generated

— put the sorting code after you finish collecting the five numbers and call it from Main. As suggested, the neatest approach is a separate static method (for example BubbleSort) so Main just gathers input, calls the sorter, then prints the results.

Use a clear input loop with int.TryParse and a descriptive variable name (don’t reuse Sum for a single input). This avoids exceptions and fixes the validation logic. Example input pattern:

int value;
for (int i = 0; i < integers.Length; i++)
{
    Console.Write("Enter integer between 10 and 50: ");
    while (!int.TryParse(Console.ReadLine(), out value) || value < 10 || value > 50)
    {
        Console.Write("Invalid. Enter integer between 10 and 50: ");
    }
    integers[i] = value;
}

Put the bubble sort in its own method and call it from Main. A correct, robust version uses the usual two nested loops and an early-exit optimization:

static void BubbleSort(int[] a)
{
    int n = a.Length;
    for (int i = 0; i < n - 1; i++)
    {
        bool swapped = false;
        for (int j = 0; j < n - i - 1; j++)
        {
            if (a[j] > a[j + 1])
            {
                int tmp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = tmp;
                swapped = true;
            }
        }
        if (!swapped) break;
    }
}

Call BubbleSort(integers); from Main and display with something like Console.WriteLine("Sorted: " + string.Join(", ", integers));.

Quick troubleshooting notes: watch off-by-one bounds (inner loop should go to n - i - 2 when using 0-based indices) and avoid typos that assign (=) instead of index arithmetic (j + 1). Also remember bubble sort is O(n^2) — fine for five items, but in real code prefer Array.Sort for simplicity and performance.

Recommended Answers

All 3 Replies

Where you have it now (lines 21-34) is actually workable. The implementation has several problems with it as it stands, but you could leave it right where it is. However, that's not what you would usually do.

Has your instructor (or textbook) explained about writing separate methods other than Main() yet?

The usual solution to this is to write a bubblesort() method and pass the data to that. If you have covered methods already, then the solution is to write the bubblesort() method after the end of the Main() method, and call it in Main().

Just some side notes.
change this

while (Sum < 10 || Sum > 50)

to this

while (Sum > 10 || Sum < 50)

and in line 27 to this

if (integers[j] > integers[j + 1])

This should work

static void Main(string[] args)
        {
            string myName = "";
            int[] integers = new int[5];
            int Sum = 0;
            int temp;
            Console.WriteLine("What is your name?");
            myName = Console.ReadLine();
            for (int i = 0; i < integers.Length; i++)
            {
                while (Sum>10 || Sum<50)
                {    
                    Console.WriteLine("Please enter {0}  integers between 10 and 50"
                        , integers.Length - i);
                    Sum = Convert.ToInt32(Console.ReadLine());
                }
                integers[i] = Sum;
                Sum = 0;
            }               
                for (int i = 0; i < integers.Length; i++)          
            {    
                for (int pass = 1; pass <= integers.Length - 2; pass++)
                {
                    for (int j = 0; j <= integers.Length - 2; j++)
                    {
                        if (integers[j]>integers[j+1])
                        {
                            temp = integers[j + 1];
                            integers[j + 1] = integers[j];
                            integers[j] = temp;
                        }
                    }
                }
                           }
            Console.WriteLine("Your name is {0}.", myName);
            Console.ReadKey();
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.