Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
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.
initial
60 50 40 30 20
values
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
initial
values
10 40 30
60 50
i=4
10
50 60
50 60
i=3
10 30 40
i=2 DONE: no swaps on previous pass
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. initial 60 50 40 30 20 values 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 initial values 10 40 30 60 50 i=4 10 50 60 50 60 i=3 10 30 40 i=2 DONE: no swaps on previous pass
3
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
3.2
initial values 8 5 3 10 2
i=0
i=1
i=2
i=3
i = 4
initial values 40
i=0
i=1
i=2
i=3
i = 4
42 -3 10 0
Transcribed Image Text:3 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 3.2 initial values 8 5 3 10 2 i=0 i=1 i=2 i=3 i = 4 initial values 40 i=0 i=1 i=2 i=3 i = 4 42 -3 10 0
Expert Solution
steps

Step by step

Solved in 2 steps

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