1098. Insertion or Heap sort (25) "Sort"--pat (Advanced level) practise

Source: Internet
Author: User

Topic Information

1098. Insertion or Heap Sort (25)

Time limit (MS)
Memory Limit 65536 KB
Code length limit 16000 B

According to Wikipedia:

Insertion sort iterates, consuming one INPUT element each repetition, and growing a sorted output list. Each iteration, insertion sort removes one element from the input data, finds the location it belongs within the sorted Li St, and inserts it there. It repeats until no input elements remain.

Heap sort divides its input to a sorted and an unsorted region, and it iteratively shrinks the unsorted region by Extrac Ting the largest element and moving, the sorted region. It involves the use of a heap data structure rather than a linear-time search to find the maximum.

Now given the initial sequence of integers, together with a sequence which is a result of several iterations of some sorti Ng method, can you tell which sorting method we is using?

Input Specification:

Each input file contains the one test case. For each case, the first line gives a positive integer N (<=100). Then on the next line, N integers is given as the initial sequence. The last line contains the partially sorted sequence of the N numbers. It is assumed, the target sequence is always ascending. All the numbers in a line is separated by a space.

Output Specification:

For each test case, print in the first line either "insertion sort" or "Heap sort" to indicate the method used to obtain T He partial result. Then run this method for one more iteration and output in the second line the resuling sequence. It is guaranteed, the answer is unique for each test case. All the numbers in a line must is separated by a space, and there must is no extra space at the end of the line.

Sample Input 1:
10
3 1 2 8 7 5 9 4 6 0
1 2 3 7 8 5 9 4 6 0
Sample Output 1:
Insertion Sort
1 2 3 5 7 8 9 4 6 0
Sample Input 2:
10
3 1 2 8 7 5 9 4 6 0
6 4 5 1 0 3 2 7 8 9
Sample Output 2:
Heap Sort
5 4 3 1 0 2 6 7 8 9

Thinking of solving problems

Single-step sequencing to do tests

AC Code
#include <cstdio>#include <algorithm>#include <vector>using namespace STD;intMain () {intN, a, B; vector<int>VA, VB, VC;scanf("%d", &n); for(inti =0; I < n; ++i) {scanf("%d", &a);    Va.push_back (a); } for(inti =0; I < n; ++i) {scanf("%d", &b);    Vb.push_back (b); } VC = VA;intp = n; Make_heap (Vc.begin (), Vc.begin () + p); while(VC! = VB && P >=0) {Pop_heap (Vc.begin (), Vc.begin () + p);    --p; }if(vc = = vb) {printf("Heap sort\n");        Pop_heap (Vc.begin (), Vc.begin () + p);    vb = VC; }Else{p =1; while(P < n && vb[p-1] <= vb[p]) ++p; while(P >=0&& vb[p-1] > Vb[p]) {swap (vb[p], vb[p-1]);        --p; }printf("Insertion sort\n"); }printf("%d", vb[0]); for(inti =1; I < n; ++i) {printf("%d", Vb[i]); }return 0;}

1098. Insertion or Heap sort (25) "Sort"--pat (Advanced level) practise

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.