Code example of C# implementing bubble sorting algorithm
1. Principle:Start from the first position of the array to compare array[index] and array[index+1] in pairs. If array[index] is greater than array[index+1], then exchange the positions of array[index] and array[index+1] and stop until the end of the array;
Start from the first position of the array, repeat the above action, and end until the end of the length of the array is reduced by one position;
Start from the first position of the array, repeat the above action, and end until the two positions of the array length are reduced;
。。。。
2. Time complexity: O(N²),(n-1)*(n-2)...=n*(n-1)/2 comparisons and the number of exchanges is about half of the number of comparisons (in both cases), then the time complexity according to the large O representation is O(N^2)
3. Code example:
using System; namespace MySort { // Create a class first, and then put all sorting methods into this class. public class SumSort { //The bubble sorting method is from small to large. Although many bubble sortings are from large to small, //But I just want to arrange this way, what can you do to me? public void PopSort(int[] list) { int i, j, temp; //Define the variable to use first for (i = 0; i < - 1; i++) { for (j = i + 1; j < ; j++) { if (list[i] > list[j]) //If the second one is smaller than the first one { //Swap the positions of two numbers, here you can also write a swap method separately, just call it here temp = list[i]; //Put the large number in a temporary storage location list[i] = list[j]; //Then assign the small number to the previous one to ensure that the minimum before each order is list[j] = temp; //Then assign the large number in the temporary position to the next } } } } } public class test { // Here is a set of test data, print the output to see how the sorting method works static void Main() { int[] arr = { 1, 4, 2, 43, 5, 61, 89, 34, 67, 32, 40 }; //Instantiate the data sorting class and then call the method. //What? I also need an instance, but what should I do if I don’t want to instantiate it? //That doesn't matter. Just add a static before the PopSort method and call (arr) directly SumSort mysort = new SumSort(); //Come on, everyone line up according to height, short ones in front, tall ones behind, tall ones behind. (arr); //Be really obedient, see which one everyone ranks for (int i = 0; i < ; i++) { ("The{0}The bit is{1}\n", i + 1, arr[i]); } (); } } }
Related Articles
Example of method to implement WPS file to PDF format in C#
This article mainly introduces the method of C# to implement WPS file to PDF format, involving C#'s related references and operation techniques for office components. Friends who need it can refer to it2017-11-11Overview of WinForm's Delay Loading Controls
This article mainly introduces WinForm's delay loading control, which is very practical and has a wide range of applications in C# programming. Friends who need it can refer to it.2014-08-08Example of OCX control written by C# Call vc
This article mainly introduces the example of OCX control written by C# calling vc. Friends who need it can refer to it2014-04-04Basic operations of using Redis in C#
This article mainly introduces the basic operations of using Redis in C#. Friends who need it can refer to it.2017-06-06Problem of multiple return values for C# function out
This article mainly introduces multiple return value issues of C# function out, which has good reference value, and hopes it will be helpful to everyone. If there are any mistakes or no complete considerations, I hope you will be very grateful for your advice2023-02-02C# example code to implement dynamic execution of string scripts (optimized version)
This article mainly introduces in detail how C# can implement dynamic execution of string scripts (optimized version). The sample code in the article is explained in detail. Interested friends can follow the editor to learn it.2023-03-03In-depth analysis of generic classes and generic interfaces in C#
This article mainly introduces the generic classes and generic interfaces in C#. Support for generics is an important feature of C# language. Friends who need it can refer to it.2016-02-02Introduction to the basics of C# language introduction
This article mainly introduces the introductory basics of C# language, which is a new programming language that is not only object-oriented, but also type-safe. This tutorial outlines the main components of the language in C# 8 and later. Let’s go to the article to learn more details2021-12-12A simple QR code and barcode generation tool based on C#
This article mainly introduces in detail how to implement simple QR codes and barcode tools based on C# for the generation and recognition of QR code barcodes. Interested friends can follow the editor to learn about it.2023-12-12C# Calculate the time difference between the incoming time and today
This article introduces the time difference between C# and today's time difference in C# calculation of the incoming time distance. The code is simple and easy to understand. If you need it, please refer to it.2017-08-08