Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please read the following piece of JAVA code public static void InsertionSort(in

ID: 3669727 • Letter: P

Question

Please read the following piece of JAVA code

public static void InsertionSort(int[] num)
{
int j; // the number of items sorted so far
int key; // the item to be inserted
int i;

for (j = 1; j < num.length; j++) // Start with 1 (not 0)
{
key = num[j];
// Smaller values are moving up
for(i = j - 1; (i >= 0) && (num[ i ] < key); i--)   
{
num[i+1] = num[i];
}
num[i+1] = key; // Put the key in its proper location

}

How should one handle the array variable in this Java Code in terms of Def-use pairing? Please explain.


Explanation / Answer

The insertion sort, unlike the other sorts, passes through the array only once.  The insertion sort is commonly compared to organizing a handful of playing cards. You pick up the random cards one at a time. As you pick up each card, you insert it into its correct position in your hand of organized cards.  

The insertion sort splits an array into two sub-arrays. The first sub-array (like the cards in your hand) is always sorted and increases in size as the sort continues. The second sub-array (like the cards to be picked up) is unsorted, contains all the elements yet to be inserted into the first sub-array, and decreases in size as the sort continues.

Let's look at our same example using the insertion sort for descending order.

Array at beginning:

2nd sub-array empty


The insertion sort maintains the two sub-arrays within the same array. At the beginning of the sort, the first element of the first sub-array is considered the "sorted array". With each pass through the loop, the next element in the unsorted second sub-array is placed into its proper position in the first sorted sub-array.

The insertion sort can be very fast and efficient when used with smaller arrays. Unfortunately, it loses this efficiency when dealing with large amounts of data

Insertion sort is used to put elements in either increasing or decreasing order. It's a simple algorithm that is more suited towards smaller inputs of code rather than larger ones. When working with larger heaps of data insertion sort falls short compared to other sorts such as quick sort or merge sort. Below is an example of the time it takes to complete an insertion sort based on the amount of data presented
Execution Time
The execution time relating to this java method is directly relatable to the size of the raw data array. Due to the nature of this method, each element being sorted in run-time is individually compared to those elements previously sorted. This results in a concept which is very efficient for small amounts of data, but can become very slow and inefficient with large amounts of data.
The insertion sort, unlike the other sorts, passes through the array only once. The insertion sort is commonly compared to organizing a handful of playing cards. You pick up the random cards one at a time. As you pick up each card, you insert it into its correct position in your hand of organized cards.

The insertion sort splits an array into two sub-arrays. The first sub-array (like the cards in your hand) is always sorted and increases in size as the sort continues. The second sub-array (like the cards to be picked up) is unsorted, contains all the elements yet to be inserted into the first sub-array, and decreases in size as the sort continues.

The insertion sort, unlike the other sorts, passes through the array only once.  The insertion sort is commonly compared to organizing a handful of playing cards. You pick up the random cards one at a time. As you pick up each card, you insert it into its correct position in your hand of organized cards.  

The insertion sort splits an array into two sub-arrays. The first sub-array (like the cards in your hand) is always sorted and increases in size as the sort continues. The second sub-array (like the cards to be picked up) is unsorted, contains all the elements yet to be inserted into the first sub-array, and decreases in size as the sort continues.

Let's look at our same example using the insertion sort for descending order.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote