


Learn how to use selection sort and bubble sort in three minutes
Today I will share with you some algorithms about C language, Selection sort and bubble sort.
For selection sorting, first understand the idea of sorting. Given an array, this idea first assumes that the first element of the array is the largest or smallest. At this time, three variables will be used to represent the subscripts of the elements.
One represents the current one, one represents the largest or smallest subscript found, and one is used to store the subscript of the maximum value in each cycle. After mastering the basic idea of the program, proceed with sequencing. After finding the largest subscript, assign it to the largest subscript except each time.
After finding it, determine whether the assumed current value is the maximum value of this cycle. If not, exchange the maximum and current values, thereby arranging the array in a certain order, and finally write a loop to output the result. .
The code is not difficult, so I will explain it step by step. I just attach the code. If you don’t understand, you can leave me a message and I will explain it to you. Or if there is anything wrong, I can correct it.
#include<stdio.h> void main()//主函数 { int a[10]; int i,j,w; printf("请输入10个数字: \n"); for(i=0;i<10;i++) scanf("%d",&a[i]); for(i=0;i<10;i++) { for(j=0;j<10;j++) if(a[i]<a[j])//进行比较 //比较后进行交换 { w=a[i]; a[i]=a[j]; a[j]=w; } } printf("排序后:\n"); for(i=0;i<10;i++) printf("%4d",a[i]); printf("\n"); }
Result display:
Next is Bubble sort, this is the most popular in C language One of the most commonly used algorithms, because it is easier to understand, it is the first thing most people use when they want to sort. This algorithm is relatively easy to understand.
For bubble sorting, the main idea is to compare adjacent numbers in pairs. If the latter one is larger or smaller than the previous one, swap its positions until all numbers are compared.
If an array of size n is given, n-1 comparisons are required, and each comparison is n-1-i times. i represents the subscript that has been compared in the last loop. Write two loop judgments. If exchange is needed, exchange it. If exchange is not required, compare the next two numbers until all numbers are compared.
Finally, use a loop to output all the sorted numbers. The code is as follows:
#include<stdio.h> #define N 10 void main() { int a[10]; int i,j,t; printf("请输入10个数字: \n"); for(i=0;i<10;i++) scanf("%d",&a[i]); //使用两层循环 for(i=0;i<N-1;i++) { for(j=i+1;j<N-(i+1);j++) if(a[j]<a[j+1])//比较大小 { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } } printf("排序后:\n"); for(i=0;i<10;i++) printf("%4d",a[i]); printf("\n"); }
Result:
##Conclusion:
For selection The analysis of sorting is very simple. The size of the input is determined by the array elements. The basic operation is the key value comparison A[j]Therefore, for any input, selection sort is an O(n^2) algorithm. Therefore, the time complexity of this experiment is O(100) and the space complexity is O(10). However, the number of times the keys are exchanged is only O(n), or to be more precise, n-1 times (one exchange is performed for each iteration of the i loop). This feature makes selection sort superior to many other sorting algorithms. Bubble sorting is a process in which two adjacent numbers are compared, and the larger number sinks to the bottom (or the smaller number floats up). A total of n-1 comparisons and exchanges are performed. In order to facilitate the implementation of the above bubble algorithm, consider using only a one-dimensional array to store 10 integer data. During the sorting process, the data is always in this array (operated in place and does not occupy additional space). Therefore, the time complexity of this algorithm is O(n-1) and the space complexity is O(1). Thank you everyone for reading, I hope you will benefit a lot. This article is reproduced from: https://blog.csdn.net/zjy18886018024/article/details/80718713 Recommended tutorial: "C Language"
The above is the detailed content of Learn how to use selection sort and bubble sort in three minutes. For more information, please follow other related articles on the PHP Chinese website!

C# and .NET provide powerful features and an efficient development environment. 1) C# is a modern, object-oriented programming language that combines the power of C and the simplicity of Java. 2) The .NET framework is a platform for building and running applications, supporting multiple programming languages. 3) Classes and objects in C# are the core of object-oriented programming. Classes define data and behaviors, and objects are instances of classes. 4) The garbage collection mechanism of .NET automatically manages memory to simplify the work of developers. 5) C# and .NET provide powerful file operation functions, supporting synchronous and asynchronous programming. 6) Common errors can be solved through debugger, logging and exception handling. 7) Performance optimization and best practices include using StringBuild

.NETFramework is a cross-language, cross-platform development platform that provides a consistent programming model and a powerful runtime environment. 1) It consists of CLR and FCL, which manages memory and threads, and FCL provides pre-built functions. 2) Examples of usage include reading files and LINQ queries. 3) Common errors involve unhandled exceptions and memory leaks, and need to be resolved using debugging tools. 4) Performance optimization can be achieved through asynchronous programming and caching, and maintaining code readability and maintainability is the key.

Reasons for C#.NET to remain lasting attractive include its excellent performance, rich ecosystem, strong community support and cross-platform development capabilities. 1) Excellent performance and is suitable for enterprise-level application and game development; 2) The .NET framework provides a wide range of class libraries and tools to support a variety of development fields; 3) It has an active developer community and rich learning resources; 4) .NETCore realizes cross-platform development and expands application scenarios.

Design patterns in C#.NET include Singleton patterns and dependency injection. 1.Singleton mode ensures that there is only one instance of the class, which is suitable for scenarios where global access points are required, but attention should be paid to thread safety and abuse issues. 2. Dependency injection improves code flexibility and testability by injecting dependencies. It is often used for constructor injection, but it is necessary to avoid excessive use to increase complexity.

C#.NET is widely used in the modern world in the fields of game development, financial services, the Internet of Things and cloud computing. 1) In game development, use C# to program through the Unity engine. 2) In the field of financial services, C#.NET is used to develop high-performance trading systems and data analysis tools. 3) In terms of IoT and cloud computing, C#.NET provides support through Azure services to develop device control logic and data processing.

.NETFrameworkisWindows-centric,while.NETCore/5/6supportscross-platformdevelopment.1).NETFramework,since2002,isidealforWindowsapplicationsbutlimitedincross-platformcapabilities.2).NETCore,from2016,anditsevolutions(.NET5/6)offerbetterperformance,cross-

The C#.NET developer community provides rich resources and support, including: 1. Microsoft's official documents, 2. Community forums such as StackOverflow and Reddit, and 3. Open source projects on GitHub. These resources help developers improve their programming skills from basic learning to advanced applications.

The advantages of C#.NET include: 1) Language features, such as asynchronous programming simplifies development; 2) Performance and reliability, improving efficiency through JIT compilation and garbage collection mechanisms; 3) Cross-platform support, .NETCore expands application scenarios; 4) A wide range of practical applications, with outstanding performance from the Web to desktop and game development.


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

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
