数据结构常用算法

将一整数序列中的所有负数转移到所有正数之前

 

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

namespace CSharpTest
{
    class Program
    {
        public static int[] Sort(int[] A)
        {
            int low = 0, high = A.Length - 1;
            int t = A[0];
            while (low < high)
            {
                while (low < high && A[high] >= 0)
                    high--;

                if (low < high)
                {
                    A[low] = A[high];
                    low++;
                }
                low++;
                while (low < high && A[low] < 0)
                    low++;
                if (low < high)
                {
                    A[high] = A[low];
                    high--;
                }
                A[low] = t;
            }
            return A;
        }

        static void Main(string[] args)
        {
            int[] test = new int[] { 10, -19, 88, -81, 100, -25, 778, 22, -971 };
            int[] testResult =  Sort(test);
           
            //print all data
            foreach(int d in testResult)
            {
                Console.Write(d.ToString());
                Console.Write("/n");
            }

            Console.Read();
        }
    }
}

起泡算法

 

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

namespace CSharpTest
{
    class Program
    {
        static void Main(string[] args)
        {
           int[] test = new int[] { 10, -19, 88, -81, 100, -25, 778, 22, -971 };
           int[] testResult = Bubble(test);
           
            //print all data
            foreach(int d in testResult)
            {
                Console.Write(d.ToString());
                Console.Write("/n");
            }

            Console.Read();
        }

        static int[] Bubble(int[] L)
        {
            int temp;
            for (int i = 0; i < L.Length-1; i++)
            {
                for (int j = 0; j < L.Length-1-i; j++)
                {
                    if (L[j] > L[j + 1])
                    {
                        temp = L[j + 1];
                        L[j + 1] = L[j];
                        L[j] = temp;
                    }
                }
            }
            return L;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值