educoder data structure sorting level 1: to achieve direct insertion sorting

Table of contents

mission details

related information

programming requirements

Evaluation Description

Ac_Code


mission details

This level requires the completion function DirecInsSortto 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,18the 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 ".[ ][ ]i1n-1a[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; will a[1]insert a[0:0]to get a[0:1]: [46 58] 15 45 90 18; will a[2]insert a[0:1]to get a[0:2]: [15 46 58] 45 90 18; will insert to get : ; will a[3]insert to get : ; will insert to get : ;a[0:2]a[0:3][15 45 46 58] 90 18a[4]a[0:3]a[0:4][15 45 46 58 90] 18a[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 variable temp: temp = a[i];
  • Then a[i-1],a[i-2],…compare with in tempturn, if it is greater than temp, 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 DirecInsSortthe 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:

 
 
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include"directInsSort.h"
  4. void DirecInsSort(int* a, int n)
  5. // direct insert sorting
  6. {
  7. int i, k;
  8. for (i=1; i<n; i++) {
  9. // 请在此添加代码,补全函数DirecInsSort
  10. /********** Begin *********/
  11. /********** End **********/
  12. }
  13. }
  14. void SortPrint(int* a, int n)
  15. {
  16. int i;
  17. printf("sort result:");
  18. for (i=0; i<n; i++)
  19. printf("%d ", a[i]);
  20. printf("\n");
  21. }

Evaluation Description

The test file of this level is step1/Main.cpp, and the test process is as follows:

  1. The platform compiles step1/Main.cpp, then links related libraries and generates exe executable files;
  2. The platform runs the exe executable file and provides test input as standard input;
  3. 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 nan integer.

Output format : Output nthe 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");
}

Guess you like

Origin blog.csdn.net/m0_62853489/article/details/128523627
Recommended