DEV Community

Lokeshwaran S
Lokeshwaran S

Posted on

Different Sorting Methodologies - CA17

My Thinking and Approach


Introduction

In this session, I learned different sorting algorithms that are used to arrange data in a specific order.

Sorting is important because it helps in organizing data and improving efficiency in searching.


Problem Statement

  • Understand different sorting techniques
  • Learn how each algorithm works
  • Analyze their performance

My Initial Thought

At first, I thought:

  • Sorting is simple arrangement
  • All methods work similarly

But I realized:

  • Each algorithm has a different approach
  • Efficiency varies

Key Observation

  • Some algorithms are easy but slow
  • Some are complex but efficient
  • Choice depends on problem size

Sorting Methodologies

1. Bubble Sort

Logic:

  • Compare adjacent elements
  • Swap if they are in wrong order

Code (Python)

```python id="bub12"
def bubble_sort(arr):
n = len(arr)

for i in range(n):
    for j in range(0, n - i - 1):
        if arr[j] > arr[j + 1]:
            arr[j], arr[j + 1] = arr[j + 1], arr[j]

return arr
Enter fullscreen mode Exit fullscreen mode



Explanation:

* Repeatedly compares adjacent elements
* After each pass, largest element moves to the end

---

### 2. Selection Sort

### Logic:

* Find minimum element
* Place it at correct position

### Code (Python)



```python id="sel34"
def selection_sort(arr):
    n = len(arr)

    for i in range(n):
        min_index = i

        for j in range(i + 1, n):
            if arr[j] < arr[min_index]:
                min_index = j

        arr[i], arr[min_index] = arr[min_index], arr[i]

    return arr
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Selects smallest element
  • Swaps with current index
  • Builds sorted part step by step

3. Insertion Sort

Logic:

  • Insert each element in correct position

Code (Python)

```python id="ins56"
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1

    while j >= 0 and arr[j] > key:
        arr[j + 1] = arr[j]
        j -= 1

    arr[j + 1] = key

return arr
Enter fullscreen mode Exit fullscreen mode



Explanation:

* Takes one element at a time
* Places it in correct position in sorted part

---

### 4. Merge Sort

### Logic:

* Divide array
* Sort halves
* Merge

### Code (Python)



```python id="mer78"
def merge_sort(arr):
    if len(arr) <= 1:
        return arr

    mid = len(arr) // 2
    left = merge_sort(arr[:mid])
    right = merge_sort(arr[mid:])

    return merge(left, right)

def merge(left, right):
    result = []
    i = j = 0

    while i < len(left) and j < len(right):
        if left[i] < right[j]:
            result.append(left[i])
            i += 1
        else:
            result.append(right[j])
            j += 1

    result.extend(left[i:])
    result.extend(right[j:])

    return result
Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Divides array recursively
  • Merges sorted halves
  • Efficient for large data

5. Quick Sort

Logic:

  • Choose pivot
  • Partition array
  • Recursively sort

Code (Python)

```python id="qui90"
def quick_sort(arr):
if len(arr) <= 1:
return arr

pivot = arr[len(arr) // 2]

left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]

return quick_sort(left) + middle + quick_sort(right)
Enter fullscreen mode Exit fullscreen mode



Explanation:

* Divides array based on pivot
* Recursively sorts parts
* Faster in practice

---

## Complexity Analysis

| Algorithm      | Time Complexity |
| -------------- | --------------- |
| Bubble Sort    | O(n²)           |
| Selection Sort | O(n²)           |
| Insertion Sort | O(n²)           |
| Merge Sort     | O(n log n)      |
| Quick Sort     | O(n log n)      |

---

## Key Takeaways

* Sorting algorithms differ in efficiency
* Simple methods are easier but slower
* Advanced methods improve performance
* Choosing correct algorithm is important

---

## Conclusion

This session helped me understand different sorting methodologies and how to choose the right one based on the problem.

---
Enter fullscreen mode Exit fullscreen mode

Top comments (0)