Lecture 11
Lecture 11
Algorithms 1
CHAPTER 4
STATIC DATA STRUCTURE (PART4):
SORTING ALGORITHMS
Sep – Dec 2023
Outline
Sorting Algorithms
Definition
Different sorting algorithms
Selection Sort
Analysis
Modular decomposition
Application
Complexity
Introduction
1. Bubble Sort
Principle: Badly sorted elements move up in the array like bubbles
rising to the surface of a liquid. The swapping of elements occurs
with permutations if elem1 > elem2. It is evident that multiple passes
over all elements are necessary. This method is less efficient.
Compare consecutive pairs of elements
Swap elements in pair such that smaller is first
When reach end of list, start over again
Stop when no more swap have been made
Largest unsorted element always at end after pass, so at most n passes
Introduction
3. Insertion Sort
Principle: This algorithm involves iterating through the values of the
array, one by one, and inserting them at the right place in the
sorted array consisting of previously inserted and sorted values.
The values are inserted in the order in which they appear in the
array. The challenge with this algorithm is that it requires traversing
the sorted array to determine where to insert the new element,
and then shifting all values greater than the element to be
inserted by one position.
Introduction
3. Insertion Sort
1. The list is divided into two parts: sorted and unsorted
2. In each pass, the first element of the unsorted part is picked up,
transferred to the sorted sublist, and inserted at the appropriate
place.
3. A list of n elements will take at most n-1 passes to sort the data
Introduction
Principle:
The smallest element of the array is swapped with the first element
of the array. Then, the smallest element of the remaining array is
swapped with the second element, and so on.
Selection Sort (Analysis)
1. First step
Extract minimum element
Swap it with element at index 0
2. Subsequent step
In remaining sublist, extract minimum element
Swap it with the element at index 1
PROCEDURE
Integer A Var Integer A
Integer B SWAP Var Integer B
PROCEDURE
Swap the content of A and B
Integer A[]
Var Integer A[]
Integer Size SELSORT
FUNCTION Sort the element of A in ascending
order using the selection method
Integer A[]
Integer B1 Integer
Integer B2
MinInd
Return the index of the smallest element
of a portion (B1, B2) of 1D array A
Selection Sort (Modules)
Module MinInd
Analysis:
We assume that B1 < B2 < Size. Integer Function MinInd (Integer A[], Integer B1,
Save B1 in Ind. Integer B2)
Variable Integer Ind, i
Traverse the array by varying the BEGIN
index: i = B1+1, B1+2, …, B2. At Ind = B1
each iteration: FOR i FROM B1+1 TO B2 DO
IF the element A[i] is smaller IF A[i] < A[Ind] THEN
than A[Ind] (a new minimum is Ind = i
found), THEN END IF
END FOR
Update the index: Ind = i. MinInd = Ind
END
Selection Sort (Modules)
Module SELSORT
Procedure SELSORT (Var Integer A[], Integer Size,)
Analysis:
Variable Integer Ind, I
Traverse the array by varying i: 0, 2, 3, Procedure SWAP
..., Size-1. At each iteration: ….Procedure Body…
Function MinInd
Search for the index of the smallest
….Function Body…
element between i and Size-1:
BEGIN
(MinInd(A, i, Size-1)).
FOR i FROM 0 TO Size-1 DO
Swap this element with A[i]. Ind = MinInd(A, i, Size-1)
SAWP (A[i], A[Ind])
END FOR
END
Selection Sort (Modules)
Step 1
We divide the array into a
left, sorted part and a
right, unsorted part. The
sorted part is empty at the
beginning:
Selection Sort (example)
Step 2
search for the
smallest element in
the right, unsorted
part.
Selection Sort (example)
Step 3
We search again in
the right…
Selection Sort (example)
Step 4
Again, we search …
Selection Sort (example)
Step 5
Again …
Selection Sort (example)
Step 6
Again …
Selection Sort (example)
Algorithm
finished
Selection Sort (Application)
Provide the solution that allows searching by dichotomy for a given
value V in a sorted array of up to 100 integers.
Analysis of Module SearchDichV
We assume having a sorted array
Initialization of a variable Found to false;
Modular Breakdown:
6 × 5 × ½ = 30 × ½ = 15
If we replace 6 with n, we get
n × (n – 1) × ½
Complexity:
O(n²) –"quadratic time"