ACM -- sort by direct insertion

Source: Internet
Author: User
NOJ--1062 insert sort directly

Time limit (Common/Java): 1000 ms/3000 Ms running memory limit: 65536 Kbyte
Total submissions: 446 pass the test: 212

Description

Given the number of input sorting elements N and the corresponding n elements, write the program and use the inner Sorting Algorithm to directly Insert the Sorting Algorithm for sorting, and output the corresponding sequence of each trip and the final result in the sorting process.

Input

There are two rows in total. The first row gives N number of sorting elements, the second row gives n elements, 1 ≤ n ≤ 400, and each element value range is)

Output

Three parts: Part 2 is two rows, and the output text of Line 3 is "Source:", and row 3 gives the original sequence. Part 3 begins to output the text "insert sort :", the sorting process is selected for subsequent output;

In Part 2, the text "Result:" Is output, and the sorting result is output later.

Sample Input

7
48 36 68 72 12 48 2

Sample output

Source:
(48) 36 68 72 12 48 2
Insert sort:
(36 48) 68 72 12 48 2
(36 48 68) 72 12 48 2
(36 48 68 72) 12 48 2
(12 36 48 68 72) 48 2
(12 36 48 48 68 72) 2
(2 12 36 48 68 72)
Result:
(2 12 36 48 68 72)

Prompt

Data structure a Experiment 4

Question Source

Chenz

 

#include<iostream>#include<stdio.h>using namespace std;int main(){    int N,i,j;    scanf("%d",&N);    int* p=new int[N];    for(i=0;i<N;i++)        scanf("%d",&p[i]);    printf("Source:\n(%d)",p[0]);    for(i=1;i<N;i++)    printf(" %d",p[i]);    printf("\nInsert Sort:\n");    for(i=1;i<N;i++){        if(p[i]<p[i-1])        {        int temp=p[i];        for(j=i-1;j>=0&&temp<p[j];j--){        p[j+1]=p[j];        }        p[j+1]=temp;        }        printf("(%d",p[0]);        for(int t=1;t<=i;t++) printf(" %d",p[t]);        printf(")");        for(int t=i+1;t<N;t++) printf(" %d",p[t]);        printf("\n");    }    printf("Result:\n(%d",p[0]);    for(i=1;i<N;i++)    printf(" %d",p[i]);    printf(")\n");        return 0;}

 

 

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.