Tải bản đầy đủ (.ppt) (34 trang)

Session 07 Introduction to Programming

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (228.64 KB, 34 trang )

LBC, Session 7
Searching and Sorting

FPT APTECH COMPUTER EDUCATION HANOI


Objectives



Explain the need of searching and sorting



Discuss basic sorting algorithms:
– Bubble Sort
– Insertion Sort



Discuss basic searching algorithms:
– Linear Search
– Binary Search

LBC/Session 7

2


Sorting
• Sorting involves arranging the array


data in a specified order such as
ascending or descending
• Data in an array is easier to search
when the array is sorted
• Two of methods to sort arrays:
Selection Sort and Bubble Sort

LBC/Session 7

3


Sorting (cont’d)
• In the selection sort method, the
value present in each element is
compared with the subsequent
elements in the array to obtain the
least/greatest value
• There are 2 approaches in bubble
sort implementation:
 Bottom-up
 Top-down
LBC/Session 7

4


Bubble Sort





12
6

12
6

22
14

22
14
8

8
17
22

17
22

6

12

14
8

14

8

17

22

For an array of n elements
Repeat these following steps n-1 times:


With a[i] and a[i+1]:


If a[i] greater than a[i+1] then swap there location.
LBC/Session 7

5


Bubble Sort

int i,j,temp,arr[5]={23,90,9,25,16};
for (int i=0; i<4; i++)
{
for (int j=0; j<5-i-1; j++)
{
if (arr[j] > arr[j+1])
{
int temp = arr[j];
arr[j] = arr[j+1];

arr[j+1] = temp;
}
}
}
LBC/Session 7

6


Insertion 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
LBC/Session 7

7


Insertion Sort

23

17

45


18

LBC/Session 7

12

22

8


Insertion Sort

23

17

45

18

12

22

1

2

3


4

5

6

LBC/Session 7

9


Insertion Sort

23

17

45

18

12

22

1

2


3

4

5

6

1

2

3

4

5

6

LBC/Session 7

10


Insertion Sort

1

17


45

18

12

22

2

3

4

5

6

2

3

4

5

6

23

1

LBC/Session 7

11


Insertion Sort

1

2

17

23

1

2

45

18

12

22

3


4

5

6

3

4

5

6

LBC/Session 7

12


Insertion Sort

1

2

3

17


23

45

1

2

3

18

12

22

4

5

6

4

5

6

LBC/Session 7


13


Insertion Sort

1

2

3

4

17

18

23

45

1

2

3

4

LBC/Session 7


12

22

5

6

5

6

14


Insertion Sort

1

2

3

4

5

6


12

17

18

22

23

45

1

2

3

4

5

6

LBC/Session 7

15


Insertion Sort

int data[5] = { 23, 90, 9, 25, 16 };
int tmp,i,j;
//perform insertion sorting
for (j=1; ji =j - 1;
tmp = data[j];
while ( (i>=0) && (tmp < data[i]) ) {
data[i+1] = data[i];
i--;
}
data[i+1] = tmp;
}
LBC/Session 7

16


Searching

For this lesson, we discuss two
kinds of searching:
• Linear search
• Binary search

LBC/Session 7

17


Linear Searching


 This is the simplest way to find an
element in an array by visiting all
elements of the array until the item
is found
 Also called Sequential searching

LBC/Session 7

18


Linear Searching

int data[] = {5,2,9,7,6,10};
int a = 7,i;
for(i = 0; i<6; i++){
if(a == data[i])
break;
}
if(i<6){
printf(“Number %d found at %d”,a,i);
}else{
printf(“Number not found”);
}
}
LBC/Session 7

19



Binary Searching

• Binary
Search
is
implemented
sorted arrays
• This algorithm uses recursion
implement searching:

on
to

 Step 1: Compare item to search with the element in the
middle location of the array. If the comparison returns
equals, 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
LBC/Session 7

20


Binary Searching - Example

Example: Find number 33 in the sorted array below:


6
0

13 14 25 33 43 51 53 64 72 84 93 95 96 97
1

2

3

4

5

6

7

8

lo

9

10

11

12


13

14

hi

LBC/Session 7

21


Binary Searching - Example

Example: Find number 33 in the sorted
array below:

6
0

lo

13 14 25 33 43 51 53 64 72 84 93 95 96 97
1

2

3

4


5

6

7

8

mid

LBC/Session 7

9

10

11

12

13

14

hi

22


Binary Searching - Example


Example: Find number 33 in the
sorted array below:

6
0

lo

13 14 25 33 43 51 53 64 72 84 93 95 96 97
1

2

3

4

5

6

7

8

9

10


11

12

13

14

hi

LBC/Session 7

23


Binary Searching - Example

Example: Find number 33 in the
sorted array below:

6
0

lo

13 14 25 33 43 51 53 64 72 84 93 95 96 97
1

2


3

mid

4

5

6

7

8

9

10

11

12

13

14

hi

LBC/Session 7


24


Binary Searching - Example

Example: Find number 33 in the
sorted array below:

6
0

13 14 25 33 43 51 53 64 72 84 93 95 96 97
1

2

3

4

lo

5

6

7

8


9

10

11

12

13

14

hi

LBC/Session 7

25


×