0% found this document useful (0 votes)
9 views

Lecture 11

Uploaded by

bachir20062006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Lecture 11

Uploaded by

bachir20062006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

Data Structure &

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

The goal of a sorting operation is to arrange elements in


an array according to a specific order or criterion, either
in ascending or descending order.

There are two types of sorting:


 Internal sorting, where all elements are in main
memory (RAM).
 External sorting, where only a portion of the elements is
in main memory, while the others are in secondary
memory (Hard Disk, SSD).
Introduction

There are several sorting algorithms:


1. Odd-Even Transposition Sort
Principle: Two adjacent elements are compared and swapped
if the second element is smaller than the first. In this case, a
backward check is performed to ensure that the order has not
been modified, and if so, it is restored.
It consists of 2 phases – the odd phase and even phase:
 Odd phase: Every odd indexed element is compared with the next
even indexed element(considering 1-based indexing).
 Even phase: Every even indexed element is compared with the next
odd indexed element.
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

4. Selection Sort (Successive Minimum Sort)


sorts an array of N integers in ascending order.

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

3. Keep the left portion of the list sorted


 At i’th step, first i elements in list are sorted
 All other elements are bigger than first i elements
Selection Sort (Analysis)

 The principle of selection sort is to exchange, at each iteration i,


ranging from 0 to Size-1, the integer A[i] with the smallest
integer in the part of the array ranging from index i to Size-1
(size of the array).
 Modular Breakdown: We will build a module, a procedure that
performs the selection sort (SELSORT), which will require the
following modules:
 A module (MinInd) that gives us the index of the minimum element in
a portion of the array.
 A module (SWAP) that swaps two elements in the array.
 We will also need modules to read the initial array (READ1D) and
display the sorted array (WRITE1D).
Selection Sort (Modules)

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)

Construct the main algorithm


Analysis:
 Read the array A Algorithm Selection_Sort
Constant MAX = 100
 Sort the array A Variable Integer A[MAX], Size
 Display the array A Procedures SWAP, READ1D, WRITE1D, SELSORT
….Procedures Body…
BEGIN
READ1D(A, Size)
SELSORT(A, Size)
WRITE1D(A, Size)
END
Selection Sort (example)

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: 

 Initialization of two variables Min to 0 and Max to Size-1;


 We need a module
 While (Min < Max) we perform the following:
SearchDichV
 We assign to a variable mid the value (Min + Max) DIV 2
 Basic modules
 We compare the element A[mid] with the value V
 READ1D and WRITE1D
 If A[mid] < V, we set another minimum, which is "mid + 1"
 Otherwise, we set another maximum, which is "mid"
 We compare if A[mid] is equal to V
 If it is equal, we assign True to Found
 We assign: RechDichV = Found
Selection Sort (Application)
Boolean FUNCTION SearchDichV (Integer A[], Integer Size, Integer V)
Variable Integer Min, Max, mid
Boolean Found
BEGIN
Found = False
Min = 0
Max = Size -1
WHILE Min < Max AND NOT Found DO
mid = (Min + Max) DIV 2
IF A[mid] < V THEN
Min = mid + 1
ELSE
Max = mid
END IF
IF A[mid] == V THEN
Found = True
END IF
END WHILE
SearchDichV = Found
END
Selection Sort (Application)
Algorithm Selection_Sort
Constant MAX = 100
Variable Integer A[MAX], Size, V
Construct the main algorithm Boolean Result
Analysis: Procedures READ1D, WRITE1D, SearchDichV
 Read the array A ….Procedures Body…
BEGIN
 Read the searched value V READ1D(A, Size)
 Call the Function SearchDichV WRITE (‘The searched value is: ’)
 Display the result (V found or not)
READ (V)
Result = SearchDichV(A, Size, V)
IF Result == True THEN
WRITE (V, “ exist in the array A”)
ELSE
WRITE (V, “ does not exist in the array A”)
END IF
END
Complexity of Selection Sort

To assess the efficiency and


speed of a sorting algorithm, the
concept of complexity is used,
which provides an approximate
time for the algorithm expressed
in the number of operations
performed.
It is an order of magnitude
expressed in terms of the amount
of data to be processed.
Complexity of Selection Sort

6 × 5 × ½ = 30 × ½ = 15
If we replace 6 with n, we get
n × (n – 1) × ½

When multiplied, that's:


½ n² – ½ n

Complexity:
O(n²) –"quadratic time"

The average, best-case, and worst-case time


complexity of Selection Sort is: O(n²)
Selection Sort (Conclusion)

You might also like