Answered step by step
Verified Expert Solution
Question
1 Approved Answer
C++ only InsertSort /** CIS 22B: Homework 3A This program uses the INSERTION sort algorithm to sort an array of integers in ascending order. Modify
C++ only InsertSort /**
CIS 22B: Homework 3A
This program uses the INSERTION sort algorithm to sort an
array of integers in ascending order.
Modify the insertion sort function to sort an array of doubles in descending order:
1. Sort an array of doubles (instead of integers)
2. Sort it in descending order
NAME:
*/
#include
using namespace std;
// Function prototypes
void insertionSort(int *, int *);
void showArray(int *, int *);
int main()
{
// Define and initialize an array with unsorted values
const int SIZE = 100;
int list[SIZE] = {50, 70, 20, 80, 90, 10};
int length = 6;
// Display the array.
cout << "The unsorted values are ";
showArray(list, list + length - 1);
// Sort the array.
insertionSort(list, list + length - 1);
// Display the array again.
cout << "The sorted values are ";
showArray(list, list + length - 1);
return 0;
}
/***************************************************************
Definition of function insertionSort.
This function performs an ascending order insertion sort on
array.
pList - points to the first element in the list
pLast - points to the last element in the list
****************************************************************/
void insertionSort(int *pList, int *pLast)
{
for (int *curr = pList + 1; curr <= pLast; curr++)
{
int hold = *curr; // copy current element to hold
int *back = curr - 1;
while (back >= pList && hold < *back) // search where to insert the current element
{
*(back + 1) = *back; // shift to the right
back--;
}
*(back + 1) = hold; // put hold back to the array
}
}
/***************************************************************
Definition of function showArray.
This function displays the contents of array.
pList - points to the first element in the list
pLast - points to the last element in the list
****************************************************************/
void showArray(int *pList, int *pLast)
{
for (int *ptr = pList; ptr <= pLast; ptr++)
cout << *ptr << " ";
cout << endl;
}
/***************************************************************
Save the new OUTPUT below
The unsorted values are
50 70 20 80 90 10
The sorted values are
10 20 50 70 80 90
*/
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started