I took this code somewhere off internet, i want the output to display the number of swaps and sorted data, can someone help me with modifying this code.
//Variables:
//double arr[]: An array to store and sort elements.
//int swaps: Integer variable to count the number of swaps performed.
#include<iostream>
int main()
{
usingnamespace std;
int arr[100]; //Declare our array and initialize to 0
int n,swaps=0; //Variable to count the number of swaps performed
cout<<"Insertion sort Demonstration."<<endl<<endl;
cout<<"enter value of n: \n";
cin>>n;
for (int i=0;i<n;i++) //To traverse through the array 8 times and accept 8 numbers
{
cout<<"Enter element number "<<i+1<<": "; //We use i+1 just to start the display of numbers from 1 rather than 0
cin>>arr[i];
int j = i;
while(j>0 && arr[j]<arr[j-1]) //Runs until the new number has been placed in its correct place
{
swap(arr[j],arr[j-1]); //Swap if the elements are out of order.
swaps++; //Increase swap counter
j--; //decrease array index
}
}
cout<<endl<<"Array After Sorting = ";
for(int i=0;i<8;i++) //Loop to print our sorted array.
{
cout<<arr[i]<<" ";
}
cout<<endl;
cout<<"Total number of swaps performed: "<<swaps<<endl<<endl;
cin.get();
return 0;
}