- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
<分区>
代码应该这样做:a) 给定一个未排序的整数数组,您的任务是通过应用以下算法对数组进行排序(假设输入不包含重复项):从数组中的第一个元素开始执行以下步骤:– 计算较小元素的数量以找到正确的位置 i。– 如果元素在其正确位置,则移动到后续元素。– 否则,将当前元素与位置 i 中找到的元素交换。– 重复前面的步骤,直到到达最后一个元素。
例子:5 7 3 6 9检查 a[0],有一个元素比它小,所以它应该与元素交换位置1。7 5 3 6 9检查新元素 a[0]。它应该移动到位置 3。6 5 3 7 9检查新元素 a[0]。它应该移动到位置 2。3 5 6 7 9检查新元素 a[0]。它位于正确的位置,因此我们移动到后续元素 a[1]。
public class Assignment1_T11_25_2729_Sara_Aly {
private int[] a;
private int max;
private int n;
int position=0;
public Assignment1_T11_25_2729_Sara_Aly (int max){
a= new int[max];
}
public void insert(int x){
a[n]=x;
n++;
}
public void sort(){
int out=0, smaller=0;
while(out<n){
for(int in=out+1;in<n;n++){
if(a[in]<a[out])
smaller++;
}
if (smaller==0){
out++;
}
else {
swap(a[out], a[smaller]);
}
}
}
private void swap (int one, int two){
int temp=a[one];
a[one]=a[two];
a[two]=temp;
}
public void display(){
for (int i=0;i<n;i++){
System.out.print(a[i]+ " ");
}
System.out.println("");
}
public static void main(String[]args){
int maxsize=5;
Assignment1_T11_25_2729_Sara_Aly trial;
trial= new Assignment1_T11_25_2729_Sara_Aly(maxsize);
trial.insert(5);
trial.insert(7);
trial.insert(3);
trial.insert(6);
trial.insert(9);
trial.display();
trial.sort();
trial.display();
}
}
Tried a few algorithims to get it to work but for some reason it won't sort any suggestions??
也尝试了这种排序方法,但没有成功。
public void sort(){
boolean finished = false;
int position =0;
while (position<max){
if (finished==true){
position++;
finished =false;
}
else {
int smaller=0;
for (int j = position+1; j<max; j++){
int temp=a[position];
if (a[j] <a[position]){
smaller++;
}
}
if (smaller==0){
finished= true;
}
else {
int temp= a[smaller];
a[smaller]=a[position];
a[position]=temp;
}
}
}
}
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!