加入收藏 | 设为首页 | 会员中心 | 我要投稿 北几岛 (https://www.beijidao.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

c# 使用“冒泡”法对数组元素进行排序

发布时间:2021-07-06 05:39:11 所属栏目:大数据 来源: https://blog.csdn.net/summoxj
导读:结果如图所示 代码如下: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApp3__冒泡_法排序_数组_ { ? ? class Program ? ? { ? ? ? ? static void Main(string[

结果如图所示

代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApp3__冒泡_法排序_数组_
{
? ? class Program
? ? {
? ? ? ? static void Main(string[] args)
? ? ? ? {
? ? ? ? ? ? double[] a = new double[10];
? ? ? ? ? ? Console.WriteLine("请输入10个数:");
? ? ? ? ? ? for(int i = 0; i < 10; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.Write("第{0}个数:",i+1);
? ? ? ? ? ? ? ? a[i] = double.Parse(Console.ReadLine());
? ? ? ? ? ? }
? ? ? ? ? ? for(int i = 0; i < 10; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.Write("{0}t",a[i]);
? ? ? ? ? ? }
? ? ? ? ? ? Console.WriteLine();
? ? ? ? ? ? double temp;
? ? ? ? ? ? for(int i = 0; i < 9; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? for(int j = 0; j < 9-i ; j++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (a[j] > a[j+1])
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? temp = a[j];
? ? ? ? ? ? ? ? ? ? ? ? a[j] = a[j + 1];
? ? ? ? ? ? ? ? ? ? ? ? a[j + 1] = temp;
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? Console.WriteLine("排好序为:");
? ? ? ? ? ? for(int j = 0; j < 10; j++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.Write("{0}t",a[j]);
? ? ? ? ? ? }
? ? ? ? }
? ? }
}

(编辑:北几岛)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读