我正在编写一个使用数组并使用shell排序的程序。我要抓住的是,我试图展示从开始到结束之间的每个步骤。因此,当排序发生时,我试图显示每个通过以及是否应该交换值。这就是我正在做的事情,但我意识到这是令人费解的方法,无论如何都不会起作用。但这给了我思考过程的思路。我没有包含shell排序代码或任何其他部分,因为我不需要这些帮助。
v1 <- v[which(v == 25) + (-2:2)]
embed(v1, 3)[, 3:1]
# [,1] [,2] [,3]
#[1,] 24.0 24.5 25.0
#[2,] 24.5 25.0 25.5
#[3,] 25.0 25.5 26.0
所以我需要帮助的地方是写这篇文章;我需要一种方法来实现所有可用于每次通过的if语句,然后在每次通过之后实现新的数组...我想我需要编写一个循环,该循环将在通过数组的过程中增加值。我也打算实现这一点来实现交换的值。我实现了5,因为对于此特定的通过,这是交换发生的地方。
int array[] = {29, 38, 85, 37, 49, 69, 33, 55, 88, 71, 52, 41};
//sorting pass 1
int pos;
pos = gap;
System.out.println("Insert Value: "+ array[pos]);
if (array[pos]>array[0]){
System.out.println(" COMPARE: [" +pos+ "]" +array[pos]+ " < [0] ["+array[0]+ "] ? No");
System.out.println(" Already inserted in correct place");
}else{
System.out.println(" COMPARE: [" +pos+ "]" +array[pos]+ " < [0] ["+array[0]+ "] ? Yes - Move " +
array[pos]+ " to index [" +pos+ "]");
System.out.println("INSERT " +array[pos]+ " at index [0]");
}
System.out.println("Insert Value: "+ array[pos+1]);
if (array[pos+1]>array[1]){
System.out.println(" COMPARE: [" +(pos+1)+ "]" +array[pos+1]+ " < [1] ["+array[1]+ "] ? No");
System.out.println(" Already inserted in correct place");
}else{
System.out.println(" COMPARE: [" +(pos+1)+ "]" +array[pos+1]+ " < [1] ["+array[1]+ "] ? Yes - Move " +
array[1]+ " to index [" +(pos+1)+ "]");
System.out.println("INSERT " +array[pos+1]+ " at index [1]");
}
System.out.println("Insert Value: "+ array[pos+2]);
if (array[pos+2]>array[2]){
System.out.println(" COMPARE: [" +(pos+2)+ "]" +array[pos+2]+ " < [2] ["+array[2]+ "] ? No");
System.out.println(" Already inserted in correct place");
}else{
System.out.println(" COMPARE: [" +(pos+2)+ "]" +array[pos+2]+ " < [2] ["+array[2]+ "] ? Yes - Move " +
array[2]+ " to index [" +(pos+2)+ "]");
System.out.println("INSERT " +array[pos+2]+ " at index [2]");
}
System.out.println("Insert Value: "+ array[pos+3]);
if (array[pos+3]>array[3]){
System.out.println(" COMPARE: [" +(pos+3)+ "]" +array[pos+3]+ " < [3] ["+array[3]+ "] ? No");
System.out.println(" Already inserted in correct place");
}else{
System.out.println(" COMPARE: [" +(pos+3)+ "]" +array[pos+3]+ " < [3] ["+array[3]+ "] ? Yes - Move " +
array[3]+ " to index [" +(pos+3)+ "]");
System.out.println("INSERT " +array[pos+3]+ " at index [3]");
}
System.out.println("Insert Value: "+ array[pos+4]);
if (array[pos+4]>array[4]){
System.out.println(" COMPARE: [" +(pos+4)+ "]" +array[pos+4]+ " < [4] ["+array[4]+ "] ? No");
System.out.println(" Already inserted in correct place");
}else{
System.out.println(" COMPARE: [" +(pos+4)+ "]" +array[pos+4]+ " < [4] ["+array[4]+ "] ? Yes - Move " +
array[4]+ " to index [" +(pos+4)+ "]");
System.out.println("INSERT " +array[pos+4]+ " at index [4]");
}
System.out.println("Insert Value: "+ array[pos+5]);
if (array[pos+5]>array[5]){
System.out.println(" COMPARE: [" +(pos+5)+ "]" +array[pos+5]+ " < [5] ["+array[5]+ "] ? No");
System.out.println(" Already inserted in correct place");
}else{
System.out.println(" COMPARE: [" +(pos+5)+ "]" +array[pos+5]+ " < [5] ["+array[5]+ "] ? Yes - Move " +
array[5]+ " to index [" +(pos+5)+ "]");
System.out.println("INSERT " +array[pos+5]+ " at index [5]");
}
System.out.println("Insert Value: "+ array[pos+6]);
if (array[pos+6]>array[6]){
System.out.println(" COMPARE: [" +(pos+6)+ "]" +array[pos+6]+ " < [6] ["+array[6]+ "] ? No");
System.out.println(" Already inserted in correct place");
}else{
System.out.println(" COMPARE: [" +(pos+6)+ "]" +array[pos+6]+ " < [6] ["+array[6]+ "] ? Yes - Move " +
array[6]+ " to index [" +(pos+6)+ "]");
System.out.println("INSERT " +array[pos+6]+ " at index [6]");
}
System.out.println("Insert Value: "+ array[pos+7]);
if (array[pos+7]>array[7]){
System.out.println(" COMPARE: [" +(pos+7)+ "]" +array[pos+7]+ " < [7] ["+array[7]+ "] ? No");
System.out.println(" Already inserted in correct place");
}else{
System.out.println(" COMPARE: [" +(pos+7)+ "]" +array[pos+5]+ " < [7] ["+array[7]+ "] ? Yes - Move " +
array[7]+ " to index [" +(pos+7)+ "]");
System.out.println("INSERT " +array[pos+7]+ " at index [7]");
}
System.out.println("Insert Value: "+ array[pos+8]);
if (array[pos+8]>array[8]){
System.out.println(" COMPARE: [" +(pos+8)+ "]" +array[pos+8]+ " < [8] ["+array[8]+ "] ? No");
System.out.println(" Already inserted in correct place");
}else{
System.out.println(" COMPARE: [" +(pos+8)+ "]" +array[pos+8]+ " < [8] ["+array[8]+ "] ? Yes - Move " +
array[8]+ " to index [" +(pos+8)+ "]");
System.out.println("INSERT " +array[pos+8]+ " at index [8]");
}
System.out.println("Insert Value: "+ array[pos+9]);
if (array[pos+9]>array[9]){
System.out.println(" COMPARE: [" +(pos+9)+ "]" +array[pos+9]+ " < [9] ["+array[9]+ "] ? No");
System.out.println(" Already inserted in correct place");
}else{
System.out.println(" COMPARE: [" +(pos+9)+ "]" +array[pos+9]+ " < [9] ["+array[9]+ "] ? Yes - Move " +
array[9]+ " to index [" +(pos+9)+ "]");
System.out.println("INSERT " +array[pos+5]+ " at index [9]");
}
System.out.println("Insert Value: "+ array[pos+5]);
if (array[pos+10]>array[10]){
System.out.println(" COMPARE: [" +(pos+10)+ "]" +array[pos+10]+ " < [10] ["+array[10]+ "] ? No");
System.out.println(" Already inserted in correct place");
}else{
System.out.println(" COMPARE: [" +(pos+10)+ "]" +array[pos+10]+ " < [10] ["+array[10]+ "] ? Yes - Move " +
array[10]+ " to index [" +(pos+10)+ "]");
System.out.println("INSERT " +array[pos+10]+ " at index [10]");
}
System.out.println("Insert Value: "+ array[pos+11]);
if (array[pos+11]>array[11]){
System.out.println(" COMPARE: [" +(pos+11)+ "]" +array[pos+11]+ " < [11] ["+array[11]+ "] ? No");
System.out.println(" Already inserted in correct place");
}else{
System.out.println(" COMPARE: [" +(pos+11)+ "]" +array[pos+11]+ " < [11] ["+array[11]+ "] ? Yes - Move " +
array[10]+ " to index [" +(pos+11)+ "]");
System.out.println("INSERT " +array[pos+11]+ " at index [11]");
}
System.out.println("Insert Value: "+ array[pos+5]);
if (array[pos+12]>array[12]){
System.out.println(" COMPARE: [" +(pos+12)+ "]" +array[pos+12]+ " < [12] ["+array[12]+ "] ? No");
System.out.println(" Already inserted in correct place");
}else{
System.out.println(" COMPARE: [" +(pos+12)+ "]" +array[pos+12]+ " < [12] ["+array[12]+ "] ? Yes - Move " +
array[12]+ " to index [" +(pos+12)+ "]");
System.out.println("INSERT " +array[pos+12]+ " at index [12]");
很抱歉,这可能到处都是,我脑海中可以看到需要做些什么,只是我还没有真正实现它。我将竭尽所能!