Forem

ARUL SELVI ML
ARUL SELVI ML

Posted on

sorting

šŸ”¹ 1. Bubble Sort

šŸ’” Concept:

Bubble Sort repeatedly compares adjacent elements and swaps them if they are in the wrong order.

āš™ļø How it Works:

  • Compare first and second elements
  • Swap if needed
  • Continue for the entire array
  • Repeat until no swaps are needed

ā±ļø Complexity:

  • Time: O(n²)
  • Space: O(1)

āœ… Best For:

  • Small datasets
  • Learning purposes

šŸ”¹ 2. Selection Sort

šŸ’” Concept:

Find the minimum element and place it at the beginning.

āš™ļø Steps:

  • Find the smallest element in the array
  • Swap it with the first element
  • Repeat for the remaining array

ā±ļø Complexity:

  • Time: O(n²)
  • Space: O(1)

āœ… Best For:

  • Simple implementations

šŸ”¹ 3. Insertion Sort

šŸ’” Concept:

Build the sorted array one element at a time.

āš™ļø Steps:

  • Take one element
  • Insert it into its correct position in the sorted part

ā±ļø Complexity:

  • Time: O(n²), Best case: O(n)
  • Space: O(1)

āœ… Best For:

  • Nearly sorted arrays

šŸ”¹ 4. Merge Sort

šŸ’” Concept:

Divide and conquer algorithm.

āš™ļø Steps:

  • Divide array into halves
  • Recursively sort each half
  • Merge sorted halves

ā±ļø Complexity:

  • Time: O(n log n)
  • Space: O(n)

āœ… Best For:

  • Large datasets
  • Stable sorting

šŸ”¹ 5. Quick Sort

šŸ’” Concept:

Pick a pivot and partition the array around it.

āš™ļø Steps:

  • Choose a pivot
  • Place smaller elements to left, larger to right
  • Recursively apply to subarrays

ā±ļø Complexity:

  • Average: O(n log n)
  • Worst: O(n²)

āœ… Best For:

  • Fast practical sorting

šŸ”¹ 6. Heap Sort

šŸ’” Concept:

Uses a binary heap structure.

āš™ļø Steps:

  • Build a max heap
  • Extract maximum repeatedly

ā±ļø Complexity:

  • Time: O(n log n)
  • Space: O(1)

āœ… Best For:

  • Memory-efficient sorting

šŸ”¹ 7. Counting Sort

šŸ’” Concept:

Counts frequency of elements.

āš™ļø Steps:

  • Count occurrences
  • Place elements accordingly

ā±ļø Complexity:

  • Time: O(n + k)
  • Space: O(k)

āœ… Best For:

  • Small range integers

šŸ”¹ 8. Radix Sort

šŸ’” Concept:

Sort numbers digit by digit.

āš™ļø Steps:

  • Sort based on least significant digit
  • Move to most significant digit

ā±ļø Complexity:

  • Time: O(nk)
  • Space: O(n + k)

āœ… Best For:

  • Large numbers

Comparison Table

Algorithm Time Complexity Space Stable
Bubble Sort O(n²) O(1) Yes
Selection Sort O(n²) O(1) No
Insertion Sort O(n²) O(1) Yes
Merge Sort O(n log n) O(n) Yes
Quick Sort O(n log n) O(log n) No
Heap Sort O(n log n) O(1) No
Counting Sort O(n + k) O(k) Yes
Radix Sort O(nk) O(n+k) Yes

Top comments (0)