Insert Sort Method---Topic

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.