CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Threaded View

  1. #1
    Join Date
    Oct 2016
    Posts
    5

    Bubble sort, Selection Sort, Insertion Sort

    mkir100 (1)
    So I'm new to C++ and am needing a little help. All feedback is welcome.
    I am suppose to create an AList object and then use that AList object to make the following method calls in order:

    Read
    Print
    BubbleSort
    Print
    Read
    Print
    InsertionSort
    Print
    Read
    Print
    SelectionSort
    Print


    This is what I have so far:

    Code:
    #include <iostream>
    
    using namespace std;
    
    const int MLS = 50;
    typedef int element;
    const element SENTINEL = -1;
    
    class AList {
    private:
    
        element items [MLS];
        int size;
        void Swap(int pos1, int pos2); 
        int Read_int();
        element Read_element();
    
    public:
        void Read();
        void Print();
        void BubbleSort();
        void InsertionSort();
        void SelectionSort();
    };
    
    int main() {
        AList B;
        B.Read();
        B.Print();
        B.BubbleSort();
        B.InsertionSort();
        B.SelectionSort();
    }
    
    void AList::Read() {
        element userval;
        cout<< "Enter elements : ";
        while ((userval!=SENTINEL) && (size<MLS)) {
            items [size] = userval;
            size ++;
        }
    }
    
    void AList::Print() {
        for (int i=0;i<size;i++)
            cout<< items[i]<<endl;
    }
    
    void AList::BubbleSort() {
        for (int i=0; size-1; i++){
            for (int j=0; j<size-1-i; j++) 
                if (items[j]>items[j+1])
                    Swap (j, j+1);
                else 
                    ;
        }
    }
    
    void AList::Swap(int pos1, int pos2) {
        element temp;
        temp = items[pos2];
        items[pos2]=items[pos1];
        items[pos1]=temp;
    }
    
    void AList::InsertionSort() {
        int j;
        bool done;
        for (int i=1; i<size; size++)
           j=i;
    
        done = false;
        while ((j>=1) && (!done))
            if (items[j] < items[j-1]) {
                Swap (j,j-1);
                j--;
           }
           else 
                done=true;
    }
    
    void AList::SelectionSort() {
        int maxpos;
        for (int i = size-1; i<0; i--) {
            maxpos = 0;
            for (int j=1; j<=i; j++)
                if (items[j] < items[maxpos])
                    maxpos = j;
                else;
    
            Swap(maxpos, i);
        }
    }

    Edit & Run



    I don't think that I'm doing it in the order that needs to be done and I'm also having a problem printing everything. Please help!
    Last edited by 2kaud; October 18th, 2016 at 01:29 AM. Reason: Added code tags

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured