0% found this document useful (0 votes)
10 views87 pages

LEC 9 Sorting

The document is a lecture on sorting algorithms, focusing on Selection Sort, Bubble Sort, and Insertion Sort. It outlines the algorithms' steps, time complexities, advantages, and disadvantages, emphasizing their efficiency in different scenarios. The content is structured to provide a clear understanding of how each sorting method operates and its practical implications.

Uploaded by

ibrahemashhab
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)
10 views87 pages

LEC 9 Sorting

The document is a lecture on sorting algorithms, focusing on Selection Sort, Bubble Sort, and Insertion Sort. It outlines the algorithms' steps, time complexities, advantages, and disadvantages, emphasizing their efficiency in different scenarios. The content is structured to provide a clear understanding of how each sorting method operates and its practical implications.

Uploaded by

ibrahemashhab
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/ 87

(ELE 144)

Data Structures and Algorithms

Lecture 9
Sorting Algorithms

Instructor:
DR. Nada El-Meligy
Agenda

Selection Bubble Insertion


Sort Sort Sort
Objectives
• Determine the running time of simple algorithms in the:
• Best case
• Average case
• Worst case
• Sorting algorithms
• Use O notation to measure the running time of algorithms
• It is important to be able to describe the efficiency of algorithms
• Time efficiency
• Space efficiency
Sorting Algorithms
➢Sorting Algorithm is an algorithm made up of a series of instructions
that takes an array as input, and outputs a sorted array.

➢There are many sorting algorithms, such as:


➢Selection Sort, Bubble Sort, Insertion Sort, Merge Sort, Heap Sort, Quick Sort,
Radix Sort, Counting Sort, Bucket Sort, Shell Sort, Comb Sort and Cycle Sort.
Selection Sort
The Selection Sort Algorithm
• The selection sort algorithm sorts an array repeatedly finding the
minimum element (considering ascending order) from unsorted part
and putting it at the beginning.
• Algorithm:
➢Step1: Find the minimum value in the list.
➢Step2: Swap it with the value in the current position.
➢Step3: Repeat this process from step 1 for all elements until the entire array is
sorted.
Selection Sort
• Example 1: Assume the following Array

8 12 5 9 2
Selection Sort Cont.
• Compare between data stored in min and data stored in j

8 12 5 9 2

i j
min
Selection Sort Cont.
• Compare between data stored in min and data stored in j

8 12 5 9 2

i j
min
Selection Sort Cont.
• Move min to j

8 12 5 9 2

i j
min
Selection Sort Cont.
• Compare between data stored in min and data stored in j

8 12 5 9 2

i min j
Selection Sort Cont.
• Compare between data stored in min and data stored in j

8 12 5 9 2

i min j
Selection Sort Cont.
• Move min to j

8 12 5 9 2

i j
min
Selection Sort Cont.
• Smallest

8 12 5 9 2

i min
Selection Sort Cont.
• Swap between data stored in i and min

8 12 5 9 2

i min
Selection Sort Cont.
• Sorted
• Unsorted

2 12 5 9 8

Sorted Unsorted
Selection Sort Cont.
• Compare

2 12 5 9 8

i j
Sorted
min
Selection Sort Cont.
• Move

2 12 5 9 8

i j
Sorted
min
Selection Sort Cont.
• Compare

2 12 5 9 8

i min j
Sorted
Selection Sort Cont.
• Compare

2 12 5 9 8

i min j
Sorted
Selection Sort Cont.
• Smallest

2 12 5 9 8

i min
Sorted
Selection Sort Cont.
• Swap

2 12 5 9 8

i min
Sorted
Selection Sort Cont.
• Sorted
• Unsorted

2 5 12 9 8

Sorted Unsorted
Selection Sort Cont.
• Compare

2 5 12 9 8

i j
Sorted
min
Selection Sort Cont.
• Move

2 5 12 9 8

i j
Sorted
min
Selection Sort Cont.
• Compare

2 5 12 9 8

i min j
Sorted
Selection Sort Cont.
• Move

2 5 12 9 8

i j
Sorted
min
Selection Sort Cont.
• Smallest

2 5 12 9 8

i min
Sorted
Selection Sort Cont.
• Swap

2 5 12 9 8

i min
Sorted
Selection Sort Cont.
• Sorted
• Unsorted

2 5 8 9 12

Sorted Unsorted
Selection Sort Cont.
• Compare

2 5 8 9 12

Sorted i j
min
Selection Sort Cont.
• Sorted
• Unsorted

2 5 8 9 12

Sorted Unsorted
Selection Sort Cont.
• Sorted
• Unsorted

2 5 8 9 12

Sorted i
min
Selection Sort Cont.
• Array is now sorted

2 5 8 9 12

Sorted
Selection Sorted Algorithm Cont.
• Example 2:

12, 10, 16, 11, 9, 7


void selection_sort(int data[ ], int n)
{
int i, j, smallest;
int temp;

if(n < 2) return; // nothing to sort!!

for(i = 0; i < n-1 ; i++)


{ O(n)
// find smallest in unsorted part of array
smallest = i;
for(j = i+1; j <= n-1; j++) O(n)
if(data[smallest] > data[j])
smallest = j;

// put it at front of unsorted part of array (swap)


temp = data[i];
data[i] = data[smallest];
data[smallest] = temp;
}
}
Time Complexity of Selection Sort
• Best-case: O(n2), best case occurs when the array is already sorted.
(where n is the number of integers in an array)

• Average-case: O(n2), the average case arises when the elements of the
array are in a disordered or random order, without a clear ascending or
descending pattern.

• Worst-case: O(n2), The worst-case scenario arises when we need to


sort an array in ascending order, but the array is initially in descending
order.
Advantage and Disadvantage
• Advantage:
• Simple Implementation: Selection sort is one of the easiest sorting algorithms to understand
and implement.
• Minimal Memory Usage: Selection sort is an in-place algorithm, meaning it sorts the array
directly without needing any extra memory space that increases with the size of the input.
• Used to get minimum and maximum number of items and in this case the performance = O(n).
• Disadvantage:
• Poor Time Complexity: The worst-case, average-case, and best-case time complexity of
selection sort are all O(n^2).
• Not Suitable for Large Datasets: Due to its O(n^2) time complexity, selection sort is not a
good choice for sorting large amounts of data.
• Not Adaptive: Selection sort is not an adaptive sorting algorithm, meaning that its
performance does not improve if the input data is already partially sorted.
Bubble Sort
Bubble Sort
• Bubble sort is the simplest sorting algorithm that works by repeatedly
swapping the adjacent elements if they are in wrong order.
• Algorithm:
➢Step1: Compare each pair of adjacent elements in the list.
➢Step2: Swap two element if necessary.
➢Step3: Repeat this process from step 1 for all elements until the entire array is
sorted.
Bubble Sort
• Example 1: Assume the following Array

8 12 5 9 2
Bubble Sort Cont.
• First iteration:
• Compare

8 12 5 9 2

j j+1
Bubble Sort Cont.
• First iteration:
• Compare

8 12 5 9 2

j j+1
Bubble Sort Cont.
• First iteration:
• Swap

8 5 12 9 2

j j+1
Bubble Sort Cont.
• First iteration:
• Compare

8 5 12 9 2

j J+1
Bubble Sort Cont.
• First iteration:
• Swap

8 5 9 12 2

j J+1
Bubble Sort Cont.
• First iteration:
• Compare

8 5 9 12 2

j j+1
Bubble Sort Cont.
• First iteration:
• swap

8 5 9 2 12

j j+1
Bubble Sort Cont.
• Complete First iteration:

8 5 9 2 12
Bubble Sort Cont.
• Second iteration:
• Compare

