Need help understanding the structure of this insertion method

insertion sort
selection sort
bubble sort
insertion sort java
merge sort
insertion sort c++
insertion sort python

I am working on a homework assignment involving array sorting methods, we are given the methods, and I'm having a little trouble understanding how this insertion sort method functions. More specifically, the role the two variables passed to the method play.

As I understand, the Key variable describes the index of the array that you'd like to place your inserted number in and the item is the number itself. Within main, I simply request the user to enter two numbers and pass them to the method, one for the key and the other for the item. Here is the given code for this segment:

public final void insertion(double Key, double Item)
    if (arraySize == 0)
        arr[0] = Item;
    /* find the position for inserting the given item */
    int position = 0;
    while (position < arraySize &  Key > arr[position])
    for (int i = arraySize; i > position; i--)
        arr[i] = arr[i - 1];
    arr[position] = Item;
    arraySize = arraySize + 1;


However, when I pass doubles to the method as I have explained, I get an error stating that index (array length) is out of bounds for length (array length).

Clearly, I am misunderstanding the purpose or structure of this method and I can't figure it out. Any help would be appreciated. I know this is a very simple problem.

EDIT: Here is how I initialize my array, the given code is in a separate class from my main method:

    public static double[] arr;
    private int arraySize;

    public sortedArrayAccess(int scale)
        arr = new double[scale];
        arraySize = arr.length;

Within my main method:

    System.out.print("Enter an array size: ");
    int d = sc.nextInt();
    sortedArrayAccess test = new sortedArrayAccess(d);
    for(int i=0;i<test.arr.length;i++)
        System.out.print("Enter a number for index " + i + ": ");
        double c = sc.nextDouble();
        test.arr[i] = c;

How do you initialize ‘arr’ variable?

Anyway the problem is that when you create the array - you should specify initial capacity. And every time when you add the element into array you are about to increase it. When you try to ask for for a cell of array indexed i if array capacity is less then I - you’ll be given the arrayOutOfBoundsException.

Insertion Sort, It contains well written, well thought and well explained computer science and Insertion sort is a simple sorting algorithm that works the way we sort playing Let us loop for i = 1 (second element of the array) to 4 (last element of the Learn all the important concepts of Data Structures and Algorithms with the help of the  Doing this makes the array unordered. I do understand that it is the incoming inserts. However, I've written an insertion sort method that definitely works on sorted arrays. Because of this I reduce it to [3, 5, 7, 11, 13]; so that it would be sorted. This has led me to end up failing the challenge exercise.

Insertion Sort Tutorials & Notes | Algorithms, Detailed tutorial on Insertion Sort to improve your understanding of Algorithms. /*storing current element whose left side is checked for its correct position . To first understand this structure, we must first understand a key concept in math, art, and computer science, known as recursion. called insertion. Insertion. The first method of the Binary

if (arraySize == 0)
        arr[0] = Item;

As you know, in computer science, indices starts from 0. Which means arr[0] here is the first slot of your array. if arraySize is 0, there is no such index arr[0]. Your code tries to insert the Item to zero sized array. This causes Index out of bound exception.

By the way, If it is a sorting algorithm for the values, "Key" variable is not required. You can delete it. But if it is required, you should sort the elements by their key values.

For example if we have:

element 1 -> key= 101 , value= 6
element 2 -> key= 201 , value= 9
element 3 -> key= 301 , value= 2

you should not sort them as element 3 < element 1 < element 2 you should sort them like: element 1 < element 2 < element 3 in order to their key values.

In other words, if you want to sort them by their values, passing key values as a parameter is meaningless.

Data Structure and Algorithms Insertion Sort, Data Structure and Algorithms Insertion Sort - This is an in-place comparison-​based sorting algorithm. Here, a sub-list is maintained which is always sorted. For  When you first learn to code, it’s common to learn arrays as the “main data structure.” Eventually, you will learn about hash tables too. If you are pursuing a Computer Science degree, you have to take a class on data structure. You will also learn about linked lists, queues, and stacks. Those data structures are called “linear” data structures because they all have a logical start

6.9. The Insertion Sort, At this point in the algorithm, a sorted sublist of five items consisting of 17, 26, 54, 77, and 93 exists. We want to insert 31 back into the already sorted items. The  9. To understand the flow of the problem. 10. To measure the behavior (or performance) of the methods in all cases (best cases, worst cases, average cases) 11. With the help of an algorithm, we can also identify the resources (memory, input-output) cycles required by the algorithm. 12. With the help of algorithm, we convert art into a science. 13.

Insertion Sort Algorithm, And if at every comparison reveals that the element in question can be inserted at a particular position, then space is created for it by shifting the other elements  The linked list data structure is one of the fundamental data structures in computer science. Think of the linked list data structure as your ABCs. Without learning the ABCs, it is difficult to conceptualize words, which are made up by stringing alphabetical characters together. Therefore, you want to know the ins and outs of the linked list data structures. In this article, we will explore

Insertion sort, Insertion sort is a simple sorting algorithm that builds the final sorted array (or list) one item at a In the worst case for insertion sort (when the input array is reverse​-sorted), insertion If a more sophisticated data structure (e.g., heap or binary tree) is used, the time Help · Community portal · Recent changes · Upload file  So i guess, you need to emit layoutChanged() signal from your model (whenever you change the data in model) in order to update connected views. Also read the QT docs for model view architecture, how it is implemented in QT. see if that helps, if it doesn't i will try to download your code and debug it and see, what's wrong. Good Luck

  • The method signature is indeed strange. It would only work as advertised if Key is always the same value as Item.
  • That is what I thought! I figured I must be wrong though... and I am hesitant to alter the given code as that is outside the scope of my assignment and it is very likely the professor will deduct points for any alterations.
  • Ask your professor about it. The two parameters don't make sense.
  • I will discuss it further with him tomorrow, thanks!
  • I have updated my post with my array initialization code!
  • I changed that portion to spit an error out at array size 0 and to assign item variable to arr[0] if it is only of size 1 and still received the same error. I should never have been tripping that code block though, as my arrays were always greater than 2 elements.