前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c#菜B学习之路3

c#菜B学习之路3

作者头像
py3study
发布2020-01-14 15:32:51
2530
发布2020-01-14 15:32:51
举报
文章被收录于专栏:python3

刚刚练习了冒泡算法,趁热打铁,练习鸡尾酒算法。Cocktail!

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace 鸡尾酒排序算法
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr1 = new int[] { 2, 5, 3, 18, 23, 12, 13, 16, 11 };
            Cocktail(arr1);
            for (int i = 0; i < arr1.Length; i++)
            {
                Console.Write(arr1[i] + " ");
            }
            Console.Read();
        }
        static void change(ref int left, ref int right)//change方法来作值交换
        {
            int temp;
            temp = left;
            left = right;
            right = temp;
        }
        static void Cocktail(int[] arr)//鸡尾酒排序
        {
            int up = arr.Length - 1;//设置排序上限
            int low = 0;//设置排序下限
            while (low < up)
            {
                for (int i = low; i < up; i++)
                {
                    if (arr[i] <= arr[i + 1])
                    {
                        continue;
                    }
                    else
                    {
                        change(ref arr[i], ref arr[i + 1]);
                    }
                }
                up--;
                for (int j = up; j > low; j--)
                {
                    if (arr[j] >= arr[j - 1])
                    {
                        continue;
                    }
                    else
                    {
                        change(ref arr[j], ref arr[j - 1]);
                    }
                    low++;
                }
            }
        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/07/04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档