use c write this program test your program with this file As
use c++ write this program
test your program with this file
Assignment 3: Linear Insertion Sort We have talked about two sorting algorithms, Bubble Sort and Selection Sort. This one is slightly different and involves several concepts we have discussed Here is the method 1. You can use a static one-dimensional array of doubles for this with length 100. You will never have more than 100 things to sort, but you may have fewer. Your program will have to keep track of how many elements you actually use. 2. Ask the user for the name of a file containing data. If it does not exist, the program should display an error, then ask for a new file name. Entering an asterisk (H) should exit the program 3. The way linear insertion works is this A. If the list is empty, the number goes in the first array element B. If the list is not empty, search to find the two elements where the top one is smaller than the one you have and the bottom one is larger (assuming an ascending-order sort.) This is your insertion point. Then move everything below and including the insertion point down in the array. Put the new element in. C. Increment the count of elements in the array 4. Once the list is sorted, print it. 5. Go back to step 2 and ask for another file name. For example, if the file contains 6, 3, 7, and 2 your array would look like this at each step Step 1: Array is empty, so insert the 6. Step 2: Move the 6 down, insert the 3 Step 3: Put the 7 at the end; nothing moves Step 4: Move the 3, 6, and 7 down, then insert the 2Solution
#include<iostream>
using namespace std;
int main()
{
int i,j,n,temp,a[30],count=0;
fstream file(\"textfile.txt\");
int linecount = 0;
int a[100];
int *finalArray;
while (!file.eof())
{
file >> tempArray[linecount];
linecount++;
}
cout << \"Number of lines: \" << linecount;
finalArray = new int[linecount];
for (int i = 0; i < linecount; i++)
{
a[i] = tempArray[i];
}
for(i=1;i<=linecount-1;i++)
{
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0))
{
a[j+1]=a[j]; //moves element forward
j=j-1;
}
a[j+1]=temp; //insert element in proper place
}
cout<<\"\ Sorted list is as follows\ \";
for(i=0;i<n;i++)
{
cout<<a[i]<<\" \";
}
return 0;
}
Thanks,

