0% found this document useful (0 votes)
15 views19 pages

DSA Presentation 0101

Uploaded by

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

DSA Presentation 0101

Uploaded by

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

OUR TEAM

Fatima Ajmal
Amna
Ali Hassan
Rana Ali Akbar

DAT 5 DECEMBER ,
E: 2024
SORTING
ALGORITHMI
C
APPROACHE
A Detailed Study of Common
S Sorting Techniques
INTRODUCTION TO
SORTING
Sorting is the process of arranging elements in a
particular order (ascending or descending).
SORTING PLAYS A CRUCIAL ROLE IN:

• Search Optimization:
A sorted dataset enables faster searches (e.g.,
Binary Search).

• Data Organization:
Organizing large datasets for readability and
usability.

• Applications in Real Life:


Ranking students by grades or arranging contacts
TYPES OF
Sorting techniques can be divided into two
SORTING
major categories:
• Internal Sorting:
Sorting is performed entirely in memory.
Suitable for smaller datasets.

• External Sorting:
Data that is too large for memory is sorted
using storage like disks (e.g., External Merge
Sort).
RECURSION IN
SORTING
Recursion is a programming technique
commonly used to break down complex
problems into smaller subproblems, which
can then be solved more easily.
• Role in Sorting

1.Key in divide-and-conquer algorithms


like Merge Sort and Quick Sort.

2.Reduces complex problems into

simpler ones.
MERGE SORT
A divide-and-conquer algorithm
that splits, sorts, and merges
arrays.
• Steps:

1.Divide the array into halves recursively.


2. Sort each half. Time Complexity:
Best/Worst/Average: O(n log
3.Merge the sorted halves.
n).
Space Complexity:
O(n)
EXAMPLE
Array:[4, 2, 7, 1]
• Steps:
• 1. Divide: Split the array into halves: [4, 2] and
[7, 1].

• 2. Recursive Split:
• [4, 2] → Split into [4] and [2].
• [7, 1] → Split into [7] and [1].

• 3. Merge Step:
• Merge [4] and [2] → [2, 4].
• Merge [7] and [1] → [1, 7].
QUICK SORT
A divide-and-conquer algorithm
using a pivot to partition the
array.
• Steps:

1.Choose a pivot.
2.Partition elements around the pivot. Time Complexity:
Best/Average: O(n log n).
3. Recursively apply the algorithm to
Worst Case: O(n²)
subarrays.
Space Complexity:
O(log n).
EXAMPLE
[5, 3, 8, 6, 2]

• Steps:
• 1. Choose Pivot: Select the last element as the
pivot (2).

• 2. Partition: Rearrange elements such that all


elements less than the pivot are on the left and
greater on the right.
After partition: [2, 3, 8, 6, 5] (pivot is at its correct
position).

• 3. Recursive Steps:
Left Subarray: [2] (already sorted).
Right Subarray: [3, 8, 6, 5]
Choose Pivot: 5, partition → [3, 5, 8, 6].
Sort left [3] (already sorted) and right [8, 6] → [6, 8].
INSERTION SORT
Inserts elements into their correct
position one at a time.

• Steps:

1.Start with the second element.


2.Compare it with elements before it. Time Complexity:
Best Case: O(n)
3. Shift larger elements and insert the
Worst Case: O(n²)
current element.
Space Complexity:
O(1)
EXAMPLE
• Suppose you have the array:
[5, 2, 9, 1]
• The first element, 5, is already in the sorted
portion, so it stays as it is.
• The next element, 2, is smaller than 5, so we
move 2 before 5:
[2, 5, 9, 1]
• The next element, 9, is larger than 5, so it stays
as it is:
[2, 5, 9, 1]
• The next element, 1, is smaller than 2, so we
move 1 before 2:
SELECTION SORT
Selects the smallest element and
swaps it with the first unsorted
element.
• Steps:

1. Find the minimum in the unsorted part.


2.Swap it with the first unsorted element. Time Complexity:
O(n²)
3.Repeat for the rest.
Space Complexity:
O(1).
EXAMPL
• Suppose you have the array:
E
[5, 2, 9, 1]
• In the first pass: Compare 5 and 2, swap them:
[2, 5, 9, 1]
Compare 5 and 9, no swap needed.
Compare 9 and 1, swap them:
[2, 5, 1, 9]
• In the second pass: Compare 2 and 5, no swap
needed.
Compare 5 and 1, swap them:
[2, 1, 5, 9]
Compare 5 and 9, no swap needed.
• In the third pass: Compare 2 and 1, swap
them:
BUBBLE SORT
Repeatedly swaps adjacent
elements if they are in the wrong
order.
• Steps:

1.Compare adjacent elements.


2. Swap if needed. Time Complexity:
Best Case: O(n)
3. Repeat until no swaps are needed.
Worst Case: O(n²)

Space Complexity:
O(1)
EXAMPL
• Suppose you have the array:
E
[5, 2, 9, 1]
• In the first iteration: The smallest element in
the array is 1 we swap it with the first element 5:
[1, 2, 9, 5]
• In the second iteration: The smallest element
in the remaining unsorted portion [2, 9, 5] is 2,
which is already in its correct position, so no
change:
[1, 2, 9, 5]
• In the third iteration: The smallest element in
the remaining unsorted portion [9, 5] is 5 we
swap 9 and 5:
COMPARISON OF
SORTING ALGORITHMS

Algorithm Time Space


Complexity Complexity
Insertion O(n²) / O(1)

Sort
Selection
O(n)
O(n² O(1)
Sort )O(n²) /
Bubble O(1)
Sort O(n)
Merge O(n log O(n
Sort n) )
Quick O(n log n) / O(log
Sort n)
O(n²)
MERGE SORT QUICK SORT
• Time complexity • Time complexity is
is consistently O(n log n) for
O(n log n) for all best/average cases
cases (best, but O(n²) in the
average, and worst case.
worst).
• More efficient for in-
• Suitable for memory sorting of
external sorting arrays.
and linked lists.
• Simpler to
• Implementation implement but
is more complex requires careful
due to the pivot selection to
merging process. avoid the worst
CONCLUSION
Sorting is vital for efficient data
handling.
• KEY TAKEAWAYS:

• Choose algorithms based on dataset size and


constraints.
• Merge Sort and Quick Sort are ideal for larger
datasets.
• Simpler algorithms like Bubble or Selection are
useful for educational purposes.
THANK
YOU!

You might also like