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

Demo Assignmentt

Uploaded by

mahi.r
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Demo Assignmentt

Uploaded by

mahi.r
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Sorting Algorithms

1. Bubble Sort

Pros:

1. Bubble sort is easy to understand and implement.


2. Stable sort .
3. It does not require any additional memory space.

Cons:

1. Bubble sort has a time complexity of O(n2) which makes it slow .


2. Inefficient for large datasets.
3. Bubble sort is a comparison-based sorting algorithm, which means that it requires a comparison operator to
determine the relative order of elements in the input data set. It can limit the efficiency of the algorithm in
certain cases.

Applications:
1. Educational purposes for teaching sorting logic.
2. Small datasets where simplicity is key.

2. Selection Sort

Pros:
1. Simple to understand and implement.
2. Requires minimal swaps .
3. Requires only a constant O(1) extra memory space .

Cons:

1. Selection sort has a time complexity of O(n2) makes it slower compared to other sorting algorithms.
2. Not stable.

3. Does not maintain the relative order of equal elements.

Applications:
1. Good for small datasets.
2. Used when swap cost is expensive.
3. Ideal for teaching basic sorting concepts .

3. Insertion Sort

Pros:
1. Simple and intuitive.
2. If the array is nearly sorted, the algorithm performs fewer comparisons and shifts, making it faster than its
worst-case time complexity suggests .
3. Insertion sort is stable .

4. Space-efficient as it is an in-place algorithm.

Cons:
1. The time complexity is O(n2) in the worst and average case, which makes it very inefficient for large
datasets.
2. As the size of the array increases, the algorithm becomes slower because it has to perform many
comparisons and shifts for each element.

Applications:

1. It is an excellent choice for learning purposes.


2. Can be useful when array is already almost sorted.

4. Merge Sort

Pros:
1. Consistent performance O (n log n).
2. Stable sort.
3. Works well with large datasets.
4. It is a divide-and-conquer algorithm that makes it easier to solve problems.

Cons:
1. Requires additional memory O (n).
2. Slower for small datasets.

Applications:
1. Sorting linked lists.
2. External sorting for large datasets that don’t fit in memory.
3. Merge Sort and its variations are used in library methods of programming languages.

5. Quick Sort

Pros:
1. It is a divide-and-conquer algorithm that makes it easier to solve problems.
2. Very fast on average O(n log n) .
3. In-place sorting .
4. Performs well with large datasets.
Cons:
1. Worst-case performance O(n2) when pivot choice is poor.
2. It is not a stable sort, meaning that if two elements have the same key, their relative order will not be
preserved in the sorted output in case of quick sort, because here we are swapping elements according to
the pivot’s position without considering their original positions .
3. It is not a good choice for small data sets.
Applications:
1. General-purpose sorting.
2. Sorting arrays where memory usage needs to be minimized.
3. Important in theoretical computer science for analyzing average-case complexity and developing
new techniques.
4. Applied in cryptography for generating random permutations and unpredictable encryption keys.

6. Bucket Sort

Pros:
1. Very fast for uniformly distributed data O(n).
2. Easy to parallelize.

Cons:
1. Requires prior knowledge of data distribution, as this information is needed to determine the size and
number of buckets needed.
2. Not suitable for large ranges of data.

Applications:
1. Sorting floating-point numbers or integers within a known range.
2. Histogram sorting.

7. Counting Sort
Pros:
1. Linear time complexity O(n + k) for small ranges.
2. Stable sort.
3. Counting sort is easy to code.

Cons:
1. Counting sort doesn’t work on decimal values.

2. Counting sort is inefficient if the range of values to be sorted is very large.


3. Counting sort is not an In-place sorting algorithm, It uses extra space for sorting the array elements.
Applications:
1. It is a commonly used algorithm for the cases where we have limited range items. For example, sort
students by grades, sort a events by time, days, months, years, etc.
2. Used in radix sort as a subroutine.
3. The idea of counting sort is used in Bucket Sort to divide elements into different buckets.

8. Radix Sort

Pros:
1. Linear time complexity O(n⋅k)for small keys.
2. Stable sort.

Cons:
1. Requires a stable sub-sorting algorithm (e.g., counting sort).
2. Works only for data that can be digitized.

Applications:
1. Sorting strings, integers, or large datasets with uniform keys.

9. Heap Sort

Pros:

1. Consistent O(nlog⁡n)O(n \log n)O(nlogn) performance.


2. In-place sorting.

Cons:

1. Not stable.
2. Slower compared to quicksort in practice due to cache inefficiency.

Applications:

1. Priority queues.
2. Sorting in scenarios where memory usage is critical.

You might also like