Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions Icon

Related questions

Question

c++

**Sorting**

This handout explains three quadratic sorting algorithms. Each explanation assumes we are sorting an array of size n in ascending order from position position [0] through [n-1]. (Technically, we should say "non-decreasing" rather than "ascending", since we could have duplicates.)

**Bubble Sort**

As was demonstrated in class, the main idea of bubble sort is as follows.

- Make n-1 passes through the array, numbered i=n-1, n-2, etc. down to 1
  - On pass i, we consider the part of the array 0 through i  
    We walk through that part of the array, looking at each pair of adjacent elements, and swapping them if they are out of order.
  - We also can keep track of whether any swaps happen during each pass. If we make a pass that has NO swaps at all, we can stop, because we know the array is already sorted.

Some things we know about bubble sort:

- After pass i, we know that the element in position i through the end of the array (n-1) is in proper place in the array. (This was explained in lecture.)
  - Corrolary 1: After pass 1, we know that elements 1 through n-1 are sorted and in their correct place in the array.
  - Corrolary 2: After pass 1, we know the entire array is sorted, because it is not possible for element [0] to be out of place if every other element is in place.

Given this, here are two example "worked problems" for bubble sort.

The part in bold is what YOU would write if given the problem.

*Example 1:*

| initial values | 60 | 50 | 40 | 30 | 20 |
|----------------|----|----|----|----|----|
| i=4            | **50** | **40** | **30** | **20** | **60** |
| i=3            | **40** | **30** | **20** | **50** | **60** |
| i=2            | **30** | **20** | **40** | **50** | **60** |
| i=1            | **20** | **30** | **40** | **50** | **60** |

*Example 2:*

| initial values
expand button
Transcribed Image Text:**Sorting** This handout explains three quadratic sorting algorithms. Each explanation assumes we are sorting an array of size n in ascending order from position position [0] through [n-1]. (Technically, we should say "non-decreasing" rather than "ascending", since we could have duplicates.) **Bubble Sort** As was demonstrated in class, the main idea of bubble sort is as follows. - Make n-1 passes through the array, numbered i=n-1, n-2, etc. down to 1 - On pass i, we consider the part of the array 0 through i We walk through that part of the array, looking at each pair of adjacent elements, and swapping them if they are out of order. - We also can keep track of whether any swaps happen during each pass. If we make a pass that has NO swaps at all, we can stop, because we know the array is already sorted. Some things we know about bubble sort: - After pass i, we know that the element in position i through the end of the array (n-1) is in proper place in the array. (This was explained in lecture.) - Corrolary 1: After pass 1, we know that elements 1 through n-1 are sorted and in their correct place in the array. - Corrolary 2: After pass 1, we know the entire array is sorted, because it is not possible for element [0] to be out of place if every other element is in place. Given this, here are two example "worked problems" for bubble sort. The part in bold is what YOU would write if given the problem. *Example 1:* | initial values | 60 | 50 | 40 | 30 | 20 | |----------------|----|----|----|----|----| | i=4 | **50** | **40** | **30** | **20** | **60** | | i=3 | **40** | **30** | **20** | **50** | **60** | | i=2 | **30** | **20** | **40** | **50** | **60** | | i=1 | **20** | **30** | **40** | **50** | **60** | *Example 2:* | initial values
# Insertion Sort Steps

## Exercise 3

**Task:** Show the steps of insertion sort following the example of the solved problems on the handout for the algorithm and the format of your answer.

---

### 3.1

**Initial Values:** 8, 5, 3, 10, 2

- **i = 0:**  
  _(Initial element, no action needed)_

- **i = 1:**  
  _(Move 5 before 8)_

- **i = 2:**  
  _(Move 3 before 5 and 8)_

- **i = 3:**  
  _(10 remains in place)_

- **i = 4:**  
  _(Move 2 to the front)_

---

### 3.2

**Initial Values:** 40, 42, -3, 10, 0

- **i = 0:**  
  _(Initial element, no action needed)_

- **i = 1:**  
  _(42 remains in place)_

- **i = 2:**  
  _(Move -3 to the front)_

- **i = 3:**  
  _(Move 10 between -3 and 40)_

- **i = 4:**  
  _(Move 0 between -3 and 10)_
expand button
Transcribed Image Text:# Insertion Sort Steps ## Exercise 3 **Task:** Show the steps of insertion sort following the example of the solved problems on the handout for the algorithm and the format of your answer. --- ### 3.1 **Initial Values:** 8, 5, 3, 10, 2 - **i = 0:** _(Initial element, no action needed)_ - **i = 1:** _(Move 5 before 8)_ - **i = 2:** _(Move 3 before 5 and 8)_ - **i = 3:** _(10 remains in place)_ - **i = 4:** _(Move 2 to the front)_ --- ### 3.2 **Initial Values:** 40, 42, -3, 10, 0 - **i = 0:** _(Initial element, no action needed)_ - **i = 1:** _(42 remains in place)_ - **i = 2:** _(Move -3 to the front)_ - **i = 3:** _(Move 10 between -3 and 40)_ - **i = 4:** _(Move 0 between -3 and 10)_
Expert Solution
Check Mark
Step 1

C++:

C++ is a general purpose high level programming language.

It is an extension of C language.

It follows bottom up approach.

 

 

Knowledge Booster
Background pattern image
Recommended textbooks for you
Text book image
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Text book image
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Text book image
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Text book image
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Text book image
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Text book image
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY