C language classic algorithm example 3: sorting array elements

insert image description here

1. Problem description

Find the sorting of an array The
description of the problem is as
follows

  1. Use the rand() library function to randomly generate 10 numbers between 1-100.
  2. The size of the declared array is 10.
  3. 10 randomly generated numbers are assigned to the array.
  4. Sort the elements in the array from smallest to largest.

2. Algorithm instance compilation environment

The compilation environment of the C language classic algorithm example in this article uses the integrated development environment: Visual Studio 2019
insert image description here

insert image description here

Visual Studio 2019 official website link is as follows

Visual Studio 2019 official website link
insert image description here

The features of the Visual Studio 2019 integrated development environment are:

    1. Visual Studio 2019 installs the Live Share code collaboration service by default.
    1. New welcome window to help users write code quickly, improved search functionality, general performance improvements.
    1. Visual Studio IntelliCode AI Help.
    1. Better Python virtual and Conda support.
    1. As well as support for .NET Core 3.0 projects including WinForms and WPF, etc.

Third, the algorithm example implementation process

3.1, include header files

Include the header file code as follows


#pragma once


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX 10		// 定义宏


  • The C language header files that will be used are included in recent years.

3.2. Defining macros and declaring arrays

The code for defining the macro and declaring the array is as follows

	#define MAX 10		// 定义宏
    int myArr[MAX];     // 定义数组变量
  • MAX is defined, representing that MAX is a constant 10.
  • The array myArr is declared.

3.3, declare related variables

The code for declaring the relevant variables is as follows

    int i, j, tempVal;  // 定义变量
  • Declare related variables i, j, tempVal.

3.3. Randomly generate ten numbers and assign them to the array

The code to randomly generate ten numbers and assign them to the array is as follows

	 /// <summary>
    /// 随机生成十个数字赋值给数组
    /// </summary>
    /// <returns></returns>
    srand(time(NULL));
    for (i = 0; i < 10; i++)
    {
    
    
        myArr[i] = rand() % 100 + 1;
    }
  • srand(time(NULL)) is guaranteed to generate a different number each time.
  • Through the loop, assign ten randomly generated numbers to the array.
    insert image description here

3.4. Output ten randomly generated numbers

The output randomly generated ten numeric codes are shown below

 	 /// <summary>
    /// 输出随机生成的十个数字
    /// </summary>
    /// <returns></returns>
    printf("The ten randomly generated numbers are as follows\n");
    for (i = 0; i < 10; i++)
    {
    
    
        printf("%d ", myArr[i]);
    }
    printf("\n");
  • Output the data we put into the array.

Press F5 to compile, and the debugging results are as follows.

insert image description here

  • Can correctly output randomly generated numbers and data stored in arrays.

3.5. Sort the array from small to large

The code to sort the array from small to large is as follows

	 /// <summary>
    ///  数组从小到大进行排序
    /// </summary>
    /// <returns></returns>
    for (j = 0; j < 10; j++)
    {
    
    
        for (i = 0; i < 9 - j; i++)
        {
    
    
            if (myArr[i] > myArr[i + 1])
            {
    
    
                tempVal = myArr[i];

                myArr[i] = myArr[i + 1];
                
                myArr[i + 1] = tempVal;
            }
        }
    }
  • Arrays are sorted from smallest to largest
  • The sorting method used is bubble sort

3.6. Output sorted numbers of array elements

The sorted numeric code of the output array elements is shown below

 /// <summary>
    /// 输出数组元素排序好的数字
    /// </summary>
    /// <returns></returns>
    printf("\nThe ten randomly generated numbers are sorted from smallest to largest as follows\n");
    for (i = 0; i < 10; i++)
    {
    
    
        printf("%d ", myArr[i]);
    }
    printf("\n\n");

  • Can output sorted numbers.
  • Numbers are stored in numbers.

Press F5 to compile, and the debugging results are as follows.

insert image description here

The ten randomly generated numbers are as follows
78 95 27 65 62 83 19 74 8 90

The ten randomly generated numbers are sorted from smallest to largest as follows
8 19 27 62 65 74 78 83 90 95

请按任意键继续. . .
  • It can be seen that the numbers are sorted and output from small to large.
  • The sorting algorithm meets the requirements.

Fourth, the complete code of the classic algorithm example program

The complete code of the classic algorithm example program is as follows

4.1, main.h file

#pragma once


#include <stdio.h>
#include <stdlib.h>
#include <time.h>


#define MAX 10		// 定义宏

4.2, main.c file

#define _CRT_SECURE_NO_WARNINGS

#include "Main.h"

int main()
{
    
    
    system("color 3E");

    int myArr[MAX];     // 定义数组变量

    int i, j, tempVal;  // 定义变量

    /// <summary>
    /// 随机生成十个数字赋值给数组
    /// </summary>
    /// <returns></returns>
    srand(time(NULL));
    for (i = 0; i < 10; i++)
    {
    
    
        myArr[i] = rand() % 100 + 1;
    }

    /// <summary>
    /// 输出随机生成的十个数字
    /// </summary>
    /// <returns></returns>
    printf("The ten randomly generated numbers are as follows\n");
    for (i = 0; i < 10; i++)
    {
    
    
        printf("%d ", myArr[i]);
    }
    printf("\n");

    /// <summary>
    ///  数组从小到大进行排序
    /// </summary>
    /// <returns></returns>
    for (j = 0; j < 10; j++)
    {
    
    
        for (i = 0; i < 9 - j; i++)
        {
    
    
            if (myArr[i] > myArr[i + 1])
            {
    
    
                tempVal = myArr[i];

                myArr[i] = myArr[i + 1];

                myArr[i + 1] = tempVal;
            }
        }
    }

    /// <summary>
    /// 输出数组元素排序好的数字
    /// </summary>
    /// <returns></returns>
    printf("\nThe ten randomly generated numbers are sorted from smallest to largest as follows\n");
    for (i = 0; i < 10; i++)
    {
    
    
        printf("%d ", myArr[i]);
    }
    printf("\n\n");


    system("pause");
    return 0;
}

V. Summary

An example of a classic algorithm in C language: sorting array elements, the goals to be achieved are as follows and the main points are as follows

  1. Use the rand() library function to randomly generate 10 numbers between 1-100.
  2. The size of the declared array is 10.
  3. 10 randomly generated numbers are assigned to the array.
  4. Sort the elements in the array from smallest to largest.
  5. The sorting method is bubble sort.

insert image description here

The article ends here.
I hope this article is an example of a classic algorithm in C language: sorting of array elements.
It can stimulate your love for C language and algorithm learning.

Guess you like

Origin blog.csdn.net/m0_47419053/article/details/126688181
Recommended