Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Bubblesort Algorithm

Bubblesort Algorithm

Scheduled Pinned Locked Moved C#
algorithmsdata-structureshelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    Vandretta
    wrote on last edited by
    #1

    Hey all, I'm just trying to figure out why this bubble sort algorithm is ignoring the last element of the array.

    namespace Bubble_sort
    {
    class SortArray
    {
    void BubblesortArray(int[] array)
    {
    int Position = 0;
    int endpos = 0;
    bool swapped = true;
    while (swapped)
    {
    swapped = false;
    Position = 0;
    endpos = array.Length;
    while (Position < endpos)
    {
    if (array[Position] > array[Position + 1])
    {
    int Temp = array[Position];
    array[Position] = array[Position + 1];
    array[Position + 1] = Temp;
    swapped = true;
    }
    Position++;
    endpos--;
    }
    for (int i = 0; i < array.Length; i++)
    {
    Console.Write(array[i].ToString() + ", ");
    }
    Console.WriteLine();
    }
    }

        public static void Main(string\[\] args)
        {
            SortArray Sort = new SortArray();
            int\[\] a = {1, 2, 3, 4, 1, 45, 134, 762, 2};
            Sort.BubblesortArray(a);
            for (int i = 0; i < a.Length; i++)
            {
                Console.Write(a\[i\].ToString() + ", ");
            }
            Console.Read();
        }
    }
    

    }

    thats the code (its a console app). I do this out of curiosity, and annoyance( :-D ) but i would like any help you could offer me. Thankyou.

    S 1 Reply Last reply
    0
    • V Vandretta

      Hey all, I'm just trying to figure out why this bubble sort algorithm is ignoring the last element of the array.

      namespace Bubble_sort
      {
      class SortArray
      {
      void BubblesortArray(int[] array)
      {
      int Position = 0;
      int endpos = 0;
      bool swapped = true;
      while (swapped)
      {
      swapped = false;
      Position = 0;
      endpos = array.Length;
      while (Position < endpos)
      {
      if (array[Position] > array[Position + 1])
      {
      int Temp = array[Position];
      array[Position] = array[Position + 1];
      array[Position + 1] = Temp;
      swapped = true;
      }
      Position++;
      endpos--;
      }
      for (int i = 0; i < array.Length; i++)
      {
      Console.Write(array[i].ToString() + ", ");
      }
      Console.WriteLine();
      }
      }

          public static void Main(string\[\] args)
          {
              SortArray Sort = new SortArray();
              int\[\] a = {1, 2, 3, 4, 1, 45, 134, 762, 2};
              Sort.BubblesortArray(a);
              for (int i = 0; i < a.Length; i++)
              {
                  Console.Write(a\[i\].ToString() + ", ");
              }
              Console.Read();
          }
      }
      

      }

      thats the code (its a console app). I do this out of curiosity, and annoyance( :-D ) but i would like any help you could offer me. Thankyou.

      S Offline
      S Offline
      Simon P Stevens
      wrote on last edited by
      #2

      I'm hesitant to just give you the answer because this is a classic homework problem. Instead I'll give you some hints. (You'll feel better if you solve it yourself anyway) Start by attaching a debugger and stepping through the code in the bubble sort routine line by line. Each pass of a bubble sort should compare each adjacent pair of values. This routine doesn't. Look closely at the indexes you are using to the array being sorted. (Put watches on the values)

      Simon

      V 1 Reply Last reply
      0
      • S Simon P Stevens

        I'm hesitant to just give you the answer because this is a classic homework problem. Instead I'll give you some hints. (You'll feel better if you solve it yourself anyway) Start by attaching a debugger and stepping through the code in the bubble sort routine line by line. Each pass of a bubble sort should compare each adjacent pair of values. This routine doesn't. Look closely at the indexes you are using to the array being sorted. (Put watches on the values)

        Simon

        V Offline
        V Offline
        Vandretta
        wrote on last edited by
        #3

        Thankyou for your help, simon. I suppose it does look like a homework problem ;P my apologies for making it so. But the only reason I asked this is the site where I got it from has it written in a similar way i was just trying to figure out why the program didnt work. Again my apologies.

        S 1 Reply Last reply
        0
        • V Vandretta

          Thankyou for your help, simon. I suppose it does look like a homework problem ;P my apologies for making it so. But the only reason I asked this is the site where I got it from has it written in a similar way i was just trying to figure out why the program didnt work. Again my apologies.

          S Offline
          S Offline
          Simon P Stevens
          wrote on last edited by
          #4

          No need to apologise. It's not your fault, it's a common question. Did you manage to figure it out?

          Simon

          V 1 Reply Last reply
          0
          • S Simon P Stevens

            No need to apologise. It's not your fault, it's a common question. Did you manage to figure it out?

            Simon

            V Offline
            V Offline
            Vandretta
            wrote on last edited by
            #5

            Yes I did thank you Simon. The step through debugger in VS took a little while to get used to but eventually it worked a treat. And i figured it out. So, as always, logic prevailed over the human condition.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups