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;}