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

Create 2 programs defined in files slow.cpp and fast.cpp . In slow.cpp , generat

ID: 644287 • Letter: C

Question

Create 2 programs defined in files slow.cpp and fast.cpp.

In slow.cpp, generate a random vector of size 100,000 and pass it to your sorting algorithm. Run the program and observe that it never ends. Press CTRL C to terminate the process. In fast.cpp, generate a random vector of size 100,000 and pass it to the sort routine that is part of the C++ standard library. The following code shows how you would call the sort function on a vector v.

If the above line fails to compile, you may need to include the algorithm header as follows.

Run fast.cpp and notice how quickly it terminates. The reason for this is that the sorting function in the standard library is using is more efficient than the one you produced. How quickly algorithms terminate is within an area of computer science referred to as complexity theory.

Explanation / Answer

slow.cpp

#include
#include

class a
{
public:
   void sort(long *v, long n)
   {
       long i,j;
       int c;

       for(i=0;i        {
           for(j=0;j<=i;j++)
           {
               if(v[j]>v[i])
               {
                   c=v[j];
                   v[j]=v[i];
                   v[i]=c;
               }
           }
       }


       cout<<" Sorted Arrary is :-";

       for(i=0;i        {
           cout<<" "<        }
       getch();
   }
};
void main()
{
   long v[100000],a,i,c;

   a o;
   clrscr();

   for(i=0;i<100000;i++)
   {
       v[i]=i;
   }
   o.sort(&v[0], 100000);
}

fast.cpp

#include
#include
#include

class a
{
public:
   void sort(long *v, long n)
   {
       long i,j;
       int c;

       for(i=0;i        {
           for(j=0;j<=i;j++)
           {
               if(v[j]>v[i])
               {
                   c=v[j];
                   v[j]=v[i];
                   v[i]=c;
               }
           }
       }


       cout<<" Sorted Arrary is :-";

       for(i=0;i        {
           cout<<" "<        }
       getch();
   }
};
void main()
{
   long v[100000],a,i,c;

   a o;
   clrscr();

   for(i=0;i<100000;i++)
   {
       v[i]=i;
   }
   sort(v.begin[0], v.end[100000]);
}