You are on page 1of 5

7/10/2021 Sort The Given Array Using Insertion Sort. Show Th... | Chegg.

com

  Textbook Solutions Expert Q&A Study Pack Practice

Find solutions for your homework Search

home / study / engineering / computer science / computer science questions and answers / sort the given array using insertion sort. sho

Question: Sort the given array using insertion sort. Show the steps of

Sort the given array using insertion sort. Show the steps of
your simulation.

4 76 172   -1   87   -93  


666

The Code: class InsertionSort { /*Function to sort array using


insertion sort*/ void sort(int arr[]) { int n =
arr.length; for
(int i = 1; i < n; ++i) { int key = arr[i]; int j = i - 1; /*
Move elements of arr[0..i-1], that are greater
than key, to one
position ahead of their current position */ while (j >= 0
&& arr[j] > key) { arr[j + 1] = arr[j]; j
= j - 1; }
arr[j + 1] = key; } }

Expert Answer

Anonymous answered this


Was this answer helpful? 0
882 answers

Solution

Assume 4 already sorted

---

FIrst
Iteration

key=76

Now compare 76>4

no swapping takes place

---

Second
Iteration

key=172

76<172

https://www.chegg.com/homework-help/questions-and-answers/sort-given-array-using-insertion-sort-show-steps-simulation-4-76-172-1-87-93-666-cod… 1/5
7/10/2021 Sort The Given Array Using Insertion Sort. Show Th... | Chegg.com

  Textbook Solutions Expert Q&A Study Pack Practice


no swapping takes
place

---

Third
Iteration

key=-1

-1<172

swaps 172,-1

still

-1<76

seaps 76,-1

-1<4

swaps -1,4

---

Fourth
Iteration

key=87

87>-93

---

Fifth
Iteration

key=-93

---

Sixth
iteration

key=666

---
https://www.chegg.com/homework-help/questions-and-answers/sort-given-array-using-insertion-sort-show-steps-simulation-4-76-172-1-87-93-666-cod… 2/5
7/10/2021 Sort The Given Array Using Insertion Sort. Show Th... | Chegg.com

Seventh
Iteration
  Textbook Solutions Expert Q&A Study Pack Practice

Explanation

Step
1

Iterate the array from first position to last position

Step
2

Compare the current value(key) of the array to its


predecessor

Step
3

if the key value is smaller than its prececessor compare its


value before

move the greater value one position up to make space for the
swapped value

---

good luck

Comment


Up next for you in Computer Science

Master's Method: Show Solve the recurrence with

tight
recurrence tree method
asymptotic bounds for (a)

these
T(n) = 2T(n/2) + 1 (b) T(n)
recurrences: a) T(n) = =
See more questions
2T(n/4)
3T(n/3) + n for subjects you study
+ 1 b) T(n) = 2T(n/4) + n1/2
c)

See answer See answer

Questions viewed by other students

Q: 1. i) Solve the following recurrence relations with Master


Method and comment on the runtime complexities
(i.e.
Theta).
(a) T(n) = 9T(n/3) + n2
(b) T(n) = 2T(n/2) + nlogn
ii) Solve the recurrence with iterative substitution
method
comment on the runtime complexities
(i.e. Big-Theta).
(a) T(n) = 2T(n/2) + cn
(b) T(n) = 2T(n/2) + n
https://www.chegg.com/homework-help/questions-and-answers/sort-given-array-using-insertion-sort-show-steps-simulation-4-76-172-1-87-93-666-cod… 3/5
7/10/2021 Sort The Given Array Using Insertion Sort. Show Th... | Chegg.com

A: See
 answer Textbook Solutions Expert Q&A Study Pack Practice

Q: Master's Method: Show tight asymptotic bounds for these


recurrences:
a) T(n) = 2T(n/4) + 1
b) T(n) = 2T(n/4) + n1
T(n) = 2T(n/4) + n
d) T(n) = 2T(n/4) + n2
e) T(n) = 3T(n/2) + nlgn

A: See answer

Show more 

COMPANY

LEGAL & POLICIES

CHEGG PRODUCTS AND SERVICES

CHEGG NETWORK

CUSTOMER SERVICE

© 2003-2021 Chegg Inc. All rights reserved.

https://www.chegg.com/homework-help/questions-and-answers/sort-given-array-using-insertion-sort-show-steps-simulation-4-76-172-1-87-93-666-cod… 4/5
7/10/2021 Sort The Given Array Using Insertion Sort. Show Th... | Chegg.com

  Textbook Solutions Expert Q&A Study Pack Practice

https://www.chegg.com/homework-help/questions-and-answers/sort-given-array-using-insertion-sort-show-steps-simulation-4-76-172-1-87-93-666-cod… 5/5

You might also like