如何仅使用这种方式对数组进行排序? [英] How do I sorting an array using this way only ...?
问题描述
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 ofa
starting from indexi
.swap(a,i,j)
swaps the itemsi
,j
of the arraya
.
这篇关于如何仅使用这种方式对数组进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!