You are on page 1of 5

Algorithm Design and Programming Techniques Lecture 7

1st class Asst. Lecture Omar Nowfal

Sorting and Searching Algorithms


 Sorting Algorithms:
The sorting problem is to rearrange the items of a given list in nondecreasing order.
Type of Sorting Algorithm:
There are three types of sorting algorithm, and they are:
1. Selection Sort.
2. Bubble Sort.
3. Insertion Sort.

Selection Sort:
 Scan the entire given list to find its smallest element and exchange it with the first element,
putting the smallest element in its final position in the sorted list.

 Then scan the list, starting with the second element, to find the smallest among the last n −
1 elements and exchange it with the second element, putting the second smallest element in
its final position.

 Generally, on the ith pass through the list, which we number from 0 to n − 2, the algorithm
searches for the smallest item among the last n − i elements and swaps it with A[i].

Example1: Sort by Hand the following list using Selection Sort.


89 45 68 90 29 34 17

59 
 
Algorithm Design and Programming Techniques Lecture 7
1st class Asst. Lecture Omar Nowfal

Solution:
89 45 68 90 29 34 17
17 45 68 90 29 34 89
17 29 68 90 45 34 89
17 29 34 90 45 68 89
17 29 34 45 90 68 89
17 29 34 45 68 90 89
17 29 34 45 68 89 90

Sorted List 17 29 34 45 68 89 90

Selection Sort Algorithm:

Algorithm SelectiobSort (A[0..100],n )


Begin
for i ← 0 to n – 2 do
min ← i
for j ← i + 1 to n – 1 do
if (A[j] < A[min]) then
min ← j
temp ← A[i]
A[i] ← A[min]
A[min] ← temp
End

60 
 
Algorithm Design and Programming Techniques Lecture 7
1st class Asst. Lecture Omar Nowfal

Bubble Sort:
1. Compare adjacent elements of the list and exchange them if they are out of order. By
doing it repeatedly, we end up “bubbling up” the largest element to the last position on
the list.
2. The next pass bubbles up the second largest element, and so on, until after n − 1 passes
the list is sorted.

Example2: Sort by Hand the following list using bubble sort.


89 45 68 90 29
45 89 68 90 29 Step 1
45 68 89 90 29
45 68 89 29 90

45 68 89 29 90
Step 2
45 68 29 89 90

45 68 29 89 90
Step 3
45 29 68 89 90

45 29 68 89 90 Step 4
29 45 68 89 90

29 45 68 89 90 Sorted List

61 
 
Algorithm Design and Programming Techniques Lecture 7
1st class Asst. Lecture Omar Nowfal

Bubble Sort Algorithm:

Algorithm BubbleSort (A[0..100],n)


Begin
for i ← 0 to n – 2 do
for j ← 0 to n – 2 – i do
if (A[j + 1] < A[j]) then
temp ← A[j]
A[j] ← A[j + 1]
A[j + 1] ← temp
End

Insertion Sort:
 We need to find an appropriate position for A[n-1] among the sorted elements and
insert it there.

 This is usually done by scanning the sorted subarray from right to left until the first
element smaller than or equal to A[n-1] is encountered to insert A[n − 1] right after that
element.
Starting with A[1]and ending with A[n-1], A[i] is inserted in its appropriate place among the
first i elements of the array that have been already sorted (but, unlike selection sort, are generally
not in their final positions).

Example3: Sort by Hand the following list using Insertion Sort.


89 45 68 90 29 34 17

62 
 
Algorithm Design and Programming Techniques Lecture 7
1st class Asst. Lecture Omar Nowfal

89 45 68 90 29 34 17
45 89 68 90 29 34 17
45 68 89 90 29 34 17
45 68 89 90 29 34 17
29 45 68 89 90 34 17
29 34 45 68 89 90 17

Sorted List 17 29 34 45 68 89 90

Insertion Sort Algorithm:

Algorithm InsertionSort (A[0..100],n)


Begin
for i ← 1 to n – 1 do
V ← A[i]
j←i–1
while j >= 0 AND A[j] > V do
A[j + 1] ← A[j]
j←J–1
A[j + 1] ← V
End

63 
 

You might also like