Top Tweets for #8DaysofSorting
Day 2 of #8DaysofSorting
Today, we will learn about Insertion sort which stands out for its simplicity and effectiveness on nearly sorted or small datasets.
What is Insertion Sort?
Insertion sort is a comparison-based sorting algorithm that builds the final sorted array one element at a time.
It works by repeatedly taking an element from the unsorted portion of the array and inserting it into its correct position within the sorted portion.
How Insertion Sort Works?
A function called insertionSort is created which takes an array as input and sorts it using the insertion sort algorithm. The outer loop iterates through the array, starting from the second element (index 1) to the last element.
Inside the loop, the current element is stored in the variable key. This element will be inserted into its correct position in the sorted portion of the array.
The inner while loop compares the key with the elements in the sorted portion of the array, moving them one position to the right until the correct insertion point is found.
Once the insertion point is determined, the key element is placed in its correct position by assigning it to array [j + 1].
The process continues until all elements are iterated over, resulting in a fully sorted array.
(Video reference given below)
Time Complexity and Performance:
The time complexity of insertion sort is O(n²) in the worst and average case scenarios, where ’n’ is the number of elements in the array.
Code Optimization and Variations:
1. Early termination if the current element is already in its correct position:
During the inner while loop, if we find that array[j] is not greater than the key element, we can terminate the loop since the key element is already in its correct sorted position.
2. Binary search to find the insertion point instead of a linear search:
Instead of performing a linear search to find the correct position for the key element, we can utilize a binary search to find the insertion point.
Conclusion:
By grasping the inner workings of Insertion sort, I guess you have gained a valuable tool for sorting data efficiently. Remember to consider the nature and size of your dataset when choosing the appropriate sorting algorithm.
Last Seen Hashtags on Sotwe
lightskinpyt
Seen from United States
eldorina
Seen from United States
淫曲
Seen from Germany
レスキューカー
Seen from Colombia
EshghNemikhabeh
Seen from United Kingdom
islandpower
Seen from Peru
splitscreen
thickassdaphne
Seen from United States
soundcheck
Seen from United States
Rodamrix NSFW
Seen from United States
Trends for you
Most Popular Users

Elon Musk 
@elonmusk
240.1M followers

Barack Obama 
@barackobama
119.3M followers

Donald J. Trump 
@realdonaldtrump
111.6M followers

Cristiano Ronaldo 
@cristiano
108.9M followers

Narendra Modi 
@narendramodi
107M followers

Rihanna 
@rihanna
97.3M followers

NASA 
@nasa
92.1M followers

Justin Bieber 
@justinbieber
90.6M followers

KATY PERRY 
@katyperry
86.8M followers

Taylor Swift 
@taylorswift13
80.6M followers

Lady Gaga 
@ladygaga
72.1M followers

Kim Kardashian 
@kimkardashian
69.4M followers

YouTube 
@youtube
68.6M followers

Virat Kohli 
@imvkohli
68.5M followers

Bill Gates 
@billgates
63.4M followers

The Ellen Show
@theellenshow
62.5M followers

CNN 
@cnn
61.9M followers

Neymar Jr 
@neymarjr
61M followers

X 
@x
60.9M followers

CNN Breaking News 
@cnnbrk
59.9M followers
