一维数组最后几题(java)

1、在一组排序好的数组中,插入一个数。

int[] a = { 90, 80, 70, 60, 50, 40, 30, 20, 10, 0 };

            int b = 25;

            for (int i = 0; i <= a.length - 2; i++)

            {

                if (b > a[i])

                {

                    for (int j = a.length - 2; j >= i; j--)

                    {

                        a[j + 1] = a[j];

                    }

                    a[i] = b;

                    break;

                }

            }

            for (int i = 0; i < a.length; i++)

            {

                System.out.print(a[i] + " ");

            }

2、对一个乱序的一维数组排序

int[] a = { 1, 3, 5, 2, 4, 6 };

                   int t;

                   for(int j = 1; j <= a.length - 1; j++)

                   {

                            for(int i = a.length - 1; i >= j; i--)

                            {

                                     if(a[i - 1] > a[i])

                                     {

                                               t= a[i - 1];

                                               a[i- 1] = a[i];

                                               a[i]= t;

                                     }

                            }

                   }

                   for(int k = 0; k < a.length; k++)

                   {

                            System.out.print(a[k]+ " ");

                   }

3、求一组数中的最大值和第二大值

int[] a= {1,4,7,2,5,8,3,6,9};

                   intmax1=0,max2=0;

                   for(inti=0;i

                   {

                            if(a[i]>max1)

                            {

                                     max2=max1;

                                     max1=a[i];

                            }

                            elseif(a[i]>max2)

                            {

                                     max2=a[i];

                            }

                   }

                   System.out.println(max1);

                   System.out.println(max2);

4、求一组数中的前n个最大值

int[] a= {1,4,7,2,5,8,3,6,9},b;

                   intc;

                   Scannerscan=new Scanner(System.in);

                   System.out.println("想对前几个数排序?");

                   c=scan.nextInt();

                   scan.close();

                   b=newint[c];

                   for(inti=0;i

                   {

                            for(intj=0;j

                            {

                                     if(a[i]>b[j])

                                     {

                                               for(intk=b.length-2;k>=j;k--)

                                               {

                                                        b[k+1]=b[k];

                                               }

                                               b[j]=a[i];

                                               break;

                                     }

                            }

                   }

                   System.out.println("数组中最大的前"+c+"个是:");

                   for(int k = 0; k < b.length; k++)

                   {

                            System.out.print(b[k]+ " ");

                   }

你可能感兴趣的:(一维数组最后几题(java))