Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize Bubble sort, insert sort and Select sort in C # algorithm

2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the C# algorithm how to achieve bubble sorting, insertion sorting, selection sorting, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article. Next, let the editor take you to understand it.

Bubbling sorting method

It is an array of linearly arranged numbers from large to small or from small to large.

Take sorting from small to big as an example.

Data 11, 35, 39, 30, 7, 36, 22, 13, 1, 38, 26, 18, 12, 5, 45, 32, 6, 21, 42, 23

Use the array int [] array to store numbers.

Procedure (array sorted from small to large)

The thinking cycle puts the largest number in the last place, and the number of unordered numbers minus 1.

I is the current task location, n the number of unordered numbers left

Starting from bit 0, compare the size of the two digits before and after, and when array [I] > array [iTunes 1], the values are exchanged.

After a loop, the largest value has been saved to the last bit of the array.

The number of out-of-order numbers nMel 1

For (int j = array.Length-1; j > 0; JMY -) / / each row, minus one {for (int I = 0; I) of the remaining unordered numbers.

< j; i++)    //一个for循环获得一个最大的数 { if (array[i] >

Array [I + 1]) / / numerical exchange {var sap = array [I]; array [I] = array [I + 1]; array [I + 1] = sap;}

Sort result

The moving picture is as follows

Insertion sorting method

The insertion sorting algorithm inserts a number into an already sorted array.

For example, insert 22 into [1, 5, 10, 17, 28, 39, 42]

Results [1, 5, 10, 17, 22, 28, 39 and 42].

Use insertion sorting for arrays

Array int [] array = [11, 39, 35, 30, 7, 36, 22, 13, 1, 38, 26, 18, 12, 5, 45, 32, 6, 21, 42, 23]

Array elements are unordered, set a direction from big to small or from small to big, the first is ordered [11]

Insert for the first time: [11, 39, 35, 30, 7, 36, 22, 13, 1, 38, 26, 18, 12, 5, 45, 32, 6, 21, 42, 23].

Take the second number and compare it with the first one, and the two are in order [11jue 39]

Second insertion: [11, 39, 35, 30, 7, 36, 22, 13, 1, 38, 26, 18, 12, 5, 45, 32, 6, 21, 42, 23]

Take the third number, [11, 39, 35], and insert it.

[11,35, 39, 30, 7, 36, 22, 13, 1, 38, 26, 18, 12, 5, 45, 32, 6, 21, 42, 23]

......

In the future, take one number at a time and insert the array.

There are many ways to realize it, and the author's method is similar to the bubbling sorting method.

Public static void ReSort (ref int [] array) {for (int I = 0; I)

< array.Length; i++)    //要将第几位数进行插入 { for (int j = i; j >

0; jmurf -) {if (array [j] > array [j-1]) break; / / if the number to be sorted is greater than the maximum value of the sorted element, there is no need to compare. Otherwise, it is necessary to keep comparing and finding the right location else {int sap = array [j]; array [j] = array [j-1]; array [j-1] = sap }

Try copying the following code to the console, and you can see the result of each sort.

Using System;namespace ConsoleApp1 {class Program {public static void ReSort (ref int [] array) {for (int I = 0; I)

< array.Length; i++) { Console.WriteLine("\n- - - - - - -"); Console.WriteLine("\n未排序前:"); for (int sun = 0; sun 0; j--) { if (array[j] >

Array [j-1]) break; else {int sap = array [j]; array [j] = array [j-1]; array [j-1] = sap }} Console.WriteLine ("\ nsorted:"); for (int sun = 0; sun

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report