Data Structure
Data Structure
1. Swap Function
void swap(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
2. Partition Function
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // last element is pivot
int i = low - 1;
for (int j = low; j < high; j++) {
Explanation:
3. QuickSort Function
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high); // get pivot
index
Explanation:
• We use the partition function to get the pivot index.
• Then we recursively sort:
4. Main Function
int main() {
int arr[] = { 10, 7, 8, 9, 1, 5 };
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
return 0;
}
// Main function
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
return 0;
}
Detailed Explanation
It:
2. merge() Function
void merge(int arr[], int left, int mid, int right)
int i = 0, j = 0, k = left;
Initialize pointers:
Compare elements of both arrays, insert the smaller one into original array.
if (L[i] <= R[j])
arr[k] = L[i++];
else
arr[k] = R[j++];
k++;
3. mergeSort() Function
void mergeSort(int arr[], int left, int right)
If array has more than one element, split it into two parts.
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
4. main() Function
int arr[] = {12, 11, 13, 5, 6, 7};
Print the original array, sort it using mergeSort, then print the sorted array.
Insertion Sort :
Idea:
Insertion Sort builds the final sorted array one element at a time by comparing and inserting the current
element into its correct position among the already sorted elements.
Steps:
1. Start from the second element (index 1) assuming the first element is already "sorted".
2. Compare it with elements on its left.
3. Shift all greater elements one position to the right.
4. Insert the current element into the correct position.
5. Repeat until the entire array is sorted.
Example:
Pass 1: (key = 2)
Compare with 5 → shift 5 → Insert 2
→ [2, 5, 4, 6, 1, 3]
Pass 2: (key = 4)
Compare with 5 → shift 5 → Insert 4
→ [2, 4, 5, 6, 1, 3]
Pass 3: (key = 6)
Already in correct place
→ [2, 4, 5, 6, 1, 3]
Pass 4: (key = 1)
Compare with 6, 5, 4, 2 → shift all
→ Insert 1
→ [1, 2, 4, 5, 6, 3]
Pass 5: (key = 3)
Compare with 6, 5, 4 → shift all → insert 3
→ [1, 2, 3, 4, 5, 6]
Time Complexity:
Here’s the C++ code for Insertion Sort with comments to help you understand each step:
#include <iostream>
using namespace std;
// Main function
int main() {
int arr[] = {5, 2, 4, 6, 1, 3};
int n = sizeof(arr) / sizeof(arr[0]);
insertionSort(arr, n);
return 0;
}
Output:
Original array: 5 2 4 6 1 3
Sorted array: 1 2 3 4 5 6
What is a Greedy Algorithm?
Imagine you're solving a problem and, at every step, you simply choose what looks
best at that moment, without thinking too much about the future. That’s the greedy
approach.
It’s like eating your favorite food from a plate — you pick the tastiest piece first
every time, hoping that by doing that, the entire meal will be enjoyable.
Say you want to make 37 cents, and you have coins of 25, 10, 5, and 1.
It works here because this coin system allows the greedy method to give the best
solution.
Suppose you have several activities with start and end times, and you want to attend
as many as possible without time clashes.
Let’s say:
The greedy idea here is to pick the activity that ends earliest so you can have more
free time left for the next ones.
So:
• Pick Activity 1
• Skip Activity 2 (overlaps)
• Pick Activity 3
• Pick Activity 4
So you do 3 activities, which is the best possible. Greedy works perfectly here.
1. Choosing the best step now leads to the best overall answer. (Greedy Choice
Property)
2. The smaller parts of the problem help build the full answer. (Optimal
Substructure)
Suppose your coins are: 1, 3, and 4 cents, and you want to make 6 cents.
• 3 + 3 → only 2 coins
So greedy fails here — because the best short-term choice (4) doesn’t help us get
the best total.
In a Nutshell:
1. 0/1 Knapsack: You either take the whole item or leave it (no splitting).
2. Fractional Knapsack: You can take a part of an item (used with greedy).
Items:
A 10 60
B 20 100
C 30 120
Solution:
Knapsack weight = 50
Items:
A 10 60 6.0
B 20 100 5.0
C 30 120 4.0
Greedy Approach:
Steps:
Total weight = 10 + 20 + 20 = 50
Total value = 60 + 100 + 80 = 240