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

Below is an implementation of the selection sort, I just need the code to be abl

ID: 3582961 • Letter: B

Question

Below is an implementation of the selection sort, I just need the code to be able to test this C++ function

------------------------------------------------------------------------------------------------------------------------

/** Finds the largest item in an array.
@pre The size of the array is >= 1.
@post The arguments are unchanged.
@param theArray The given array.
@param size The number of elements in theArray.
@return The index of the largest entry in the array. */
int findIndexofLargest(const ItemType theArray[], int size);
/** Sorts the items in an array into ascending order.
@pre None.
@post The array is sorted into ascending order; the size of the array
is unchanged.
@param theArray The array to sort.
@param n The size of theArray. */
void selectionSort(ItemType theArray[], int n)
{
   // last = index of the last item in the subarray of items yet
   // to be sorted;
   // largest = index of the largest item found
   for (int last = n - 1; last >= 1; last––)
   {
       // At this point, theArray[last+1..n-1] is sorted, and its
       // entries are greater than those in theArray[0..last].
       // Select the largest entry in theArray[0..last]
       int largest = findIndexofLargest(theArray, last + 1);
       // Swap the largest entry, theArray[largest], with
       // theArray[last]
       std::swap(theArray[largest], theArray[last]);
   } // end for
} // end selectionSort
int findIndexofLargest(const ItemType theArray[], int size)
{
   int indexSoFar = 0; // Index of largest entry found so far
   for (int currentIndex = 1; currentIndex < size; currentIndex++)
   {
       // At this point, theArray[indexSoFar] >= all entries in
       // theArray[0..currentIndex - 1]
       if (theArray[currentIndex] > theArray[indexSoFar])
           indexSoFar = currentIndex;
   } // end for
   return indexSoFar; // Index of largest entry
} // end findIndexofLargest

Explanation / Answer

CODE:

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int size, arr[50], i ,j,temp;

cout<<"Enter Array Size : ";

cin>>size;

cout<<"Enter Array Elements : ";

for(i=0; i<size; i++)

{

cin>>arr[i];

}

cout<<"Sorting array usinfg selection sort... ";

for(i=0; i<size; i++)

{

for(j=i+1; j<size; j++)

{

if(arr[i]>arr[j]

{

temp=arr[i];

arr[i]=arr[j];

arr[j]=temp;

}

}

}

cout<<"Now the array after sorting is : ";

for(i=0; i<size; i++)

{   

cout<<arr[i]<<" ";

}

getch();

}

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