Question

In: Computer Science

using java Which sorting algorithm is this, justify in detail your answer? image of code in...

using java

Which sorting algorithm is this, justify in detail your answer?

image of code in this link

https://lh3.googleusercontent.com/ie0xsSduZTDJ0PYd3skj43dNApdae1rKAXt1OIdkUE9WAyIrrzLndH2pbsVs_laCJ91H61wxBRopgHPcF8PayLjWHiW-Pn72ti02xxb0aNpx87tFUbT7j8lW0bL4=w655

Solutions

Expert Solution

The algorithm that is used in the attached picture is Insertion Sort and It is implemented as it will sort the data in decreasing order.

Insertion sort is a simple technique to sort the array, this algorithm splits the array (unsorted) into two parts sorted part and unsorted part and after each pass, the length of the sorted part array gets increased by 1 and the unsorted part gets decreased by 1 at the same time so after the n-1 pass, we get all the array sorted.

More specifically, for an ith pass ( for sorting in descending order as in the pic) the algorithm checks for an element that has the index lesser than (i-1) and value less than the value at ith index element (key). If such an element is found then it stores that element at the next index of that element (space is created ) and continues checking until the condition gets false., when all such elements get checked then this key(i.e, the element at the ith position) gets placed at that empty space created, in this way all the smaller elements than key element gets the index greater than key element. So basically in each pass it picks a key element and inserts it at such index so that all the elements have index lesser than it has the value greater. That's why it is called Insertion sort, it inserts the key element in each pass.

For example:

Let's assume an array is to be sorted in descending order with this algorithm is

Arr 2 7 1 9 5 6

Let's see how this gets sorted by the same algorithm, there are 6 numbers so there will be 5(starting with 2nd element as the first element has no index lesser than it) passes, we will see the result of each pass one by one.

In pass 1: key = 7 (1st index element)

so after pass 1:

Arr 7 2 1 9 5 6

Element 2 and 7 got swapped as element 2 has the lesser index than key and its value is also less than key.

In pass 2: key = 1 (2nd index element)

so after pass 2:

Arr 7 2 1 9 5 6

No change as there is no such element that has a lower index with a lower value.

In pass 3: key = 9 ( 3rd index element)

so after pass 3:

Arr 9 7 2 1 5 6

The key element is the largest among all its lower index's element so it gets the first position, and all shifted by 1.

In pass 4: key = 5 (4th index element)

so after pass 4:

Arr 9 7 5 2 1 6

There are two elements 1 and 2 which has a lower index and value than the key element, so both get shifted by 1, and the key element takes the empty space created.

In pass 5: key = 6 (5th index element)

so after pass 5:

Arr 9 7 6 5 2 1

There are three elements 1 and 2 and 5 which have a lower index and value than the key element, so all get shifted by 1, and the key element takes the empty space created.

Now we can see that the array is now sorted in descending order.

NOTE: In case of any query, you can mention in the comment section. HAPPY LEARNING!!


Related Solutions

Which Sorting algorithm are in place algorithm and which are not? Is it advantageous to design...
Which Sorting algorithm are in place algorithm and which are not? Is it advantageous to design in place sorting algorithm? How this in place term is related to the time complexity and space complexity of the program?
Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting...
Develop an algorithm and implement a Preemptive Priority scheduling algorithm using C++ and using bubble sorting .Arrival time, burst time and priority values.The code should also display: (i) Gantt chart and determine the following: (ii) Determine the Turnaround time(TAT), waiting time(WT) of each process (iii) Determine the Average Waiting Time (AWT) and Average Turnaround Time (ATAT) of all processes. please write the comments
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java...
Develop an algorithm for INSERTION SORT. Give the pseudo-code version. Convert your pseudo-code into a Java program.
IN JAVA. Which sorting does in place sorting? Which sort does the minimum swap to order...
IN JAVA. Which sorting does in place sorting? Which sort does the minimum swap to order ascending /desendinf manner?
Question #1 (Java) - Which sorting does in-place sorting? - Which sort does the minimum swap...
Question #1 (Java) - Which sorting does in-place sorting? - Which sort does the minimum swap to order ascending/descending manner? Note: I just need a short answer to the question no code.
What are the advantages of using Laplace transform? Justify your answer
What are the advantages of using Laplace transform? Justify your answer
Give the entire code for Minimum spanning tree using Boruvka's algorithm in java (not copy-pasted from...
Give the entire code for Minimum spanning tree using Boruvka's algorithm in java (not copy-pasted from any other website)
Answer the following. Justify your answer. a) What are the 4 features of perfect competition? Using...
Answer the following. Justify your answer. a) What are the 4 features of perfect competition? Using these features, is the market for table salt perfect competition (or close to perfect competition)? Explain each point. b) What 4 outcomes will a binding minimum wage have, and what 1 outcome has 2 possibilities. c) Describe economies of scope and economies of experience. Use a house builder as an example. d) When is a perfectly competitive firm in the short run? What 3...
In your opinion, which price discrimination is acceptable in the market? Justify your answer.
In your opinion, which price discrimination is acceptable in the market? Justify your answer.
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove,...
Java Searching and Sorting, please I need the Code and the Output. Write a method, remove, that takes three parameters: an array of integers, the length of the array, and an integer, say, removeItem. The method should find and delete the first occurrence of removeItem in the array. If the value does not exist or the array is empty, output an appropriate message. (After deleting an element, the number of elements in the array is reduced by 1.) Assume that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT