C was initially used for systems programmingA system program forms a portion of the operating system of the computer or its support utilitiesOperating Systems, Interpreters, Editors, Assembly programs are usually called system programsThe UNIX operating system was developed using CThere are C compilers available for almost all types of PC’s
Trang 1FPT APTECH COMPUTER EDUCATION HANOI
LBC, Session 7
Searching and Sorting
Trang 2• Explain the need of searching and sorting
• Discuss basic sorting algorithms:
Trang 3data in a specified order such as
ascending or descending
when the array is sorted
Selection Sort and Bubble Sort
Trang 4Sorting (cont’d)
value present in each element is
compared with the subsequent
elements in the array to obtain the least/greatest value
sort implementation:
Bottom-up
Top-down
Trang 5148 1488 17 22
17
148
126
6
With a[i] and a[i+1]:
If a[i] greater than a[i+1] then swap there location.
Bubble Sort
Trang 7Insertion Sort
• Main Ideas for sorting using
Insertion Sort:
– For each element of the array, put it to the
‘right’ position between other sorted elements– When the last element is put to the ‘right’
location, the array is sorted
Trang 823 17 45 18 12 22
Insertion Sort
Trang 923 17 45 18 12 22
Insertion Sort
Trang 1223 17
Trang 18Linear Searching
element in an array by visiting all elements of the array until the item
is found
Trang 20equals, end searching
Step 2: If the comparison returns ‘less than’ repeat step
1 with the left-side part of the array
Step 3: If the comparison returns ‘greater than’ repeat
step 1 with the right-side part of the array
Trang 218 2
0
64 14
13 25 33 43 51 53 72 84 93 95 96 97 6
Trang 22Binary Searching - Example
Example: Find number 33 in the sorted array below:
8 2
0
64 14
13 25 33 43 51 53 72 84 93 95 96 97 6
Trang 23Binary Searching - Example
Example: Find number 33 in the
sorted array below:
8 2
0
64 14
13 25 33 43 51 53 72 84 93 95 96 97 6
Trang 24Binary Searching - Example
Example: Find number 33 in the
sorted array below:
8 2
0
64 14
13 25 33 43 51 53 72 84 93 95 96 97 6
Trang 25Binary Searching - Example
Example: Find number 33 in the
sorted array below:
8 2
0
64 14
13 25 33 43 51 53 72 84 93 95 96 97 6
Trang 26Binary Searching - Example
Example: Find number 33 in the
sorted array below:
8 2
0
64 14
13 25 33 43 51 53 72 84 93 95 96 97 6
lo mid hi
Trang 27Binary Searching - Example
Example: Find number 33 in the
sorted array below:
8 2
0
64 14
13 25 33 43 51 53 72 84 93 95 96 97 6
lo hi
Trang 28Binary Searching - Example
Example: Find number 33 in the
sorted array below:
8 2
0
64 14
13 25 33 43 51 53 72 84 93 95 96 97 6
lo hi mid
Trang 29Binary Searching - Example
Example: Find number 33 in the
sorted array below:
8 2
0
64 14
13 25 33 43 51 53 72 84 93 95 96 97 6
lo hi mid
Trang 30Binary Searching – Impl.
Trang 31Binary Searching – Impl.
Trang 32• Variables and constants
• Identifier and guidelines for naming identifiers
• Data types
• Basic data types: char, int, float, double, void
• Derived data types: unsigned, short, long
Trang 33Review (cont’d)
• Input output in C:
– Formatted
– Unformatted
• Conditional statement: if else, switch.
• Loop: for, while, do while.
• Array
• String/character arrays.
Trang 34• Textbook, Chapter 19
• Sort Animation