LEC 9 Sorting
LEC 9 Sorting
Lecture 9
Sorting Algorithms
Instructor:
DR. Nada El-Meligy
Agenda
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:
• 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.
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;
• 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).
• 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.
• 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