Question: I want to edit this c++ insertion sort algorithm i made to read in a .dat file. the .dat file is something like this in

I want to edit this c++ insertion sort algorithm i made to read in a .dat file.

the .dat file is something like this in format

15

1 3 5 7 8 9 2 8 10 32 11

Where the first line it reads is the size of the array and the second line is the array to be sorted. Currently my algorithm does this statically as shown below. Can somebody help me rewrite this where it does this same process but gets the numbers while reading in a file? the file can be called testfile.dat for this purpose.

#include

#include

#include

#include

#include

#include

using namespace std;

void insertionSort(int arr[], int n){

int temp, j;

for (int i = 1; i

temp = arr[i];

j = i-1;

while (j >= 0 && arr[j] > temp){

arr[j+1] = arr[j];

j = j-1;

}

arr[j+1] = temp;

}

}

int main(){

int nums[15] = {1,4,5,8,9,10,7,6,3,2};

int size = sizeof(nums)/sizeof(nums[0]);

cout<

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

cout<< nums[i]<< " ";

}

cout<<" ";

insertionSort(nums, size);

for (int j = 0; j < size; j++){

cout<< nums[j]<< " ";

}

cout<<" ";

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!