8 5 9 2 12

j j+1
Bubble Sort Cont.
• Second iteration:
• Swap

5 8 9 2 12

j j+1
Bubble Sort Cont.
• Second iteration:
• Compare

5 8 9 2 12

j j+1
Bubble Sort Cont.
• Second iteration:
• Compare

5 8 9 2 12

j J+1
Bubble Sort Cont.
• Second iteration:
• Swap

5 8 2 9 12

j J+1
Bubble Sort Cont.
• Complete Second iteration:

5 8 2 9 12
Bubble Sort Cont.
• Third iteration:
• Compare

5 8 2 9 12

j J+1
Bubble Sort Cont.
• Third iteration:
• Compare

5 8 2 9 12

J J+1
Bubble Sort Cont.
• Third iteration:
• Swap

5 2 8 9 12

J J+1
Bubble Sort Cont.
• Complete third iteration:

5 2 8 9 12
Bubble Sort Cont.
• Fourth iteration:
• Compare

5 2 8 9 12

J J+1
Bubble Sort Cont.
• Fourth iteration:
• Swap

2 5 8 9 12

J J+1
Bubble Sort Cont.
• Complete fourth iteration:

2 5 8 9 12
Bubble Sort Cont.
• Array is now sorted

2 5 8 9 12
Bubble Sorted Algorithm Cont.
• Example 2:

5, 4, 2, 1, 3
void bubble_sort(int arr[ ], int n)
{
int i, j;
int temp;

if(n < 2) return; // nothing to sort!!

for(i = 1; i < n; ++i)


{
for(j = 0; j < n-i;++j)
if(arr[j] > arr[j+1]) // if out of order, swap!
{
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
void bubble_sort(int arr[ ], int n)
{
int i, j;
int temp;
bool swapped = true;

if(n < 2) return; // nothing to sort!!

for(i = 1; swapped & i < n; i++)


{ // if no elements swapped in an iteration,
// then elements are in order: done!
for(swapped = false, j = 0; j < n-i; j++)
if(arr[j] > arr[j+1]) // if out of order, swap!
{
swap(arr[j],arr[j+1]);
swapped = true;
}
}
}
Time Complexity

• Best Case O(N):The best case occurs when the array is already sorted. So
the number of comparisons required is N-1 and the number of swaps
required = 0. Hence the best case complexity is O(N).

• Average Case O(N2): The number of comparisons is constant in Bubble


Sort. So in average case, there are O(N2) comparisons.

• Worst Case O(N2): The worst-case condition for bubble sort occurs when
elements of the array are arranged in decreasing order.
In the worst case, the total number of iterations or passes required to sort a
given array is (N-1). where ‘N’ is the number of elements present in the
array.
Advantage and Disadvantage
• Advantage:
• Simple Implementation: Bubble Sort is one of the easiest sorting algorithms to understand and
implement.
• Easy to Detect Sorted Array: Bubble sort is adaptive and can detect an already sorted array in O(n)
time. During each pass, if no swaps occur, the algorithm can conclude that the array is sorted and
terminate early, making it efficient in such cases.
• Minimal Memory Usage: bubble sort is an in-place algorithm, meaning it sorts the array directly
without needing any extra memory space that increases with the size of the input.
• Used to get minimum and maximum number of items and in this case the performance = O(n).
• Disadvantage:
• Poor Time Complexity: The worst and average case time complexity is O(n^2), making it
inefficient for large datasets.
• Inefficient for Large Datasets: Due to its quadratic time complexity, Bubble Sort is not suitable for
sorting large amounts of data.
Insertion Sort
Insertion Sort
• Insertion sort is a simple sorting algorithm that works the way we sort
playing cards in our hands.

• Algorithm:
• Step1: Compare each pair of adjacent elements in the list.
• Step2: insert element into the sorted list, until it occupies correct position.
• Step3: swap two elements if necessary.
• Step4: Repeated this process from step1 for all the elements until the entire
array is sorted.
Insertion Sort
• Example 1: Assume the following Array

8 12 5 9 2
Insertion Sort

8 12 5 9 2

Sorted Unsorted
Insertion Sort
• Compare

8 12 5 9 2

i-1 i
Insertion Sort
• Sorted
• Unsorted

8 12 5 9 2

Sorted Unsorted
Insertion Sort
• Compare:
Found 5<12 so we need to move it in location 0 and shift all elements

8 12 5 9 2

i-1 i
Insertion Sort
• Compare:
Found 5<12 so we need to move it in location 0 and shift all elements

8 12 5 9 2

i-1 i
Insertion Sort
• Move 5

5 8 12 9 2

Sorted Unsorted
Insertion Sort
• Compare 9 with 12:
Found 9<12 so we need to move it in location 2 and shift all elements

5 8 12 9 2

i-1 i
Insertion Sort
• Move 9

5 8 9 12 2

Sorted Unsorted
Insertion Sort
• Compare 2 with 12:
Found 2<12 so we need to move it in location 0 and shift all elements

5 8 9 12 2

i-1 i
Insertion Sort
• Move 2

2 5 8 9 12

Sorted
Bubble Sorted Algorithm Cont.
• Example 2:

8, 4, 6, 3, 1, 9, 5
void insertion_sort(int list[ ], int listLength)
{
int i, location;
int temp;
for (i = 1; i < listLength;i++)
{if (list[ i] < list[i- 1] )
{
temp = list[i] ;
location = i;
do
{
list[ location] = list[ location - 1] ;
location--;
}
while (location > 0 && list[ location - 1] > temp) ;
list[ location] = temp;
}
}
}
Time Complexity
Best Case: O(N)
• The best-case time complexity of Insertion Sort occurs when the input array is already sorted.
• In this scenario, each element is compared with its preceding elements until no swaps are needed, resulting
in a linear time complexity.
• Therefore, the best-case time complexity is O(N), where n is the number of elements in the array.
Average Case: O(N2)
• The average-case time complexity of Insertion Sort is also O(N^2).
• This complexity arises from the nature of the algorithm, which involves pairwise comparisons and swaps to
sort the elements.

Worst Case: O(N^2)


• The worst-case time complexity of Insertion Sort occurs when the input array is in reverse sorted order.
• In this scenario, each element needs to be compared and possibly swapped with every preceding element,
resulting in a quadratic time complexity.
• Therefore, the worst-case time complexity is O(N^2), where n is the number of elements in the array.
Advantage and Disadvantage
• Advantage:
• Simple Implementation: insertion Sort is one of the easiest sorting algorithms to understand and
implement.
• Adaptive Algorithm: The algorithm performs well when working with nearly sorted data. It can
achieve a time complexity of O(n) in such cases because it requires minimal element movements to
complete the sorting.
• Minimal Memory Usage: insertion sort is an in-place algorithm, meaning it sorts the array directly
without needing any extra memory space that increases with the size of the input.
• Used to get minimum and maximum number of items and in this case the performance = O(n).
• Disadvantage:
• Poor Time Complexity: The worst and average case time complexity is O(n²), making it inefficient
for large datasets.
• Not Suitable for Large Datasets: Due to its quadratic time complexity, it becomes very slow when
dealing with large amounts of data.
• Many Element Shifts: For each insertion, many elements may need to be shifted, leading to
expensive operations in arrays.
Timing and Other Issues

• Selection Sort, Insertion Sort, and Bubble Sort all have a worst-case
time of O(n2), making them impractical for large arrays.
• But they are easy to program, easy to debug.
• Insertion Sort also has good performance when the array is nearly
sorted to begin with.
• But more sophisticated sorting algorithms are needed when good
performance is needed in all cases for large arrays.
• Next time: Quick Sort

You might also like