输入10个数 分别用选择法和气泡法按照从小到大的顺序进行排序后输出

解决方案 »

  1.   

    楼上几位不要说得这么难听呀,每个学编程的最初都差不多这样,这是我曾经学习编程时总结的三种排序法
    http://user.qzone.qq.com/823247117/blog/1276367454
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            public void ZhuZhu(int[] list)//选择排序。
            {
                int temp;//交换数组元素。
                for (int i = 0; i < list.Length - 1; i++)
                {
                    int k = i; //存放数组元素下标位置
                    for (int j = i + 1; j < list.Length; j++)
                    {
                        if (list[k] > list[j])
                            k = j;//找小值。k值会不停的变化,直到最后把最小值找到。
                    }
                    temp = list[k];
                    list[k] = list[i];
                    list[i] = temp;//把找到的最小值与基值交换。
                }
                for (int i = 0; i < list.Length; i++)
                {
                    Console.Write("{0}\t", list[i]);
                }
            }
            public void ZhuZhu_1(int []list)//冒泡排序。
            {
                int temp_1;
                for (int i = 0; i < list.Length; i++)
                {
                    for (int j = 0; j < list.Length-1-i; j++)//这里有个减i,是因为每比较i次,后面就有i个数已经变成有序数列了。所以这内个数我们不做比较。提高运行速度
                    {
                        if (list[j] > list[j + 1])
                        {
                            temp_1 = list[j];
                            list[j] = list[j + 1];
                            list[j + 1] = temp_1 ;
                        }
                    }
                }
                for (int i = 0; i < list.Length; i++)
                {
                    Console.Write("{0}\t",list[i]);
                }
            }
            public void ZhuZhu_2(int[] list)//插入排序
            {
                for (int i = 1; i<list .Length ;i++)
                {
                    int temp_2=list [i];//存放要比较的数,这步必须要,且不能直接用list[i].
                    int j;
                    for (j = i; j >0; j--)
                    {
                        if (list[j - 1] > temp_2)//每次都要tmp_2做比较.
                            list[j] = list[j - 1];//交换位置,说白了就是给tmp_2让出空间。
                        else
                            break;
                    }
                    list[j] = temp_2;
                   
                }
                for (int i = 0; i < list.Length; i++)
                {
                    Console.Write("{0}\t", list[i]);
                }
            }        static void Main(string[] args)
            {
                int[] list = new int[10];
                Random ra = new Random();
                Program P = new Program();
                for (int i = 0; i < list.Length; i++)
                {
                    list[i] = ra.Next(10, 100);//包涵10,但不包涵100.
                    Console.Write("{0}\t", list[i]);
                }
                P.ZhuZhu(list);
                P.ZhuZhu_1(list);
                P.ZhuZhu_2(list);
            }
        }
    }
      

  3.   

    static void Main(string[] args)
            {
                while (true)
                {
                    int[] num = new int[10];
                    int i;
                    for (i = 0; i < 10; i++)
                    {
                        Console.WriteLine("第{0}个数", (i + 1));
                        num[i] = int.Parse(Console.ReadLine());
                    }
                    Sort(num);
                    foreach (int x in num)
                    {
                        Console.Write(x+" ");
                    }
                }
            }
    //冒泡法
            public static 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++;
                }
            }