100% found this document useful (2 votes)
20 views25 pages

Sorting Algorithms Overview and Analysis

The document provides information on selection sort, bubble sort, insertion sort, and merge sort algorithms. It explains that selection sort and bubble sort find the minimum value and swap it into the first position, repeating for the remainder of the list. Insertion sort works by inserting elements into their sorted position in the existing sorted portion of the list. Merge sort uses a divide and conquer approach by recursively sorting halves of the array and then merging the sorted halves. The document analyzes the time complexity of insertion sort and merge sort, concluding that insertion sort is O(n^2) in the worst case while merge sort is O(n log n).

Uploaded by

api-3825915
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
20 views25 pages

Sorting Algorithms Overview and Analysis

The document provides information on selection sort, bubble sort, insertion sort, and merge sort algorithms. It explains that selection sort and bubble sort find the minimum value and swap it into the first position, repeating for the remainder of the list. Insertion sort works by inserting elements into their sorted position in the existing sorted portion of the list. Merge sort uses a divide and conquer approach by recursively sorting halves of the array and then merging the sorted halves. The document analyzes the time complexity of insertion sort and merge sort, concluding that insertion sort is O(n^2) in the worst case while merge sort is O(n log n).

Uploaded by

api-3825915
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Merge sort, Insertion sort

Sorting I / Slide 2

Sorting
☛ Selection sort or bubble sort
1. Find the minimum value in the list
2. Swap it with the value in the first position
3. Repeat the steps above for remainder of the list (starting at the
second position)

☛ Insertion sort
☛ Merge sort
☛ Quicksort
☛ Shellsort
☛ Heapsort
☛ Topological sort
☛ …
Sorting I / Slide 3

Bubble sort and analysis


for (i=0; i<n-1; i++) {
for (j=0; j<n-1-i; j++) {
if (a[j+1] < a[j]) { // compare the two neighbors
tmp = a[j]; // swap a[j] and a[j+1]
a[j] = a[j+1];
a[j+1] = tmp;
}
}
}

☛ Worst-case analysis: N+N-1+ …+1= N(N+1)/2, so O(N^2)


Sorting I / Slide 4

☛ Mergesort:
■ Divide and conquer principle
☛ Insertion:
■ Incremental algorithm principle
Sorting I / Slide 5

Mergesort
Based on divide-and-conquer strategy

☛ Divide the list into two smaller lists of about equal


sizes
☛ Sort each smaller list recursively
☛ Merge the two sorted lists to get one sorted list

How do we divide the list? How much time needed?


How do we merge the two sorted lists? How much time
needed?
Sorting I / Slide 6

Dividing
☛ If an array A[0..N-1]: dividing takes O(1) time
■ we can represent a sublist by two integers left
and right: to divide A[left..Right], we
compute center=(left+right)/2 and obtain
A[left..Center] and A[center+1..Right]
Sorting I / Slide 7

Mergesort
☛ Divide-and-conquer strategy
■ recursively mergesort the first half and the second
half
■ merge the two sorted halves together
Sorting I / Slide 8

[Link]
Sorting I / Slide 9

How to merge?
☛ Input: two sorted array A and B
☛ Output: an output sorted array C
☛ Three counters: Actr, Bctr, and Cctr
■ initially set to the beginning of their respective arrays

(1) The smaller of A[Actr] and B[Bctr] is copied to the next entry in
C, and the appropriate counters are advanced
(2) When either input list is exhausted, the remainder of the other
list is copied to C
Sorting I / Slide 10

Example: Merge
Sorting I / Slide 11

Example: Merge...

☛Running time analysis:


■ Clearly, merge takes O(m1 + m2) where m1 and m2 are
the sizes of the two sublists.
☛Space requirement:
■merging two sorted lists requires linear extra memory
■additional work to copy to the temporary array and back
Sorting I / Slide 12
Sorting I / Slide 13

Analysis of mergesort
Let T(N) denote the worst-case running time of
mergesort to sort N numbers.

Assume that N is a power of 2.

☛ Divide step: O(1) time


☛ Conquer step: 2 T(N/2) time
☛ Combine step: O(N) time

Recurrence equation:
T(1) = 1
T(N) = 2T(N/2) + N
Sorting I / Slide 14

Analysis: solving recurrence


N
T ( N ) = 2T ( )+ N Since N=2k, we have k=log2 n
2
N N
= 2(2T ( ) + ) + N
4 2 N
T ( N ) = 2 T ( k ) + kN
k

N 2
= 4T ( ) + 2 N
4 = N + N log N
N N = O ( N log N )
= 4(2T ( ) + ) + 2 N
8 4
N
= 8T ( ) + 3 N = 
8
N
= 2 T ( k ) + kN
k

2
Sorting I / Slide 15

Insertion sort
1) Initially p = 1
2) Let the first p elements be sorted.
3) Insert the (p+1)th element properly in the list (go inversely from
right to left) so that now p+1 elements are sorted.
4) increment p and go to step (3)
Sorting I / Slide 16

Insertion Sort
Sorting I / Slide 17

Insertion Sort

[Link]

☛ Consists of N - 1 passes
☛ For pass p = 1 through N - 1, ensures that the elements in
positions 0 through p are in sorted order
■ elements in positions 0 through p - 1 are already sorted
■ move the element in position p left until its correct place is found
among the first p + 1 elements
Sorting I / Slide 18

Extended Example
To sort the following numbers in increasing order:
34 8 64 51 32 21

p = 1; tmp = 8;
34 > tmp, so second element a[1] is set to 34: {8, 34}…
We have reached the front of the list. Thus, 1st position a[0] = tmp=8
After 1st pass: 8 34 64 51 32 21
(first 2 elements are sorted)
Sorting I / Slide 19

P = 2; tmp = 64;
34 < 64, so stop at 3rd position and set 3rd position = 64
After 2nd pass: 8 34 64 51 32 21
(first 3 elements are sorted)
P = 3; tmp = 51;
51 < 64, so we have 8 34 64 64 32 21,
34 < 51, so stop at 2nd position, set 3rd position = tmp,
After 3rd pass: 8 34 51 64 32 21
(first 4 elements are sorted)
P = 4; tmp = 32,
32 < 64, so 8 34 51 64 64 21,
32 < 51, so 8 34 51 51 64 21,
next 32 < 34, so 8 34 34, 51 64 21,
next 32 > 8, so stop at 1st position and set 2nd position = 32,
After 4th pass: 8 32 34 51 64 21
P = 5; tmp = 21, . . .
After 5th pass: 8 21 32 34 51 64
Analysis: worst-case running
Sorting I / Slide 20

time

☛ Inner loop is executed p times, for each p=1..N


⇒ Overall: 1 + 2 + 3 + . . . + N = O(N2)
☛ Space requirement is O(N)
Sorting I / Slide 21

The bound is tight


☛ The bound is tight Θ(N2)
☛ That is, there exists some input which actually uses Ω
(N2) time
☛ Consider input as a reversed sorted list
■ When a[p] is inserted into the sorted a[0..p-1], we
need to compare a[p] with all elements in a[0..p-1]
and move each element one position to the right
⇒ Ω(i) steps

■ the total number of steps is Ω(Σ1N-1 i) = Ω(N(N-1)/2)


= Ω(N2)
Sorting I / Slide 22

Analysis: best case


☛ The input is already sorted in increasing order
■ When inserting A[p] into the sorted A[0..p-1], only
need to compare A[p] with A[p-1] and there is no
data movement
■ For each iteration of the outer for-loop, the inner
for-loop terminates after checking the loop
condition once => O(N) time
☛ If input is nearly sorted, insertion sort runs fast
Sorting I / Slide 23

Summary on insertion sort


