locked
bubble sort

    Question

  • I am kinda new to programming and need some help in completing an excercise I'm on.
    I have two bubble sorts to sort an array of structs into ascending or descending order on the users prompt. ie. the users types "A" for ascendng and "D" for descending then the data is displayed. Once this data is on the screen is there a way I could then toggle between the two (A & D) This is all being done in a Console App by the way.

    Thanks
    Wednesday, April 27, 2005 6:13 PM

Answers

  • Hi cromwell,


    Bummer... I still haven't downloaded C# Express Beta 2... But I have a code here in C++/CLI... Its quite very similar to C#...


    using namespace System;

    value struct myStruct {
       int key;
    };

    void BubbleSort(array<myStruct>^ x, bool asc) { 
       int n = x->Length - 1;
       bool intchnge = true;
       int pass = 1;
     
       while(pass <= n - 1 && intchnge) {
          intchnge = false;
          for (int j = 0; j <= n - pass; j++) {
             if (asc) {
                if (x[j].key > x[j + 1].key) {
                   intchnge = true;
                   myStruct hold = x[j];
                   x[j] = x[j + 1];
                   x[j + 1] = hold;
                }
            } else {
               if (x[j].key < x[j + 1].key) {
                  intchnge = true;
                  myStruct hold = x[j];
                  x[j] = x[j + 1];
                  x[j + 1] = hold;
               }
           }
        }
      pass++;
     }
    };

    void DisplayValues(array<myStruct>^ x) {
        for (int i = 0; i < 5; i++) {
          Console::WriteLine("arr[{0}].key = {1}", i.ToString(), xIdea.key.ToString());
        }
    };

    int main(array<System:Tongue Tiedtring ^> ^args)
    {
        array<myStruct>^ arr = gcnew array<myStruct>(5);
        arr[0].key = 20;
        arr[1].key = 22;
        arr[2].key = 15;
        arr[3].key = 10;
        arr[4].key = 12;
        Console::WriteLine("Values Before Bubble Sort");
        DisplayValues(arr);
        String^ in = "";
        Console::Write("Press A or D to Sort (X to exit):");
        while (in->ToUpper() != "X") {
            Console::CursorTop = 6;
            Console::CursorLeft = 33;
            in = Console::ReadLine();
            if (in->ToUpper() == "A")
                BubbleSort(arr, true);
            else
                BubbleSort(arr, false);
            DisplayValues(arr);
        }
        return 0;
    }

    Note: Tips on converting... Replace '->', '::' with '.'
          Also, place then codes on the main proc in a static main proc in a class...




    cheers,


    Paul June A. Domag

    Thursday, April 28, 2005 9:50 AM

All replies

  • Hi,


    I guess this is cheating... Big Smile
    But could you give me some more details?
    What language are you using? Or intend on using?

    Or better, you could just google on bubble sort and figure out the logic, coz you know... learning is very fun... Big Smile



    cheers,


    Paul June A. Domag
    Wednesday, April 27, 2005 7:12 PM
  • Hi,

    I think you can use Diagnostic class. Please refer the following thread.

    http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=1266
    Wednesday, April 27, 2005 7:16 PM
  • Hi Paul, I'm sure there is a very logical answer, but like I said, I am new to all of this, this seems to happen all too often where I find out the answer to something and kick myself for it being so obvious.

    I am doing C# by the way.

    I appreciate any help you may be able to give me.
    Thanks
    Wednesday, April 27, 2005 9:06 PM
  • Hi cromwell,


    Bummer... I still haven't downloaded C# Express Beta 2... But I have a code here in C++/CLI... Its quite very similar to C#...


    using namespace System;

    value struct myStruct {
       int key;
    };

    void BubbleSort(array<myStruct>^ x, bool asc) { 
       int n = x->Length - 1;
       bool intchnge = true;
       int pass = 1;
     
       while(pass <= n - 1 && intchnge) {
          intchnge = false;
          for (int j = 0; j <= n - pass; j++) {
             if (asc) {
                if (x[j].key > x[j + 1].key) {
                   intchnge = true;
                   myStruct hold = x[j];
                   x[j] = x[j + 1];
                   x[j + 1] = hold;
                }
            } else {
               if (x[j].key < x[j + 1].key) {
                  intchnge = true;
                  myStruct hold = x[j];
                  x[j] = x[j + 1];
                  x[j + 1] = hold;
               }
           }
        }
      pass++;
     }
    };

    void DisplayValues(array<myStruct>^ x) {
        for (int i = 0; i < 5; i++) {
          Console::WriteLine("arr[{0}].key = {1}", i.ToString(), xIdea.key.ToString());
        }
    };

    int main(array<System:Tongue Tiedtring ^> ^args)
    {
        array<myStruct>^ arr = gcnew array<myStruct>(5);
        arr[0].key = 20;
        arr[1].key = 22;
        arr[2].key = 15;
        arr[3].key = 10;
        arr[4].key = 12;
        Console::WriteLine("Values Before Bubble Sort");
        DisplayValues(arr);
        String^ in = "";
        Console::Write("Press A or D to Sort (X to exit):");
        while (in->ToUpper() != "X") {
            Console::CursorTop = 6;
            Console::CursorLeft = 33;
            in = Console::ReadLine();
            if (in->ToUpper() == "A")
                BubbleSort(arr, true);
            else
                BubbleSort(arr, false);
            DisplayValues(arr);
        }
        return 0;
    }

    Note: Tips on converting... Replace '->', '::' with '.'
          Also, place then codes on the main proc in a static main proc in a class...




    cheers,


    Paul June A. Domag

    Thursday, April 28, 2005 9:50 AM
  • grrr...
    can't edit my own post! Big Smile

    BTW, array<myStruct>^ is equivalent to myStruct[] in C#
    so, 
       array<myStruct>^ arr = gcnew array<myStruct>(5);
    is
       myStruct[] arr = new myStruct[5];
    in C# (Or something like that)...

    Also, just ommit the ^ operand...




    cheers,



    Paul June A. Domag
    Thursday, April 28, 2005 9:55 AM
  • Thanks Paul, I think this will help alot.

    Cheers for the adviceBig Smile
    Saturday, April 30, 2005 11:04 PM