Sorting Algorithms - Overview

 

📚 What is Sorting?

Sorting means arranging data elements (numbers, words, records, etc.) in a particular order — usually ascending (small to large) or descending (large to small).

For example:

  • Unsorted: [5, 2, 9, 1, 6]

  • Sorted (Ascending): [1, 2, 5, 6, 9]

  • Sorted (Descending): [9, 6, 5, 2, 1]

Sorting is very important because searching becomes faster, and organized data is easier to use.


📋 What are Sorting Algorithms?

Sorting algorithms are step-by-step methods or procedures to rearrange elements into a specific order.

They differ based on:

  • Time complexity (speed)

  • Space complexity (memory usage)

  • Technique used (comparison-based, divide and conquer, etc.)

In-Place Sorting

  • Definition: Sorting that does not use extra space (or uses very little extra memory, like a few variables).

  • The sorting happens within the given array/list itself by modifying it.

  • It saves memory.

✅ No extra array used (or very minimal extra memory).

Example:

  • Bubble Sort, Insertion Sort, Selection Sort, and Quick Sort are in-place sorting algorithms.

Internal Sorting

  • Definition: When all data to be sorted can fit into main memory (RAM), it is called internal sorting.

  • Sorting is completely done in memory.

  • No need to read/write from disk during sorting.

✅ Entire data is in RAM.

Example:

  • You have an array of 1,000 numbers — you sort them fully in memory using any algorithm (like Insertion  Sort or Quick Sort).

  • All in RAM → it's internal sorting.

External Sorting

  • Definition: When data is too large to fit into main memory (RAM), and sorting involves disk operations, it's called external sorting.

  • Data is partially loaded into memory, sorted in pieces, then combined.

❗ Used when data is larger than available RAM.

Example:

  • Huge databases (hundreds of GBs).

  • Sorting a 1 TB file on a computer with 16 GB RAM.

  • Techniques like External Merge Sort are used.

What is Stable Sorting?

Stable sorting means:

✅ If two elements are equal, they keep their original order after sorting.

A sorting algorithm is called stable if it preserves the relative order of records with equal keys.


📖 Example:

Suppose you have a list of students:

NameMarks
John80
Alice70
Bob80
Charlie60
Now, sort by Marks (ascending).

If stable sort is used, John (80) will stay before Bob (80) — because originally John came before Bob.

After stable sorting:

NameMarks
Charlie60
Alice70
John80
Bob80
Order among equal Marks is preserved.

🧠 Why Stability is Important?

  • In some applications (like multi-level sorting — first by marks, then by name), stability is critical.

  • Stability helps maintain earlier ordering when needed.


🔥 Which Algorithms are Stable?

Stable AlgorithmsUnstable Algorithms
    Bubble Sort    Selection Sort
    Insertion Sort    Quick Sort (standard)
    Merge Sort (standard version)    Heap Sort
    Counting Sort

🎯 Quick Tip:

  • If you need to preserve original order of equal elements, use a stable sort.

  • If order doesn't matter, you can use any sorting algorithm.



🎯 Why Sorting Algorithms are Important?

  • Efficient searching (especially Binary Search).

  • Data analysis.

  • Optimizing other algorithms.

  • Making reports, charts, or ordered displays.


🔥 Common Types of Sorting Algorithms:

AlgorithmIdeaTime Complexity (Best/Average/Worst)
Bubble SortRepeatedly swap adjacent elements if they are in wrong order        O(n) / O(n²) / O(n²)
Selection SortSelect the minimum (or maximum) element and move it to correct place        O(n²) / O(n²) / O(n²)
Insertion SortInsert each element into its correct position        O(n) / O(n²) / O(n²)
Merge SortDivide and Conquer (divide list into halves, sort, merge)O(n log n) / O(n log n) / O(n log n)
Quick SortDivide and Conquer (choose pivot, partition elements)    O(n log n) / O(n log n) / O(n²)
Heap SortBuild a heap and repeatedly remove largest elementO(n log n) / O(n log n) / O(n log n)
Counting SortCount number of occurrences of elements (only for integers)    O(n+k) / O(n+k) / O(n+k)
Radix SortSort based on individual digits/characters    O(nk) / O(nk) / O(nk)
       

     n = number of elements
     k = range of input values

Quick Comparison Table


AspectIn-place SortingInternal SortingExternal Sorting
Memory UsageVery little (almost none)Enough to hold all data in RAMRAM + Disk
Data LocationIn the same arrayEntire data in RAMData split between RAM and Disk
Example AlgorithmsBubble, Insertion, Selection SortMerge Sort (small arrays), Heap SortExternal Merge Sort, Polyphase Merge

Comments

Popular posts from this blog

Data Structures Lab PCCSL307 KTU 2024 Scheme - Dr Binu V P

Sparse Matrix - Transpose and Addition

Polynomial Addition using Arrays