All dsa sorting in one repository for quick revision .
Sorting ko broadly 2 tarah se samjho:
- Basic sorting: Bubble, Selection, Insertion
- Efficient sorting: Merge, Quick, Heap
- Special case: Counting Sort
- Practical C++:
sort()
Paas-paas ke elements compare karo. Agar order galat hai to swap.
Example:
[5, 2, 8, 1]
5 > 2 β swap
[2, 5, 8, 1]
8 > 1 β swap
[2, 5, 1, 8]
Next round:
[2, 1, 5, 8]
Final:
[1, 2, 5, 8]
Real projects mein generally Bubble Sort use nahi karte.
Mostly:
- Sorting ka basic concept samajhne ke liye
- Beginner/interview mein algorithm implementation poochne ke liye
Best: O(n)
Average: O(nΒ²)
Worst: O(nΒ²)
Space: O(1)
Q1. Bubble Sort ka main idea kya hai?
π Adjacent elements compare karke wrong order hone par swap karna.
Q2. Bubble Sort ko optimize kaise kar sakte hain?
π swapped flag use karke. Agar kisi pass mein swap nahi hua, array already sorted hai.
Har round mein minimum element find karo aur beginning mein rakh do.
Example:
[5, 2, 8, 1, 3]
Minimum = 1
[1, 2, 8, 5, 3]
Ab remaining mein minimum = 2
[1, 2, 8, 5, 3]
Next minimum = 3
[1, 2, 3, 5, 8]
- Basic sorting understanding
- Jab swaps kam rakhne ki need ho, selection sort useful concept hai
- Mostly educational/interview implementation
Best = Average = Worst = O(nΒ²)
Space = O(1)
Q. Selection Sort aur Bubble Sort mein difference?
π Bubble adjacent elements ko repeatedly swap karta hai, Selection minimum ko find karke correct position par swap karta hai.
Ye important hai.
Playing cards arrange karte waqt:
5
Phir 2 mila:
2 5
Phir 8:
2 5 8
Phir 1:
1 2 5 8
Nearly sorted data ke liye bahut useful.
Example:
[1, 2, 3, 5, 4, 6, 7]
Array almost sorted hai. Insertion Sort efficiently 4 ko correct position par le aa sakta hai.
Best: O(n)
Average: O(nΒ²)
Worst: O(nΒ²)
Space: O(1)
Q. Nearly sorted array ke liye kaunsi sorting choose karoge?
π Insertion Sort
Q. Insertion Sort ka best case O(n) kyun hai?
π Jab array already sorted ho, har element ko bas ek comparison ke around process karna padta hai.
Ye placement/interview ke liye very important hai.
Divide and Conquer
Example:
[8, 3, 2, 9, 7, 1]
Divide:
[8, 3, 2] [9, 7, 1]
Again:
[8] [3,2] [9] [7,1]
Finally single elements:
[8] [3] [2] [9] [7] [1]
Ab merge karte waqt sort:
[3,8] [2,9] [1,7]
Then:
[2,3,8] [1,7,9]
Finally:
[1,2,3,7,8,9]
Merge Sort useful hai jab:
- Guaranteed
O(n log n)chahiye - Stable sorting chahiye
- Linked List sorting
- External sorting / large data scenarios
Best: O(n log n)
Average: O(n log n)
Worst: O(n log n)
Space: O(n)
Q. Merge Sort ka concept kya hai?
π Divide array into halves, recursively sort them, then merge sorted halves.
Q. Merge Sort ki time complexity hamesha O(n log n) kyun hoti hai?
π Array repeatedly half hota hai β log n levels, aur har level par total n elements process hote hain.
So:
n Γ log n = O(n log n)
Q. Merge Sort ka disadvantage?
π Extra O(n) space lagti hai.
Ye bhi bahut important interview sorting hai.
Pivot + Partition
Example:
[5, 2, 8, 1, 3]
Pivot = 5
5 se smaller:
[2, 1, 3]
5 se greater:
[8]
So:
[2,1,3] | 5 | [8]
Ab left part ko sort:
[1,2,3] | 5 | [8]
Final:
[1,2,3,5,8]
Quick Sort useful hai jab:
- Average-case fast sorting chahiye
- In-place sorting preferred ho
- Array data ho
Best: O(n log n)
Average: O(n log n)
Worst: O(nΒ²)
Q. Quick Sort mein pivot kya hota hai?
π Wo element jiske around array ko partition kiya jata hai.
Q. Quick Sort worst case kab hota hai?
π Jab repeatedly poor pivot choose ho, jaise already sorted array par certain pivot choices.
Q. Quick Sort aur Merge Sort mein difference?
π Quick Sort generally in-place hota hai aur average mein fast hota hai, while Merge Sort guaranteed O(n log n) deta hai but extra space leta hai.
Heap Sort mein Heap data structure use hota hai.
Example:
[5, 2, 8, 1, 3]
Max Heap banaya:
8
/ \
3 5
/ \
1 2
Largest element 8 ko end mein bhejte hain.
Repeat:
[1,2,3,5,8]
- Jab
O(n log n)guaranteed chahiye - Extra array space avoid karna ho
- Heap/Priority Queue based problems mein concept useful hai
Best: O(n log n)
Average: O(n log n)
Worst: O(n log n)
Space: O(1)
Q. Heap Sort ki biggest advantage?
π Worst case bhi O(n log n) aur in-place sorting possible.
Q. Heap Sort ka connection kis data structure se hai?
π Binary Heap.
Ye thodi different hai.
Ye elements ko compare nahi karta, frequency count karta hai.
Example:
[4, 2, 2, 3, 1, 4]
Count:
1 β 1
2 β 2
3 β 1
4 β 2
Result:
[1,2,2,3,4,4]
Jab values ka range chhota ho.
Example:
Student marks:
[10, 5, 7, 5, 10, 2]
Range 0β100 hai β Counting Sort useful ho sakta hai.
O(n + k)
k = range of values.
Q. Counting Sort kab use nahi karoge?
π Jab values ka range bahut bada ho.
Example:
[1, 999999999]
Is case mein huge counting array banana inefficient hoga.
Practical coding interview mein ye sabse important hai.
sort(arr.begin(), arr.end());Example:
vector<int> arr = {5, 2, 8, 1, 3};
sort(arr.begin(), arr.end());Output:
[1,2,3,5,8]
Descending:
sort(arr.begin(), arr.end(), greater<int>());Normal coding problems mein jab interviewer algorithm implement karne ko specifically nahi bol raha.
| Sorting | Main Idea | Best Use |
|---|---|---|
| Bubble | Adjacent swap | Learning/basic interview |
| Selection | Minimum select | Basic implementation |
| Insertion | Correct position insert | Nearly sorted array |
| Merge | Divide + Merge | Guaranteed O(n log n), stable sorting |
| Quick | Pivot + Partition | Fast average-case array sorting |
| Heap | Heap | O(n log n), in-place |
| Counting | Frequency | Small integer range |
sort() |
Library sorting | Normal coding problems |
- Implement Bubble Sort.
- Implement Selection Sort.
- Implement Insertion Sort.
- Sort array in ascending/descending order.
- Find largest and smallest after sorting.
- Implement Merge Sort.
- Implement Quick Sort.
- Merge two sorted arrays.
- Sort an array containing only
0, 1, 2. - Find kth smallest/largest element.
- Count inversions in an array.
- Find minimum swaps required to sort an array.
- Sort array according to frequency.
- Merge overlapping intervals.
- Sort characters according to frequency.
Interview mein question dekhkar sorting ko blindly mat lagana.
Example:
Array contains only
0, 1, 2. Sort it.
Normal:
sort()
kaam karega.
But interviewer pooch sakta hai:
"Can you do it in O(n)?"
Tab tumhe Dutch National Flag Algorithm yaad aana chahiye.
0 β left
1 β middle
2 β right
Ye bahut important placement question hai.
Tum is order mein padho:
Bubble
β
Selection
β
Insertion
β
Merge β
β
Quick β
β
Heap
β
Counting
β
STL sort()
β
Sorting Problems π₯
Sabse pehle Bubble Sort ko master karo. Uske baad main tumhe **Bubble Sort ka C++ code line-be, dry run,