☛ Simple to implement
☛ Efficient on (quite) small data sets
☛ Efficient on data sets which are already substantially sorted
☛ More efficient in practice than most other simple O(n2)
algorithms such as selection sort or bubble sort: it is linear in the
best case
☛ Stable (does not change the relative order of elements with
equal keys)
☛ In-place (only requires a constant amount O(1) of extra memory
space)
☛ It is an online algorithm, in that it can sort a list as it receives it.
Sorting I / Slide 24

An experiment
☛ Code from textbook (using template)
☛ Unix time utility
Sorting I / Slide 25

Common questions

Powered by AI

Merge Sort uses a divide-and-conquer strategy, where it recursively splits the list into two equal halves, sorts each half, and then merges them back together . Insertion Sort, on the other hand, builds the sorted list one element at a time by repeatedly inserting the next unsorted element into the correct position within the already sorted section of the list .

Merge Sort has a time complexity of O(N log N) because it divides the array into two halves recursively and merges them, which involves log N levels and N operations per level . Bubble Sort has a time complexity of O(N^2) in its worst case due to the nested loops that compare and swap adjacent elements throughout the list . The efficiency of Merge Sort over Bubble Sort is significant for larger datasets, as Bubble Sort's quadratic time complexity becomes a major disadvantage .

Insertion Sort is advantageous for small datasets or datasets that are already close to being sorted because its best-case time complexity is O(N), much faster than its O(N^2) worst-case . It is simple to implement, operates efficiently on small to moderately sized datasets, and is stable, preserving the relative order of elements with equal keys. Furthermore, it requires only constant additional space, making it an attractive option for memory-constrained environments .

In Merge Sort, the Divide step involves splitting the list into two halves, which can be represented by obtaining the mid-point of the list, facilitating the division in constant time O(1). The Conquer step recursively sorts the two halves by further dividing them until single-element sublists are achieved, inherently sorted. These sorted halves are then merged in the Combine step to produce a fully sorted list .

Merge Sort employs the divide-and-conquer strategy, which splits the problem into sub-problems of smaller size until they can be solved directly. These solutions are then combined (merged) to form a complete solution to the original problem. This strategy facilitates efficient sorting by ensuring that each level of recursive division involves minimal direct sorting operations, with the efficiency gained primarily during the merging phase, where two already sorted lists are combined .

The tight bound of Θ(N^2) indicates that there exist worst-case scenarios (e.g., a reversed sorted list), where Insertion Sort performs poorly, requiring quadratic time due to the necessity to compare and reorder every element. However, this bound also suggests that the best-case scenario, when the list is already sorted or nearly sorted, allows Insertion Sort to perform close to linear time O(N), showcasing its remarkable efficiency under favorable conditions .

Online algorithms process their input piece-by-piece in a serial fashion, without having the entire problem available from the start. Insertion Sort qualifies as an online algorithm because it sorts the list as it receives each element, immediately placing it into the already sorted portion of the array. This characteristic allows it to start working and produce partial results immediately without needing the entire dataset upfront, which is useful in scenarios where data is streamed in real-time .

A stable sorting algorithm maintains the relative order of equal elements, which is crucial when sorting complex records based on one key while preserving another order. Insertion Sort fulfills this property by inserting elements into their correct position without altering the order of equal keys, ensuring that elements with the same value appear in the sorted order in the same sequence as they appeared in the input .

Bubble Sort is often used in educational contexts because it is simple to understand and implement. It provides a clear illustration of the concept of comparing and swapping adjacent elements, aiding learners in grasping fundamental algorithm behavior. Additionally, its step-by-step process is useful for debugging simple sorts, despite being inefficient for large datasets .

Merge Sort has a space complexity of O(N) as it requires additional space for a temporary array to merge the halves. Each recursive call produces two sub-problems whose results must be temporarily stored before merging. This is in contrast to in-place sorting algorithms like Insertion Sort, where sorting is performed within the original array with minimal extra memory usage .

You might also like