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

Algo Lec 3

Uploaded by

Jamshaid Ansari
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Algo Lec 3

Uploaded by

Jamshaid Ansari
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

N² Sorting Algorithms and Their Time

Complexity Analysis:
What is Sorting?
 Sorting is the process of arranging
elements in a specific order (ascending
or descending).
o Example: Sorting numbers ([3, 1,
4, 5, 2] → [1, 2, 3, 4, 5])
o Sorting strings (["Banana",
"Apple", "Cherry"] → ["Apple",
"Banana", "Cherry"])
Why Sorting is Important?
 Sorting is crucial for:
o Efficient Searching: Faster
searching algorithms (e.g., binary
search).
o Data presentation: Better for
reporting, financial data, etc.
o Data Storage and Processing:
Sorting is used in databases,
Design and Analysis of Algorithms 2

algorithms for efficient data


retrieval.

What Are N² Sorting Algorithms?


 These algorithms have a time
complexity of O(n²) in the worst and
average cases.
 They involve nested iterations for
sorting elements.
 Examples: Insertion Sort, Selection
Sort, Bubble Sort.

Insertion Sort:
Example Walkthrough (Step-by-Step)
Using the array [5, 2, 9, 1, 5, 6]:
1. Start with the second element
(2): Compare with 5. Shift 5 to the
right and place 2 at the start.
Array after pass 1: [2, 5, 9, 1, 5, 6]
2. Move to the third element (9): No
change, as 9 is greater than 5.

2 2
Design and Analysis of Algorithms 3

Array after pass 2: [2, 5, 9, 1, 5, 6]


3. Move to the fourth element (1):
Compare with 9, 5, 2. Shift all greater
elements to the right and place 1 at the
start.
Array after pass 3: [1, 2, 5, 9, 5, 6]
4. Move to the fifth element (5):
Compare with 9 and shift. Insert 5 in
the correct position.
Array after pass 4: [1, 2, 5, 5, 9, 6]
5. Move to the last element (6):
Compare with 9, shift, and insert 6.
Array after pass 5: [1, 2, 5, 5, 6, 9]
Final sorted array: [1, 2, 5, 5, 6, 9]

1.How Insertion Sort Works


1. Start with the second element (index 1)
because the first element (index 0) is
trivially sorted.

3 3
Design and Analysis of Algorithms 4

2. Pick the current element (called the


"key").
3. Compare it with elements in the sorted
portion (to the left of the key).
4. Shift larger elements to the right to
make space.
5. Insert the key in its correct position.
6. Repeat for all elements in the array.

Basic Structure of the Algorithm


 Outer Loop: Iterates through the
unsorted portion.
 Inner Loop: Compares the key with
elements in the sorted portion, shifting
larger elements.

Pseudocode
InsertionSort(array, n):
FOR i = 1 TO n - 1:
key = array[i]

4 4
Design and Analysis of Algorithms 5

j=i-1
WHILE j >= 0 AND array[j] > key:
array[j + 1] = array[j] // Shift
larger element to the right
j=j-1
ENDWHILE
array[j + 1] = key // Insert key
in its correct position
ENDFOR

Exact Count for Time Complexity


Outer Loop
 Runs from 1 to n-1 (total n-1
iterations).
Inner Loop
 The number of comparisons in the
inner loop depends on the sorted
portion:
o Best Case: 1 comparison per
iteration.
o Worst Case: Up to i comparisons
for each iteration of the outer loop.

5 5
Design and Analysis of Algorithms 6

Exact Count in Worst Case


 For an array sorted in reverse order:
so
1 + 2 + 3 + ... + (n-1) = n(n-1)/2

1 + 2 + 3 + ... + (n-1) = n(n-1)/2


Total operations:
Comparisons + Shifts = n(n-1)

Cost of Inserting One Element


 Inserting one element involves:
1. Comparisons: To find its position
in the sorted portion.
2. Shifts: To make space for the
element.
 Best Case: 1 comparison, 0 shifts (if
already sorted).
 Worst Case: i comparisons and i shifts
(if inserted at the beginning).

6 6
Design and Analysis of Algorithms 7

Complexity Analysis
Case Explanation Complexity
Array is already
Best sorted. Inner loop
O(n)
Case runs only once for
each element.
Array is sorted in
Worst reverse. Inner loop
O(n²)
Case runs i times for each
iteration.
Elements are in
random order. On
Average
average, half of the O(n²)
Case
sorted portion is
checked.

Do All Complexities Have Different


Values?
 Yes, the complexities differ:

7 7
Design and Analysis of Algorithms 8

1. Best Case (O(n)): Happens when


no shifting is needed because the
array is already sorted.
2. Worst Case (O(n²)): Happens
when every element needs to be
compared and shifted, as in a
reverse-sorted array.
3. Average Case (O(n²)): Falls
between the best and worst cases,
typically requiring about half the
operations of the worst case.

4.Insights from Complexity Analysis


 it helps us understand how much time
and resources an algorithm will need
based on:
o The size of the input data.
o The order of the data (e.g., sorted,
reverse-sorted, random).
 These insights guide us in choosing the
right algorithm for specific problems.

8 8
Design and Analysis of Algorithms 9

 The best-case complexity (O(n))


occurs because the algorithm avoids
unnecessary operations, hinting that
checking order first could be a way to
optimize.
 The worst-case complexity (O(n²))
shows the inefficiency of the algorithm
for larger datasets.
 These differences highlight that
Insertion Sort is suitable only for
small or nearly sorted datasets.
Best and Worst Case Analysis of
Insertion Sort
Best Case Analysis
 The best case occurs when the array is
already sorted.
 Key Observations:
o Each element is compared once
with the last element of the sorted
portion.
o No shifting of elements is required.

9 9
Design and Analysis of Algorithms 10

 Time Complexity:
o For n elements, the number of
comparisons = n-1.
o Total operations = O(n).
Worst Case Analysis
 The worst case occurs when the array
is sorted in reverse order.
 Key Observations:
For every element, all previous
o

elements must be compared and


shifted.
1 + 2 + 3 + ... + (n-1) = n(n-1)/2
 Time Complexity:
o Total operations = O(n²).

Real-World Applications of
Complexities
1.Best Case (O(n)):
o Useful for nearly sorted datasets.

10 10
Design and Analysis of Algorithms 11

o Example: Sorting incremental


records like recent transactions.
2.Worst Case (O(n²)):
o Highlights inefficiency for large or
reverse-ordered datasets.
o Example: Sorting new customer
data in descending order for
billing.
3.Average Case (O(n²)):
o Applies to randomly ordered
datasets.
o Example: Arranging unordered
logs from various sources.
Time Complexity: Constant and
Variable Cost Factors
Constant Cost Factors:
1. Operations independent of input
size, e.g.:
1. Assignments (key = array[i]).
2. Incrementing indices (i++, j--).

11 11
Design and Analysis of Algorithms 12

3. Loops
Variable Cost Factors:
1. Operations dependent on input
size, e.g.:
1. Comparisons (array[j] > key).
2. Shifts (array[j + 1] = array[j]).

12 12

You might also like