DSA Presentation 0101
DSA Presentation 0101
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.
• 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
simpler ones.
MERGE SORT
A divide-and-conquer algorithm
that splits, sorts, and merges
arrays.
• Steps:
• 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).
• 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:
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
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: