算法思路:重复走访每两个相邻元素,比较大小交换位置,直至排序完成。有兴趣电话可以看一下这个【冒泡排序踢踏舞】的视频,很形象的演示了排序过程,额呵呵~~性质:冒泡排序是一种原地排序(只有常数个元素存到数组以外的空间),最坏的时间复杂度,和平均时间复杂度都是n2。*注: 冒泡排序是算法入门级别,是面试笔试时候的禁术,古往今来死在冒泡法上的应届生真可谓前仆后继...代码:int[] BubbleSort1(int[] a){int num; for (...
class Program{staticvoid swap( refint atemp, refint btemp)//注意ref的使用 {int temp = atemp;atemp = btemp;btemp = temp;}staticvoid Main(string[] args){int temp=0;int[]arr={23,44,66,76,98,11,3,9,7};Console.WriteLine("排序前的数组:");foreach(int item in arr){Console.Write(item+"");}Console.WriteLine();for(int i=0;i<arr.Length-1;i++){for(int j=0;j<arr.Length-1-i;j++){if (arr[j] > arr[j + 1]...
通过比较,将小的数字向上浮动一位,直到整个数组的数字对比完成,结果:5,7,9,22,47,52,64.var myArray = new int[] {5,7,47,52,64,9,22}; for (var j = 1; j< myArray.Length; j++) { for (var i= 0; i < myArray.Length-1; i++) { if (myArray[i] <= myArray[i + 1]) continue; var temp = myArray[i]; myArray[i] = myArray[i + 1]; myArray[i + 1] = temp; } }原文...
冒泡: 1 //将 1, 5, 6, 8, 4, 3, 9, 2, 7 按照从小到大排序2 3 int [] a={ 1, 5, 6, 8, 4, 3, 9, 2, 7 }4 5 int temp;6 for (i=0 , i< a.lenght ,i++)7 {8 9 for(j=i+1,j<a.lenght,j++)
10 {
11 if (a[i]>a[j])
12 {
13 temp=a[i];
14 a[i]=a[j];
15 a[j]=temp;
16
17 }
18 }
19 } 递归: 1 //1,1,2,3,5,8,13 第10位数是多少2 3 foot(10)4 5 public state int foot(int count)6 {7 if (count <=0)8 return 0;9 e...
永远不要被阴云吓倒,只要我们相信自己http://www.shuhua66.com/space-4869-do-blog-id-152059.html它能帮助我们分辩出真善美和假丑恶http://www.shuhua66.com/space-4869-do-blog-id-151821.html是每次跌倒都能站起来的人http://www.shuhua66.com/space-4869-do-blog-id-151586.html它能让我们看到世间百相http://www.shuhua66.com/space-4869-do-blog-id-151353.html通过自己的努力和勤奋走到了现在乐http://www.shuhua66.com/sp...
排序规则:比较相邻的元素。如果第一个比第二个大,就交换它们两个。对每对相邻元素做同样的工作,从开始第一对到最后一对。这步做完之后,最后的元素会是最大的数。针对所有的元素重复以上的步骤,除了最后一个。持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要进行比较。
时间复杂度:O(n^2)using System;
using System.Collections.Generic;
using System.Text;namespace 排序算法
{class BubbleSort{public...
冒泡排序法:
1.比较相邻的元素。如果第一个比第二个大,就交换他们两个。
2.对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。
3.针对所有的元素重复以上的步骤,除了最后一个。
4.持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。
void MaoPao(int[] arr){for(int i=0;i<arr.Length-1;i++){ //外层循环,控制比较次数for(int j=0;j<arr.Length-1-i...
本文采用冒泡排序法对二维数组进行排序
代码内容如下
using System;namespace experment4
{class Program{private static int result;private static int result1;static void Main(string[] args){string[,] a = new String[5,2];a[0, 0] = "2"; a[0, 1] = "Smith";a[1, 0] = "4"; a[1, 1] = "John";a[2, 0] = "5"; a[2, 1] = "Mary";a[3, 0] = "1"; a[3, 1] = "Cherr";a[4, 0] = "3"; a[4, 1] = "Tom";Console.WriteLine("排序前...
请编程实现一个冒泡排序算法?
算法描述:
冒泡排序算法(Bubble Sort)是一种流行但低效的排序算法。它的原理是反复比较待排序数组中所有相邻的两个数据,使他们按照升序(或降序)排列。当待排序数组中所有相邻数据都比较过一次之后,待排序数组中最小(或最大)的数据会被逐步交换到第一位,就像气泡从水底慢慢升到水面一样,故名“冒泡排序算法”。
冒泡排序的优点:每进行一趟排序,就会少比较一次,因为每进行一趟排序都会找...
冒泡排序
一般冒泡排序
void BubbleSort(int[] arr)
{for (int i = 0; i < arr.Length - 1; i++){for (int j = 0; j < arr.Length - i - 1; j++){if (arr[j] > arr[j+1]){int temp = arr[j];arr[j] = arr[j+1];arr[j+1] = temp;}}}
}??假设我们现在排序arr[]={1,2,3,4,5,6,7,8,10,9}这组数据,按照上面的排序方式,第一趟排序后将10和9交换已经有序,接下来的8趟排序就是多余的,什么也没做。定义一个flag做标记用,初始值为0,所以...
一.冒泡排序
口诀:
N 个数字来排队,两两相比小靠前。
? 外层循环 N-1,内层循环 N-1-i。
案例:定义一个数组,输出后从大到小排列
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace _04冒泡排序
{class Program{static void Main(string[] args){//冒泡排序算法://大的往上冒,小的往下沉//对于一组数进行按照从大到小或从小到大的一个顺序排列算法...
public void BubbleSortArrayString(string[] letters) //change here
{bool swap;string temp; //change this toodo{swap = false;for (int index = 0; index < (letters.Length - 1); index++){if (letters[index] > letters[index + 1]) //if first number is greater then second then swap{//swaptemp = letters[index];letters[index] = letters[index + 1];letters[index + 1] = temp;swap = true;}}} while (swap == true)...
数据的排序方法有多种,每种排序都有各自的特点和优点,在实际的使用中需要根据实际的情况灵活的选择排序方式,不仅可以提高效率,还可以节约资源。以下用选择排序,冒泡排序和快速排序三种排序方法对相同大小的数据进行排序效率的比较。
以下是设计器代码: 1 namespace SortsCompare2 {3 partial class FormSort4 {5 /// <summary>6 /// 必需的设计器变量。7 /// </summary>8 private ...
冒泡排序基础
冒泡排序原理图分析tmp在算法中起到数据交换的作用
int[] intNums = { 12,6,9,3,8,7 };
int tmp = intNums[0];
// 一共5次冒泡,从1开始
for(int i = 1; i < intNums.Length; i++)
{// 每一次冒泡比较次数for(int j = 1; j <= intNums.Length - i; j++){if (intNums[j - 1] > intNums[j]){tmp = intNums[j-1];intNums[j - 1] = intNums[j];intNums[j] = tmp;}}
}
for(int i = 0; i < intNums.Length; i++)
{Console.W...
结构体
结构体不能重写默认无参构造函数
一位数组 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace m1w2d3_struct_array
{//用结构体描述一个学生的信息struct Student{public Point postion;public Rect body;int id;public string name;public float cSharp;float unity;public ConsoleColor color;//Student desker;不能包含自身,会死循环 ...