Total Pageviews
Saturday, June 6, 2020
DEPT_LOCATION TABLE DATA INSERTION (COMPANY DATABASE NAVATHE BOOK)
PROJECT TABLE DATA INSERTION (COMPANY DATABASE NAVATHE BOOK)
EMPLOYEE TABLE DATA INSERTION ( COMPANY DATABASE NAVATHE BOOK)
DEPARTMENT TABLE DATA INSERTION ( COMPANY DATABASE NAVATHE BOOK )
COMPANY DATABASE NAVATHE BOOK
COMPANY DATABASE NAVATHE BOOK
![]() |
Thursday, June 4, 2020
DYSTRAS ALGORITHM OR DIJKSTRAS ALGORITHM STEP BY STEP EXPLANATION
QUICK SORT algorithm implementation example step by step
QUICK SORT algorithm implementation example step by step
selection sort example step by step
selection sort example step by step explanation
🎯 What is Selection Sort?
Selection Sort is a simple sorting algorithm that repeatedly selects the smallest element from the unsorted part of the array and places it at the beginning.
📌 Definition
Selection Sort is a comparison-based sorting algorithm in which the smallest element is selected from the unsorted portion and swapped with the first unsorted element.
🧠 How Selection Sort Works
Steps:
- Find the smallest element in the array
- Swap it with the first element
- Move to next position
- Repeat for remaining unsorted part
- Continue until array is sorted
📊 Example
Unsorted Array:
A = [64, 25, 12, 22, 11]
🔁 Pass 1
Smallest = 11
Swap with first element:
[11, 25, 12, 22, 64]
🔁 Pass 2
Remaining array: [25, 12, 22, 64]
Smallest = 12
[11, 12, 25, 22, 64]
🔁 Pass 3
Remaining array: [25, 22, 64]
Smallest = 22
[11, 12, 22, 25, 64]
🔁 Pass 4
Remaining array: [25, 64]
Already in order
[11, 12, 22, 25, 64]
📌 Final Sorted Array
[11, 12, 22, 25, 64]
💻 Algorithm (Pseudocode)
SelectionSort(A, n)
1. for i = 0 to n-1
2. minIndex = i
3. for j = i+1 to n-1
4. if A[j] < A[minIndex]
5. minIndex = j
6. swap(A[i], A[minIndex])
🐍 Python Program for Selection Sort
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
# Example
arr = [64, 25, 12, 22, 11]
result = selection_sort(arr)
print("Sorted Array:", result)
Output:
Sorted Array: [11, 12, 22, 25, 64]
⚡ Time Complexity
| Case | Complexity |
|---|---|
| Best Case | O(n²) |
| Worst Case | O(n²) |
| Average Case | O(n²) |
📊 Space Complexity
- O(1) (in-place sorting)
🆚 Selection Sort vs Bubble Sort
| Feature | Selection Sort | Bubble Sort |
|---|---|---|
| Method | Select minimum | Swap adjacent |
| Swaps | Fewer swaps | Many swaps |
| Speed | Slightly faster | Slower |
| Stability | ❌ Not stable | ✅ Stable |
| Complexity | O(n²) | O(n²) |
📌 Advantages
- Simple to understand
- Performs fewer swaps than bubble sort
- Works in-place (no extra memory)
❌ Disadvantages
- Very slow for large data sets
- Always O(n²) time complexity
- Not stable sorting algorithm
🧠 Real Life Example
- Selecting smallest student roll number and placing first 🧑🎓
- Sorting cards by repeatedly picking smallest card 🃏
📌 Summary
- Selection sort selects the smallest element repeatedly
- Places it at the correct position
- Performs O(n²) comparisons
- Efficient for small datasets only
❓ Important Questions
Short Questions
- What is selection sort?
- What is the main idea of selection sort?
- What is time complexity of selection sort?
- Is selection sort stable?
- How many swaps are used in selection sort?
Long Questions
- Explain selection sort with example.
- Write algorithm of selection sort.
- Write Python program for selection sort.
- Compare selection and bubble sort.
- Explain advantages and disadvantages of selection sort.
Tuesday, June 2, 2020
INSERTION SORT ALGORITHM STEP BY STEP
🎯 What is Insertion Sort?
Insertion Sort is a simple sorting algorithm that builds the final sorted array one element at a time by inserting each element into its correct position in the already sorted part.
📌 Definition
Insertion Sort is a comparison-based algorithm in which each element is picked and inserted into its correct position in a sorted subarray.
🧠 How Insertion Sort Works
Steps:
- Start from the second element
- Compare it with elements before it
- Shift larger elements one position right
- Insert the element in correct position
- Repeat for all elements
📊 Example
Unsorted Array:
A = [12, 11, 13, 5, 6]
🔁 Step-by-step
Pass 1 (Insert 11)
Compare 11 with 12 → shift 12
[11, 12, 13, 5, 6]
Pass 2 (Insert 13)
Already in correct position
[11, 12, 13, 5, 6]
Pass 3 (Insert 5)
Shift 13, 12, 11
[5, 11, 12, 13, 6]
Pass 4 (Insert 6)
Shift 13, 12, 11
[5, 6, 11, 12, 13]
📌 Final Sorted Array
[5, 6, 11, 12, 13]
💻 Algorithm (Pseudocode)
InsertionSort(A, n)
1. for i = 1 to n-1
2. key = A[i]
3. j = i - 1
4. while j >= 0 AND A[j] > key
5. A[j + 1] = A[j]
6. j = j - 1
7. A[j + 1] = key
🐍 Python Program for Insertion Sort
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
# Example
arr = [12, 11, 13, 5, 6]
result = insertion_sort(arr)
print("Sorted Array:", result)
Output:
Sorted Array: [5, 6, 11, 12, 13]
⚡ Time Complexity
| Case | Complexity |
|---|---|
| Best Case | O(n) |
| Worst Case | O(n²) |
| Average Case | O(n²) |
📊 Space Complexity
- O(1) (in-place sorting)
🆚 Insertion Sort vs Other Sorting
| Feature | Insertion Sort | Bubble Sort | Selection Sort |
|---|---|---|---|
| Method | Insert element in correct position | Swap adjacent elements | Select minimum element |
| Best Case | O(n) | O(n) | O(n²) |
| Efficiency | Good for small/nearly sorted data | Poor | Poor |
| Stability | ✅ Stable | ✅ Stable | ❌ Not stable |
📌 Advantages
- Simple to implement
- Efficient for small or nearly sorted data
- Stable sorting algorithm
- Works in-place (no extra memory)
❌ Disadvantages
- Slow for large datasets
- O(n²) complexity in worst case
🧠 Real Life Example
- Arranging playing cards in hand 🃏
- Inserting a new book in a sorted bookshelf 📚
📌 Summary
- Insertion Sort builds a sorted list step by step
- Each element is inserted in correct position
- Best performance when data is already nearly sorted
- Simple but not suitable for large datasets
❓ Important Questions
Short Questions
- What is insertion sort?
- What is key element in insertion sort?
- What is best case complexity?
- Is insertion sort stable?
- Give one real-life example.
Long Questions
- Explain insertion sort with example.
- Write algorithm of insertion sort.
- Write Python program for insertion sort.
- Compare insertion sort with bubble and selection sort.
- Explain advantages and disadvantages of insertion sort.
