Java 如何为我的数组创建insertinoder方法

Java 如何为我的数组创建insertinoder方法,java,arrays,sorting,data-structures,Java,Arrays,Sorting,Data Structures,我是一个相当新的程序员,希望创建一个以空数组开始的方法,并允许我调用它,以便按升序向该数组添加值 例如: public class InOrder { int[] arry = new int[20]; int target = -1; int elements = 0; public static void main(String[] args) { InOrder i = new InOrder(); i.insertInOrder(6); i.insertIn

我是一个相当新的程序员,希望创建一个以空数组开始的方法,并允许我调用它,以便按升序向该数组添加值

例如:

public class InOrder 
{
int[] arry = new int[20];
int target = -1;
int elements = 0;

public static void main(String[] args) 
{
    InOrder i = new InOrder();
    i.insertInOrder(6);
    i.insertInOrder(7);
    i.insertInOrder(12);
    i.insertInOrder(17);
    i.insertInOrder(19);
    i.insertInOrder(28);


    for(int k = 0; k < 20; k++)
    {
        System.out.println(i.arry[k]);
    }
}

public void insertInOrder(int n) 
{

    if (elements == 0) 
    {
        arry[0] = n;
        elements++;
    }

    else 
    {
        for (int i = 0; i < elements; i++) 
        {
            if (n > arry[i]) 
            {
                target = i;
            }
        }

        if (target == -1) 
        {
            target = 0;
        }

        if (n > arry[target]) 
        {
            for (int x = target; x < elements; x++) 
            {
                if(x + 1 == elements)
                {
                    arry[x + 1] = n;
                    elements++;
                    break;
                }
            }
        }
    }
}
插入诺德(5)

插入诺德(3)

插入诺德(7)

插入诺德(9)

insertinoder(12)

应返回带有值的数组:

public class InOrder 
{
int[] arry = new int[20];
int target = -1;
int elements = 0;

public static void main(String[] args) 
{
    InOrder i = new InOrder();
    i.insertInOrder(6);
    i.insertInOrder(7);
    i.insertInOrder(12);
    i.insertInOrder(17);
    i.insertInOrder(19);
    i.insertInOrder(28);


    for(int k = 0; k < 20; k++)
    {
        System.out.println(i.arry[k]);
    }
}

public void insertInOrder(int n) 
{

    if (elements == 0) 
    {
        arry[0] = n;
        elements++;
    }

    else 
    {
        for (int i = 0; i < elements; i++) 
        {
            if (n > arry[i]) 
            {
                target = i;
            }
        }

        if (target == -1) 
        {
            target = 0;
        }

        if (n > arry[target]) 
        {
            for (int x = target; x < elements; x++) 
            {
                if(x + 1 == elements)
                {
                    arry[x + 1] = n;
                    elements++;
                    break;
                }
            }
        }
    }
}
0:3

1:5

2:7

3:9

4:12

如果有任何关于如何在不使用java预构建方法(如“Array.sort”)的情况下实现这一点的提示,我将不胜感激。谢谢大家!

下面是我对这段代码的尝试;然而,我所能实现的只是在数组的末尾添加一个值(如果它是最大的数字)

例如:

public class InOrder 
{
int[] arry = new int[20];
int target = -1;
int elements = 0;

public static void main(String[] args) 
{
    InOrder i = new InOrder();
    i.insertInOrder(6);
    i.insertInOrder(7);
    i.insertInOrder(12);
    i.insertInOrder(17);
    i.insertInOrder(19);
    i.insertInOrder(28);


    for(int k = 0; k < 20; k++)
    {
        System.out.println(i.arry[k]);
    }
}

public void insertInOrder(int n) 
{

    if (elements == 0) 
    {
        arry[0] = n;
        elements++;
    }

    else 
    {
        for (int i = 0; i < elements; i++) 
        {
            if (n > arry[i]) 
            {
                target = i;
            }
        }

        if (target == -1) 
        {
            target = 0;
        }

        if (n > arry[target]) 
        {
            for (int x = target; x < elements; x++) 
            {
                if(x + 1 == elements)
                {
                    arry[x + 1] = n;
                    elements++;
                    break;
                }
            }
        }
    }
}
插入诺德(1)

插入诺德(4)

插入诺德(9)

Insertinoder(17)

插入诺德(26)

会工作,但此代码不会:

public class InOrder 
{
int[] arry = new int[20];
int target = -1;
int elements = 0;

public static void main(String[] args) 
{
    InOrder i = new InOrder();
    i.insertInOrder(6);
    i.insertInOrder(7);
    i.insertInOrder(12);
    i.insertInOrder(17);
    i.insertInOrder(19);
    i.insertInOrder(28);


    for(int k = 0; k < 20; k++)
    {
        System.out.println(i.arry[k]);
    }
}

public void insertInOrder(int n) 
{

    if (elements == 0) 
    {
        arry[0] = n;
        elements++;
    }

    else 
    {
        for (int i = 0; i < elements; i++) 
        {
            if (n > arry[i]) 
            {
                target = i;
            }
        }

        if (target == -1) 
        {
            target = 0;
        }

        if (n > arry[target]) 
        {
            for (int x = target; x < elements; x++) 
            {
                if(x + 1 == elements)
                {
                    arry[x + 1] = n;
                    elements++;
                    break;
                }
            }
        }
    }
}
插入诺德(2)

插入诺德(4)

插入诺德(1)

插入诺德(3)

insertinoder(19)

代码:

public class InOrder 
{
int[] arry = new int[20];
int target = -1;
int elements = 0;

public static void main(String[] args) 
{
    InOrder i = new InOrder();
    i.insertInOrder(6);
    i.insertInOrder(7);
    i.insertInOrder(12);
    i.insertInOrder(17);
    i.insertInOrder(19);
    i.insertInOrder(28);


    for(int k = 0; k < 20; k++)
    {
        System.out.println(i.arry[k]);
    }
}

public void insertInOrder(int n) 
{

    if (elements == 0) 
    {
        arry[0] = n;
        elements++;
    }

    else 
    {
        for (int i = 0; i < elements; i++) 
        {
            if (n > arry[i]) 
            {
                target = i;
            }
        }

        if (target == -1) 
        {
            target = 0;
        }

        if (n > arry[target]) 
        {
            for (int x = target; x < elements; x++) 
            {
                if(x + 1 == elements)
                {
                    arry[x + 1] = n;
                    elements++;
                    break;
                }
            }
        }
    }
}
公共类顺序
{
int[]arry=新int[20];
int target=-1;
int元素=0;
公共静态void main(字符串[]args)
{
索引i=新的索引();
i、 插入诺德(6);
i、 插入诺德(7);
i、 insertinoder(12);
i、 Insertinoder(17);
i、 insertinoder(19);
i、 Insertinoder(28);
对于(int k=0;k<20;k++)
{
System.out.println(i.arry[k]);
}
}
公共无效插入器(int n)
{
if(元素==0)
{
arry[0]=n;
元素++;
}
其他的
{
for(int i=0;iarry[i])
{
目标=i;
}
}
如果(目标==-1)
{
目标=0;
}
如果(n>到达[目标])
{
for(int x=target;x
您可以使用此代码。您可以更改参数类型

public void insert(int x) {
    // loop through all elements
    for (int i = 0; i < size(); i++) {
        // if the element you are looking at is smaller than x, 
        // go to the next element
        if (get(i) < x) continue;
        // if the element equals x, return, because we don't add duplicates
        if (get(i) == x) return;
        // otherwise, we have found the location to add x
        add(i, x);
        return;
    }
    // we looked through all of the elements, and they were all
    // smaller than x, so we add ax to the end of the list
    add(x);
}
public void insert(int x){
//循环遍历所有元素
对于(int i=0;i
您可以使用此代码。您可以更改参数类型

public void insert(int x) {
    // loop through all elements
    for (int i = 0; i < size(); i++) {
        // if the element you are looking at is smaller than x, 
        // go to the next element
        if (get(i) < x) continue;
        // if the element equals x, return, because we don't add duplicates
        if (get(i) == x) return;
        // otherwise, we have found the location to add x
        add(i, x);
        return;
    }
    // we looked through all of the elements, and they were all
    // smaller than x, so we add ax to the end of the list
    add(x);
}
public void insert(int x){
//循环遍历所有元素
对于(int i=0;i
我认为,如果您希望具有良好的插入复杂性,同时又希望以排序的顺序存储元素,则需要更复杂的数据结构。类似于a的数据结构将起作用,也可以作为更简单的选项使用


如果您不关心复杂性,只需在每次插入操作时对数组进行排序。

我认为,如果您希望具有良好的插入复杂性,并以排序顺序存储元素,则需要更复杂的数据结构。a like a将起作用,而且您还可以作为更简单的选项使用


如果您不关心复杂性,只需在每次插入操作中对数组进行排序。

如果您总是插入排序,换句话说,如果数组始终保持排序,则可以使用有效的二进制搜索

import java.util.Arrays;

public class InOrder
{
  int[] array = new int[20];
  int target = -1;
  int elements = 0;

  public static void main(String[] args) 
  {
      InOrder i = new InOrder();
      i.insertInOrder(6);
      i.insertInOrder(17);
      i.insertInOrder(28);
      i.insertInOrder(19);
      i.insertInOrder(7);
      i.insertInOrder(12);

      for(int k = 0; k <i.elements; k++)
      {
          System.out.println(i.array[k]);
      }
  }

  public void insertInOrder(int n) 
  {
    if(elements==array.length) array=Arrays.copyOf(array, elements*2);
    int pos = Arrays.binarySearch(array, 0, elements, n);
    if(pos<0) pos=~pos;
    if(pos<elements) System.arraycopy(array, pos, array, pos+1, elements-pos);
    array[pos]=n;
    elements++;
  }
}
导入java.util.array;
公共类有序
{
int[]数组=新的int[20];
int target=-1;
int元素=0;
公共静态void main(字符串[]args)
{
索引i=新的索引();
i、 插入诺德(6);
i、 Insertinoder(17);
i、 Insertinoder(28);
i、 insertinoder(19);
i、 插入诺德(7);
i、 insertinoder(12);

对于(int k=0;k,如果总是插入排序,换句话说,如果数组始终保持排序,则可以使用有效的二进制搜索

import java.util.Arrays;

public class InOrder
{
  int[] array = new int[20];
  int target = -1;
  int elements = 0;

  public static void main(String[] args) 
  {
      InOrder i = new InOrder();
      i.insertInOrder(6);
      i.insertInOrder(17);
      i.insertInOrder(28);
      i.insertInOrder(19);
      i.insertInOrder(7);
      i.insertInOrder(12);

      for(int k = 0; k <i.elements; k++)
      {
          System.out.println(i.array[k]);
      }
  }

  public void insertInOrder(int n) 
  {
    if(elements==array.length) array=Arrays.copyOf(array, elements*2);
    int pos = Arrays.binarySearch(array, 0, elements, n);
    if(pos<0) pos=~pos;
    if(pos<elements) System.arraycopy(array, pos, array, pos+1, elements-pos);
    array[pos]=n;
    elements++;
  }
}
导入java.util.array;
公共类有序
{
int[]数组=新的int[20];
int target=-1;
int元素=0;
公共静态void main(字符串[]args)
{
索引i=新的索引();
i、 插入诺德(6);
i、 Insertinoder(17);
i、 Insertinoder(28);
i、 insertinoder(19);
i、 插入诺德(7);
i、 insertinoder(12);

for(int k=0;k您想做的事情是作为的一部分来完成的

高级描述:

Let the current position point to the last element
While the element to insert is smaller than the element at the current position
  Move the element at the current position right one
  Decrease the current position
Insert the element at the current position
伪代码:

holePos ← length(A)
while holePos > 0 and valueToInsert < A[holePos - 1]
{ //value to insert doesn't belong where the hole currently is, so shift 
    A[holePos] ← A[holePos - 1] //shift the larger value up
    holePos ← holePos - 1       //move the hole position down
}
A[holePos] ← valueToInsert
holePos← 长度(A)
而holePos>0且valueToInsert
转换为Java代码应该很容易


但是,是的,BST会更有效率。

你想做的事情是作为一部分来完成的

高级描述:

Let the current position point to the last element
While the element to insert is smaller than the element at the current position
  Move the element at the current position right one
  Decrease the current position
Insert the element at the current position
伪代码:

holePos ← length(A)
while holePos > 0 and valueToInsert < A[holePos - 1]
{ //value to insert doesn't belong where the hole currently is, so shift 
    A[holePos] ← A[holePos - 1] //shift the larger value up
    holePos ← holePos - 1       //move the hole position down
}
A[holePos] ← valueToInsert
holePos← 长度(A)
而holePos>0且valueToInsert