C#:冒泡排序

来源:互联网 发布:mysql数据库方言 编辑:程序博客网 时间:2024/05/16 04:59

//冒泡排序1
using System;
namespace BubbleSorter
{
    public class BubbleSorter
    {
        public void Sort(int [] list)
        {
            int i,j,temp;
            bool done=false;
            j=1;
            while((j<list.Length)&&(!done))
            {
                done=true;
                for(i = 0;i<list.Length - j;i ++)
                {
                    if(list[i] > list[i + 1])
                    {
                        done=false;
                        temp=list[i];
                        list[i]=list[i+1];
                        list[i+1]=temp;
                    }
                }
                j++;
            }     
         }
    }  
    public class MainClass
    {
        public static void Main()
        {
            int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
            BubbleSorter sh=new BubbleSorter();
            sh.Sort(iArrary);
            for (int m = 0; m < iArrary.Length; m++)
            Console.Write("{0} ",iArrary[m]);
            Console.WriteLine();
            //Console.WriteLine();
        }
    }
}

//冒泡排序2

using System;

namespace SelectionSorter
{
    public class SelectionSorter
    {
        private int min;
        public void Sort(int [] list)
        {
            for (int i = 0;i <list.Length -1;i ++)
            {
                min=i;
                for(int j= i +1 ;j<list.Length;j++)
                {
                    if(list[j]<list[min])
                    min=j;
                }
                int t=list[min];
                list[min]=list[i];
                list[i]=t;
            }
        }

    
    }
    public class MainClass
    {
        public static void Main()
        {
            int[] iArrary=new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
            SelectionSorter ss=new SelectionSorter();
            ss.Sort(iArrary);
            for(int m=0;m<iArrary.Length;m++)
            Console.Write("{0} ",iArrary[m]);
            Console.WriteLine();

        }
    }
}

//选择排序

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

namespace 选择排序
{
    class Program
    {
        private int min;
        public void sort(int[] list)
        {
            for (int i = 0; i < list.Length - 1; i++)
            {
                min = i;
                for (int j = i + 1;j <list.Length;j++)
                {
                    if (list[j] < list[min])
                        min = j;
                }
                int t = list[min];
                list[min] = list[i];
                list[i] = t;

 

            }
        }

        static void Main(string[] args)
        {
            int [] Arrlist = new int[] {1,21,55,88,74};
            Program sh = new Program();
            sh.sort(Arrlist);
            for (int m = 0; m < Arrlist.Length; m++)
                Console.Write("{0} ", Arrlist[m]);
            Console.WriteLine();
        }
    }
}

// 数组冒泡排序

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

namespace 数组
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] myArray = new int[] {10,8,9,74,54};
            // 取长度最长的词组 -- 冒泡法
            for (int j = 1; j < myArray.Length; j++)
            {
                for (int i = 0; i < myArray.Length - 1; i++)
                {
                    // 如果 myArray[i] > myArray[i+1] ,则 myArray[i] 上浮一位
                    if (myArray[i] > myArray[i + 1])
                    {
                        int temp = myArray[i];
                        myArray[i] = myArray[i + 1];
                        myArray[i + 1] = temp;
                    }
                   
                //Console.WriteLine();
                }
               Console.Write("{0} ", myArray[j]);
            }
         
        }
    }
}

 
原创粉丝点击