Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to sort 10 numbers, which need to be taken from the user. Once y

ID: 3564848 • Letter: W

Question

Write a program to sort 10 numbers, which need to be taken from the user. Once you take user input, ask user to select a particular sorting technique to sort the given numbers. You need to implement 3 sorting techniques: BubbleSort, MergeSort, and QuickSort. Implementation instruction: (1) In your main() function, define an array to store the input integers and allow user input. (2) Based on the users selection call particular Sorting function - BubbleSort, MergeSort, or QuickSort. (3) Implement the BubbleSort algorithm using recursive function of your own to sort 10 integers from user input. (4) Implement the MergeSort algorithm using recursive function of your own to sort 10 integers from user input. (5) Implement the QuickSort algorithm using recursive function of your own to sort 10 integers from user input. (6) Print out the sorted array of integers in your main() function.

Explanation / Answer

#include<iostream>

using namespace std;

void bubble(int a[], int n)         //Bubble sort

{

    if(n == 0)

        return;

    int i, j;

    for (i = 0; i < n - 1; i++) {

        if (a[i + 1] < a[i]){

                j = a[i + 1];

                a[i + 1] = a[i];

                a[i] = j;

        }

    }

    bubble(a, n - 1);

}

void merge(int a[],int low,int mid ,int high)    //Merge subroutine- for Merge Sort

{

     int b[10];

   int h,i,j,k;

   h=low;

   i=low;

   j=mid+1;

   while(h<=mid && j<=high){

      if(a[h]<=a[j])

                    b[i]=a[h++];

      else

                    b[i]=a[j++];

      i++;

   }

   if( h > mid)

      for(k=j;k<=high;k++)

                                      b[i++]=a[k];

   else

      for(k=h;k<=mid;k++)

                                     b[i++]=a[k];

   for(k=low;k<=high;k++)

   { a[k]=b[k];

   }

}

void mergesort(int a[],int i,int j)     //Merge Sort

{

   int mid;

   if(i<j)

   {

      mid=(i+j)/2;

      mergesort(a,i,mid);

      mergesort(a,mid+1,j);

      merge(a,i,mid,j);

   }

}

int Partition(int a[], int beg, int end){          //Function to Find Pivot Point-for Quick Sort

    int p=beg, pivot=a[beg], loc;

    for(loc=beg+1;loc<=end;loc++){

                                  if(pivot>a[loc]){

                                                   a[p]=a[loc];

                                                   a[loc]=a[p+1];

                                                   a[p+1]=pivot;

                                                   p=p+1;

                                  }

    }

    return p;

}

void QuickSort(int a[], int beg, int end){               //Quick Sort

     if(beg<end){

                 int p=Partition(a,beg,end);                       //Calling Procedure to Find Pivot

                 QuickSort(a,beg,p-1);                             //Calls Itself (Recursion)

                 QuickSort(a,p+1,end);                                                //Calls Itself (Recursion)

     }

}

int main(){                                            //main function

    int a[10];

    int choice;

    for(int i=0;i<10;i++){                                    //Input 10 numbers

            cout<<"Enter number "<<(i+1)<<" : ";

            cin>>a[i];

    }

    cout<<"Enter choice for sorting algorithm:"<<endl;        //Option presented to user

    cout<<"1.Bubble Sort";

    cout<<"2.Merge Sort";

    cout<<"3.Quick Sort";

    cin>>choice;

    switch(choice){         //Chose sorting algorith based on choice entered

                   case 1:

                        bubble(a, 10);

                        break;

                   case 2:

                        mergesort(a,0,9);

                        break;

                   case 3:

                        QuickSort(a,0,9);

                        break;

                   default:

                           cout<<"Invalid Choice!"<<endl;

                           break;

    }

    cout<<"Sorted Array :"<<endl;

    for(int i=0;i<10;i++){                //Print Sorted Array

            cout<<a[i]<<endl;

    }

    system("pause");

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote