30
Computer Science: A Structured Programming Approach Using C 1 8-5 Sorting One of the most common applications in One of the most common applications in computer science is sorting—the process computer science is sorting—the process through which data are arranged according to through which data are arranged according to their values. We are surrounded by data. If the their values. We are surrounded by data. If the data are not ordered, we would spend hours data are not ordered, we would spend hours trying to find a single piece of information. trying to find a single piece of information. Selection Sort Bubble Sort Insertion Sort Testing Sorts Topics discussed in this section: Topics discussed in this section:

Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Embed Size (px)

Citation preview

Page 1: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 1

8-5 Sorting

One of the most common applications in computer One of the most common applications in computer science is sorting—the process through which data are science is sorting—the process through which data are arranged according to their values. We are arranged according to their values. We are surrounded by data. If the data are not ordered, we surrounded by data. If the data are not ordered, we would spend hours trying to find a single piece of would spend hours trying to find a single piece of information. information.

Selection SortBubble SortInsertion SortTesting Sorts

Topics discussed in this section:Topics discussed in this section:

Page 2: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 2

FIGURE 8-18 Selection Sort Concept

Page 3: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 3

FIGURE 8-19 Selection Sort Example

Page 4: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 4

FIGURE 8-20 Design for Selection Sort

Page 5: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 5

PROGRAM 8-9 Selection Sort

Page 6: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 6

PROGRAM 8-9 Selection Sort

Page 7: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 7

FIGURE 8-21 Bubble Sort Concept

Page 8: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 8

FIGURE 8-22 Bubble Sort Example

Page 9: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 9

FIGURE 8-23 Bubble Sort Design

Page 10: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 10

PROGRAM 8-10 Bubble Sort

Page 11: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 11

PROGRAM 8-10 Bubble Sort

Page 12: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 12

FIGURE 8-24 Insertion Sort Concept

Page 13: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 13

FIGURE 8-25 Insertion Sort Example

Page 14: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 14

FIGURE 8-26 Insertion Sort Design

Page 15: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 15

PROGRAM 8-11 Insertion Sort

Page 16: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 16

PROGRAM 8-11 Insertion Sort

Page 17: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 17

PROGRAM 8-12 Testing Sorts

Page 18: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 18

PROGRAM 8-12 Testing Sort

Page 19: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 19

8-6 Searching

Another common operation in computer science is Another common operation in computer science is searching, which is the process used to find the searching, which is the process used to find the location of a target among a list of objects. In the case location of a target among a list of objects. In the case of an array, searching means that given a value, we of an array, searching means that given a value, we want to find the location (index) of the first element in want to find the location (index) of the first element in the array that contains that value.the array that contains that value.

Sequential SearchBinary Search

Topics discussed in this section:Topics discussed in this section:

Page 20: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 20

FIGURE 8-27 Search Concept

Page 21: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 21

FIGURE 8-28 Locating Data in Unordered List

Page 22: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 22

FIGURE 8-29 Unsuccessful Search in Unordered List

Page 23: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 23

FIGURE 8-30 Sequential Search Design

Page 24: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 24

PROGRAM 8-13 Sequential Search

Page 25: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 25

PROGRAM 8-13 Sequential Search

Page 26: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 26

FIGURE 8-31 Binary Search Example

Page 27: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 27

FIGURE 8-32 Unsuccessful Binary Search Example

Page 28: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 28

FIGURE 8-33 Design for Binary Search

Page 29: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 29

PROGRAM 8-14 Binary Search

Page 30: Computer Science: A Structured Programming Approach Using C1 8-5 Sorting One of the most common applications in computer science is sorting—the process

Computer Science: A Structured Programming Approach Using C 30

PROGRAM 8-14 Binary Search