int main() { int item; inti; //cout << arr[i] << ";"; cin >> item; int *arr = new int[item]; // for (int x = 0; x < item; x++) // { // } for (int x = 0; x < item; x++) { cin >> arr[x]; if (x > 0) { InsertionSort(x + 1, arr); for (inti = 0; i < x + 1; i++) { cout << arr[i] << ";"; }cout << endl; } } return 0; }
can someone please explain to me what this code is doing, it was coded in c++
int main()
{
int item; //Declaring an int variable item
int i; //Declaring an int variable i
//cout << arr[i] << ";";
cin >> item; //getting number of items from user
int *arr = new int[item]; //Creating an int array of size item
// for (int x = 0; x < item; x++)
// {
// }
for (int x = 0; x < item; x++)
{
cin >> arr[x]; //Getting value from user in array arr[] at
index x
if (x > 0) //if value of x>0
{
InsertionSort(x + 1, arr); //calling insertion sort to sort array
arr[] of size x+1
for (int i = 0; i < x + 1; i++) //printing sorted array arr[] of
size x+1
{
cout << arr[i] << ";";
}cout << endl;
}
}
return 0;
}
Basically, Above function showing insertion sort after insertion of every new element in arr[].
Where new inserted element gets its position in previously sorted array arr[].
Get Answers For Free
Most questions answered within 1 hours.