Topic content:
In fact, the insertion sort method allows the number of a sequence to be increased, and the number of digits has been converted several times.
For example, a sequence 1 3 7 9 2, the first four numbers have been lined up, and at this time the fifth digit 2 comes in, he has to exchange with 9,7,3 so that the sequence becomes 1 2 3 7 9. This number 2 is the number of times the exchange is 3 times.
Input format:
Enter the first line as a number T, which represents the pen number of the test material. And then there's a T-measure, and the first line of each measurement is a positive integer n, which means there are n numbers. The second line of each measurement will have n numbers, and each digit is separated by a space.
Digital Range:
0 < N <= 100
0 numbers <= 1000000 in <= sequence
Output format:
Output a line of numbers, and then add the answer to each measurement.
Input Sample:
3
5
5 4 3) 2 1
3
1 2 3
6
1 3 5 2 4 6
Sample output:
13
time limit: 500ms memory limit: 32000kbImplementation code:
1#include <stdio.h>2 3 intSortint*,intN); 4 5 intMainvoid){6 intarr[ -];7 intT,n;8 intsum =0; 9 inti,j;Ten Onescanf"%d\n",&t); A for(i =1; i<=t;i++){ -scanf"%d\n",&n); - the for(j =0; j<n;j++){ -scanf"%d",&arr[j]); - } - +Sum + =sort (arr,n); - } + Aprintf"%d\n", sum); at return 0; - } - - intSortint*p,intN) { - intI,j,key,count =0; - in for(i =1; i < n; i++){ -Key =P[i]; to for(J=i; j>0&& p[j-1]>key; j--){ +P[J] = p[j-1]; -count++; the } *P[J] =key; $ }Panax Notoginseng - returncount; the}
Insert Sort Method---Topic