基本排序算法(冒泡排序,选择排序,插入排序)

using System;
using System.Diagnostics;

namespace TestCArray
{
    class Program
    {
        static void Main(string[] args)
        {
            CArray numbs = new CArray(10);
           
            Random rnd = new Random(10);

            for (int i = 0; i <= 9; i++)
            {
                numbs.Insert(rnd.Next(0,10));
            }

            Console.WriteLine("Before sorting:");
            numbs.DisplayElements();

            Console.WriteLine("During sorting:");
            //numbs.BubbleSort();
            //numbs.SelectionSort();
            numbs.InsertionSort();

            Console.WriteLine("After sorting:");
            numbs.DisplayElements();
        }
    }


    public class CArray
    {
        private int[] arr;
        private int upper;
        private int numElements;

        public CArray(int size)
        {
            arr = new int[size];
            upper = size - 1;
            numElements = 0;
        }

        public void Insert(int item)
        {
            arr[numElements] = item;
            numElements++;
        }

        public void DisplayElements()
        {
            Console.Write("---->");
            for (int i = 0; i <= upper; i++)
            {
                Console.Write(arr[i] + " ");
            }
            Console.WriteLine();
        }

        public void Clear()
        {
            for (int i = 0; i <= upper; i++)
            {
                arr[i] = 0;
            }

            numElements = 0;
        }

        // 冒泡排序
        public void BubbleSort()
        {
            int temp;

            for (int outer = 0; outer <= upper; outer++)
            {
                for (int inner = 0; inner < upper-outer; inner++)
                {
                    if (arr[inner+1]<arr[inner])
                    {
                        temp = arr[inner + 1];
                        arr[inner + 1] = arr[inner];
                        arr[inner] = temp;
                    }
                }
                this.DisplayElements();
            }      
        }

        // 选择排序
        public void SelectionSort()
        {
            int temp;
            for (int outer = 0; outer < upper; outer++)
            {
                for (int inner = outer+1; inner <= upper; inner++)
                {
                    if (arr[outer] > arr[inner])
                    {
                        temp = arr[outer];
                        arr[outer] = arr[inner];
                        arr[inner] = temp;
                    }
                }
                this.DisplayElements();
            }
        }

        // 插入排序
        public void InsertionSort()
        {
            int inner, temp;
            for (int outer = 1; outer <= upper; outer++)
            {
                temp = arr[outer];
                inner = outer;

                while (inner > 0 && arr[inner-1] >= temp)
                {
                    arr[inner] = arr[inner - 1];
                    inner -= 1;
                }

                arr[inner] = temp;

                this.DisplayElements();
            }
        }
    }

    
}

转载于:https://www.cnblogs.com/wjchang/archive/2013/04/24/3671524.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值