3 Replies - 3163 Views - Last Post: 04 February 2011 - 03:01 PM Rate Topic: -----

#1 cats_rule  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 44
  • Joined: 21-November 10

Bubble Sort Help

Posted 04 February 2011 - 11:29 AM

I have an assignment question that is as follows.

Write a program that allows a user to enter 10 integer numbers, sorts them from highest to lowest value and then displays the sorted numbers. You will need to use an array and create a function that can sort the array values using a Bubble Sort. A flow chart has been provided for the sort function required.(ive attached the flowchart)

You will need a while loop containing a for loop.
See the source code below on the next page as your starting point. You may ignore or alter this code as you see fit.

Below is what ive managed to come up with based on the flowchart , but i notice there are some horrible errors.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Worksheet_4_Task_2
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] numbers = new int[10];
            int index = 0;
            string enteredNumber = "";
         

            // Read the numbers entered by the user
            for (index = 0; index < 10; index++)
            {
                Console.Write("Enter Number:");
                enteredNumber = Console.ReadLine();
                numbers[index] = int.Parse(enteredNumber);
            }

            // Sort the numbers
            BubbleSort(numbers);

            // Display the sorted numbers
            Console.WriteLine("The sorted numbers are:");
            foreach (int num in numbers)
            {
                Console.Write(num + " ");
            }

            Console.ReadLine(); // Pause
        }

        // BubbleSort function
        static void BubbleSort(int[] numbers)
        {
            bool swapped = true;
            int count = 1;
            int numPairs = 0;
            int index;
            int temp;

            // Write the bubble sort code from the flowchart here!!
            while ( swapped == true)
            {
                swapped = false;
                numPairs = numbers.Length - count;

                for (index = 0; index < numPairs; index++)
                {
                    if (numbers[index] < numbers[index + 1])
                    {
                        temp = numbers[index];
                        numbers[index] = numbers[index + 1];
                        numbers[index + 1] = temp;

                    }
                    else
                    {
                        swapped = true;
                    }

                    index = index + 1;
                    count = count + 1;
                }

            }
            

        }
    }
}




Im stuck now & have managed to confuse myself !!!
Basically all im after is pointers , hints or tips. No direct answers as im not looking to cheat but learn why/where im going wrong.
many thanks

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: Bubble Sort Help

#2 baavgai  Icon User is online

  • Dreaming Coder
  • member icon

Reputation: 5823
  • View blog
  • Posts: 12,675
  • Joined: 16-October 07

Re: Bubble Sort Help

Posted 04 February 2011 - 11:39 AM

Two biggies: swapped = true; when you swap, not when you don't. One if, but no else inside that loop.

The other is that you increment index both in the for loop and at the bottom of the loop block. Choose one.

This post has been edited by baavgai: 04 February 2011 - 11:39 AM

Was This Post Helpful? 0
  • +
  • -

#3 cats_rule  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 44
  • Joined: 21-November 10

Re: Bubble Sort Help

Posted 04 February 2011 - 11:47 AM

so would i be correct to assume the swapped=true would sit inside the if statement???

View Postbaavgai, on 04 February 2011 - 06:39 PM, said:

Two biggies: swapped = true; when you swap, not when you don't. One if, but no else inside that loop.

The other is that you increment index both in the for loop and at the bottom of the loop block. Choose one.

Was This Post Helpful? 0
  • +
  • -

#4 Ionut  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 385
  • View blog
  • Posts: 1,057
  • Joined: 17-July 10

Re: Bubble Sort Help

Posted 04 February 2011 - 03:01 PM

If you do a swap, it means your array is not order, so you need to continue with another loop.
if (numbers[index] < numbers[index + 1])
{
temp = numbers[index];
numbers[index] = numbers[index + 1];
numbers[index + 1] = temp;
swapped = true;
}



The way you did it, the sorting function stops after the first swap.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1