DEV Community

Cover image for Sorting Algorithms
Mohith
Mohith

Posted on

Sorting Algorithms

– My Learning and Approach

In this session, I learned about different sorting algorithms. At first, I thought sorting is just arranging numbers, but later I understood that how we sort matters a lot, especially when the data size is large.

Here I am writing my understanding and the way I approached each sorting method.


Why Sorting is Needed

Before going into algorithms, I understood that sorting is useful because:

  • It makes searching faster
  • It helps to organize data clearly
  • Many problems depend on sorted data

1. Bubble Sort – My Approach

How I Thought

First, I tried to compare each element with the next one. If the current element is greater, I swap it. By doing this again and again, the largest element automatically moves to the end.

What I Observed

After each iteration, one element gets fixed at the correct position.

Code

public void bubbleSort(int[] arr) {
    int n = arr.length;

    for (int i = 0; i < n - 1; i++) {
        boolean swapped = false;

        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                swapped = true;
            }
        }

        if (!swapped) break;
    }
}
Enter fullscreen mode Exit fullscreen mode

My Understanding

  • Easy to understand
  • Not efficient for large inputs

2. Selection Sort – My Approach

How I Thought

Instead of swapping repeatedly, I thought why not find the smallest element and place it at the beginning.

What I Did

  • Find minimum in the array
  • Swap it with the first element
  • Repeat for the rest

Code

public void selectionSort(int[] arr) {
    int n = arr.length;

    for (int i = 0; i < n - 1; i++) {
        int minIndex = i;

        for (int j = i + 1; j < n; j++) {
            if (arr[j] < arr[minIndex]) {
                minIndex = j;
            }
        }

        int temp = arr[minIndex];
        arr[minIndex] = arr[i];
        arr[i] = temp;
    }
}
Enter fullscreen mode Exit fullscreen mode

My Understanding

  • Less swaps compared to bubble sort
  • Still takes O(n²) time

3. Insertion Sort – My Approach

How I Thought

I imagined sorting cards in hand. I take one element and place it in the correct position among already sorted elements.

What I Did

  • Pick one element
  • Compare with previous elements
  • Shift them and insert in correct place

Code

public void insertionSort(int[] arr) {
    int n = arr.length;

    for (int i = 1; i < n; i++) {
        int key = arr[i];
        int j = i - 1;

        while (j >= 0 && arr[j] > key) {
            arr[j + 1] = arr[j];
            j--;
        }

        arr[j + 1] = key;
    }
}
Enter fullscreen mode Exit fullscreen mode

My Understanding

  • Works well for small or nearly sorted arrays
  • Simple and intuitive

4. Merge Sort – My Approach

How I Thought

Handling the whole array was difficult, so I divided the problem into smaller parts.

What I Did

  • Divide array into two halves
  • Sort each half
  • Merge them back

Code

public void mergeSort(int[] arr, int left, int right) {
    if (left < right) {
        int mid = (left + right) / 2;

        mergeSort(arr, left, mid);
        mergeSort(arr, mid + 1, right);

        merge(arr, left, mid, right);
    }
}

private void merge(int[] arr, int left, int mid, int right) {
    int n1 = mid - left + 1;
    int n2 = right - mid;

    int[] leftArr = new int[n1];
    int[] rightArr = new int[n2];

    for (int i = 0; i < n1; i++)
        leftArr[i] = arr[left + i];

    for (int j = 0; j < n2; j++)
        rightArr[j] = arr[mid + 1 + j];

    int i = 0, j = 0, k = left;

    while (i < n1 && j < n2) {
        if (leftArr[i] <= rightArr[j]) {
            arr[k++] = leftArr[i++];
        } else {
            arr[k++] = rightArr[j++];
        }
    }

    while (i < n1) arr[k++] = leftArr[i++];
    while (j < n2) arr[k++] = rightArr[j++];
}
Enter fullscreen mode Exit fullscreen mode

My Understanding

  • Efficient for large inputs
  • Uses extra space

5. Quick Sort – My Approach

How I Thought

Instead of dividing equally, I thought of placing one element in its correct position and arranging others around it.

What I Did

  • Pick a pivot
  • Place smaller elements on left
  • Place larger elements on right
  • Repeat recursively

Code

public void quickSort(int[] arr, int low, int high) {
    if (low < high) {
        int pi = partition(arr, low, high);

        quickSort(arr, low, pi - 1);
        quickSort(arr, pi + 1, high);
    }
}

private int partition(int[] arr, int low, int high) {
    int pivot = arr[high];
    int i = low - 1;

    for (int j = low; j < high; j++) {
        if (arr[j] < pivot) {
            i++;
            int temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
        }
    }

    int temp = arr[i + 1];
    arr[i + 1] = arr[high];
    arr[high] = temp;

    return i + 1;
}
Enter fullscreen mode Exit fullscreen mode

My Understanding

  • Faster in most cases
  • Worst case can be slow

Final Reflection

From this session, I understood that:

  • Simple algorithms help in building logic
  • Efficient algorithms help in real-world problems
  • Choosing the right algorithm depends on the situation

Overall, this session helped me improve my problem-solving thinking and gave me clarity on how different sorting techniques work internally.

Top comments (0)