


How to implement selection sorting of python sorting algorithm
1. Introduction
Primary sorting algorithm refers to several relatively basic and easy-to-understand sorting algorithms. There are three primary sorting algorithms: insertion sort, selection sort and bubble sort. Although their efficiency is lower than that of advanced sorting algorithms, after understanding the primary sorting algorithm, it will be much easier to learn the relatively complex advanced sorting algorithm.
2. Description
Selection sorting means selecting the smallest or largest data from an unordered array each time, and putting it from the unordered array to the end of the ordered array to achieve sorting Effect.
The average time complexity of selection sort is O(n2), and the time complexity in the best case and the time complexity in the worst case are both O(n2). Additionally, it is an unstable sorting algorithm. The selection sort process is easy to understand. Taking the ascending sorting algorithm as an example, we first traverse the unsorted array and find the smallest element, as shown in Figure 2-4. Then, remove the smallest element from the unsorted array and add it to the end of the sorted array.
Because the smallest element is 1, 1 is added to the end of the ordered array which is still empty.
As shown in Figure 2-5, we continue to traverse the remaining elements. This time, the smallest element is 2. We add it to the end of the sorted array. This operation is correct because the elements in the sorted array must be smaller than the elements in the unsorted array.
As shown in Figure 2-6, repeat the above steps. When there is only one element left in the unsorted array, add it to the end of the sorted array. The entire The sorting of the array is completed.
3. Code implementation
Select sorting code:
nums = [5,3,6,4,1,2,8,7] res = [] #用于存储已排序元素的数组 while len(nums): #当未排序数组内还有元素时,重复执行选择最小数的代码 minInd = 0 #初始化存储最小数下标的变量,默认为第一个数 for i in range(1, len(nums)): if(nums[i] < nums[minInd]): #更新最小数的下标 minInd = i temp = nums[minInd] nums.pop(minInd) #把最小数从未排序数组中删除 res.append(temp) #把最小数插入到已排序数组的末尾 print(res)
Run the program, the output result is:
[1,2,3,4,5,6,7,8]
In the program, i in the first for loop represents the first position in the unsorted array, that is, the first position after the ordered array. Then, use a for loop to find the subscript of the minimum value in the unsorted array. Initially, the minimum index minInd is assigned the index of the first element of the unsorted array. When an element smaller than the current minimum is encountered, just update the index and iterate through the entire array. After exchanging the minimum value found with the first element in the unsorted array, the minimum value is placed at the end of the ordered array.
The above is the detailed content of How to implement selection sorting of python sorting algorithm. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

In VS Code, you can run the program in the terminal through the following steps: Prepare the code and open the integrated terminal to ensure that the code directory is consistent with the terminal working directory. Select the run command according to the programming language (such as Python's python your_file_name.py) to check whether it runs successfully and resolve errors. Use the debugger to improve debugging efficiency.

VS Code extensions pose malicious risks, such as hiding malicious code, exploiting vulnerabilities, and masturbating as legitimate extensions. Methods to identify malicious extensions include: checking publishers, reading comments, checking code, and installing with caution. Security measures also include: security awareness, good habits, regular updates and antivirus software.
