求最大值、索引、排序总结

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

namespace ljun_CSharp_Study
{
    class MaxIndexPaixu
    {
        ///


        /// 求数组中的最大值和最大值的索引
        ///

        /// 传入的数组
        /// 要求的最大值的索引
        /// 数组中的最大值
        static int MaxIndex(int[] m_Array,out int m_Index)
        {
            m_Index = 0;
            int m_Max=m_Array[0];

            for (int i = 0; i < m_Array.Length; i++)
            {
                if (m_Array[i] > m_Max)
                {
                    m_Max = m_Array[i];
                    m_Index = i;
                }
            }

            return m_Max;
        }

        static void Main(string[] args)
        {
            int[] myArray = new int[7] { 5, 12, 6, 3, 89, 35, 56 };
            int maxIndex;
            int a = 0;

            Console.WriteLine("数组排序前为:");
            for (int i = 0; i < 7; i++)
            {
                Console.WriteLine("myArray[{0}]为:{1}", i, myArray[i]);
            }

            Console.WriteLine("数组中的最大值为:{0}", MaxIndex(myArray, out maxIndex));
            Console.WriteLine("数组中的最大值是第{0}号元素", maxIndex + 1);


            //用冒泡法对数组进行排序
            for (int i = 1; i < 7; i++)
            {
                for (int j = 0; j < 7 - i; j++)
                {
                    if (myArray[j] > myArray[j + 1])
                    {                       
                        a = myArray[j];
                        myArray[j] = myArray[j + 1];
                        myArray[j + 1] = a;
                    }
                }
            }

            Console.WriteLine("数组排序后为:");
            for (int i = 0; i < 7; i++)
            {
                Console.WriteLine("myArray[{0}]为:{1}", i, myArray[i]);
            }

         
            Console.ReadLine();
        }
    }
}
 

你可能感兴趣的:(C#)