如何仅使用这种方式对数组进行排序? [英] How do I sorting an array using this way only ...?

查看:81
本文介绍了如何仅使用这种方式对数组进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello all ^ _ ^

我想用以下方式排序(一维数组)。

方式是:



我们有这个数组:

指数:0 ----- 1 ----- 2 ----- 3 ----- 4 ----- 5 ----- 6

值:10 ---- 5 ----- 6 ----- 11 ---- 2 ----- 5 ----- 1



2-创建一个新数组:

  int  [] Sorting = new  int  [ 7 ] 





3-现在我们想要从源数组中找到最小数字,将它放在新数组Sorting中的索引0中,然后使源数组中的最小数字= 0,像那样:

这是一个循环后的源数组:

索引:0 ----- 1 ----- 2 ----- 3- ---- 4 ----- 5 ----- 6

值:10 ---- 5 ----- 6 ----- 11 ---- 2 ----- 5 ----- 0

这是排序数组:



指数:0 ----- 1 ----- 2 ----- 3 ----- 4 ----- 5 ----- 6

值:1 ---- null ---空 - 空 - N的ull - null - null



4-我们必须循环这一步,使Sorting数组排序,如下所示:



指数:0 ----- 1 ----- 2 ----- 3 ----- 4 ----- 5 ----- 6

值:1 ----- 2 ----- 5 ----- 5 ---- 6 ----- 10 ----- 11

================================================== ====================================

这是我的源代码:





  public   static   void  main( String  [] args)
{

int [] RandomArray = new int [ 8 ];
// 插入
for int i = 0; i< RandomArray.length; i ++)
{

String element = JOptionPane.showInputDialog( 请插入任何数字 +(i + 1));
RandomArray [i] = Integer.parseInt(element);


}

int [] SortedArray = new int [ 8 ];
for int s = 0; s< RandomArray.length; s ++)
{


for int d = 0; d< ; RandomArray.length; d ++)
{



if (s == d || RandomArray [s] == 0)
{


}
else



{


int x;

if (RandomArray [s]< RandomArray [d])

{

// SortedArray [s] = RandomArray [s];
SortedArray [s] = RandomArray [s];
x = s;

} else

{
SortedArray [s] = RandomArray [d];
x = d;
}

RandomArray [x] = 0;


} // 首先结束
} // 结束秒


} // 首先结束

}













请快速帮助我。

解决方案

这是一个选择排序并以这种方式工作(伪代码)

  for  i = 0,a.length-  1  
{
j = index_of_min(a,i)
swap(a,i,j)
}





其中

  • index_of_min(a,i)从索引开始查找 a 子阵列中最小值的索引 i
  • swap(a,i,j)交换商品 i j 数组 A

Hello all ^_^
I want to sort an (one dimension array) using the following way only.
The way is :

We have this array :
Index : 0-----1-----2-----3-----4-----5-----6
Values: 10----5-----6-----11----2-----5-----1

2- Create a new array :

int []Sorting =new int [7] 



3- now we want to find min number from the source array the put it in index 0 in the new array "Sorting" then make the min number in source array = 0 , like that :
this is the source array after one loop :
Index : 0-----1-----2-----3-----4-----5-----6
Values: 10----5-----6-----11----2-----5-----0
this is the sorted array :

Index : 0-----1-----2-----3-----4-----5-----6
Values: 1----null---null--null--null--null--null

4- We must loop this step to make "Sorting"array sorted, like this :

Index : 0-----1-----2-----3-----4-----5-----6
Values: 1-----2-----5-----5----6-----10-----11
======================================================================================
this is my source code :


public static void main(String[] args) 
    {   

int [] RandomArray = new int [8];
    //Inserting
    for(int i=0;i<RandomArray.length;i++)
    {

String element= JOptionPane.showInputDialog("Please insert any number "+ (i+1));
RandomArray [i]=Integer.parseInt(element);


    }

    int []SortedArray=new int [8];
    for(int s=0; s< RandomArray.length;s++)
    {


    for(int d=0;d<RandomArray.length;d++)
    {



            if (s==d || RandomArray[s]==0) 
        {


        }
        else



        {


            int x ;

            if (RandomArray[s]<RandomArray[d])

            {

                //SortedArray[s] = RandomArray[s];
                SortedArray[s] = RandomArray[s];
                x=s;

            }else

            {
                SortedArray[s] =    RandomArray[d];
                x=d;
            }

            RandomArray[x]=0;


        }//end first else
    }//end second for


    }//end first for

}







please help me quickly.

解决方案

That is a selection sort and works this way (pseudocode)

for i=0, a.length-1
{
 j = index_of_min(a, i)
 swap(a, i, j )
}



where

  • index_of_min(a,i) finds the index of the smallest value in the subarray of a starting from index i.
  • swap(a,i,j) swaps the items i, j of the array a.


这篇关于如何仅使用这种方式对数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