Table of contents
mission details
This level requires the completion function DirecInsSort
to realize the function of direct insertion sorting.
related information
Symbol description: a[k:r]
refers to the sequence a[k] a[k+1] a[k+2] … a[r]
.
For simplicity of discussion, assume that each record to be sorted is an integer, and this integer is the sort code.
Direct insertion sorting : first consider the first record as an ordered record sequence, and then start from the second record, insert unsorted records into this ordered record sequence in turn, until the entire file All records are sorted.
Example : Suppose the sequence to be sorted is: , the sequence in
46,58,15,45,90,18
the following description is an ordered sequence, and the subsequent records are to be sorted. The sorting process is: from to execute " insert to get the arrangement from small to large ".[ ]
[ ]
i
1
n-1
a[i]
a[0..i-1]
a[0..i]
An example procedure is as follows : initial
a[0:0]
is an ordered sequence:[46] 58 15 45 90 18
; willa[1]
inserta[0:0]
to geta[0:1]
:[46 58] 15 45 90 18
; willa[2]
inserta[0:1]
to geta[0:2]
:[15 46 58] 45 90 18
; will insert to get : ; willa[3]
insert to get : ; will insert to get : ;a[0:2]
a[0:3]
[15 45 46 58] 90 18
a[4]
a[0:3]
a[0:4]
[15 45 46 58 90] 18
a[5]
a[0:4]
a[0:5]
[15 18 45 46 58 90]
The method that will a[i]
insert into a[0:i-1]
get a[0:i]
is:
- First
a[i]
put into a temporary variabletemp
:temp = a[i]
; - Then
a[i-1],a[i-2],…
compare with intemp
turn, if it is greater thantemp
, then move back one position; - The value to put in the last vacated slot
temp
.
programming requirements
The programming task of this level is to complete DirecInsSort
the functions in the step1/direcInsSort.cpp file, and implement direct insertion sorting in ascending order.
- For details, see subsequent test samples.
The code framework of the code file direcInsSort.cpp involved in this level is as follows:
#include<stdio.h>
#include<stdlib.h>
#include"directInsSort.h"
void DirecInsSort(int* a, int n)
// direct insert sorting
{
int i, k;
for (i=1; i<n; i++) {
// 请在此添加代码,补全函数DirecInsSort
/********** Begin *********/
/********** End **********/
}
}
void SortPrint(int* a, int n)
{
int i;
printf("sort result:");
for (i=0; i<n; i++)
printf("%d ", a[i]);
printf("\n");
}
Evaluation Description
The test file of this level is step1/Main.cpp, and the test process is as follows:
- The platform compiles step1/Main.cpp, then links related libraries and generates exe executable files;
- The platform runs the exe executable file and provides test input as standard input;
- The platform obtains the output of the exe executable file, and then compares it with the expected output, and if they are consistent, the test passes; otherwise, the test fails.
Input and output format description
Input format : First input a positive integer n
. Then enter n
an integer.
Output format : Output n
the result of sorting integers from small to large.
The following is the platform's sample test set for step1/Main.cpp:
Sample input:5
45 78 2 34 90
Sample output:sort result:2 34 45 78 90
Let's start your mission, I wish you success!
Ac_Code
/*************************************************************
date: April 2009
copyright: Zhu En
DO NOT distribute this code without my permission.
**************************************************************/
//排序算法实现文件
#include<stdio.h>
#include<stdlib.h>
#include"directInsSort.h"
void DirecInsSort(int* a, int n)
// direct insert sorting
{
int i, k;
for (i=1; i<n; i++) {
// 请在此添加代码,补全函数DirecInsSort
/********** Begin *********/
int temp=a[i];
//find k to insert a[i] between a[k-1] and a[k]
k=i;
while (k-1>=0 && temp<a[k-1]) // k>0 means k-1>=0
{ a[k]=a[k-1]; k--; }
a[k]=temp;
/********** End **********/
}
}
void SortPrint(int* a, int n)
{
int i;
printf("sort result:");
for (i=0; i<n; i++)
printf("%d ", a[i]);
//printf("\n");
}