Embed presentation
Downloaded 1,141 times





![public insertionSort(int[] arr)
{
for (int i = 1; i < arr.Length; ++i)
{
int temp = arr[i]; Select
int pos = i;
while (arr[pos-1].CompareTo(temp) > 0 && pos > 0)
{
Comparing
arr[pos] = arr[pos-1];
pos--;
} Shift
arr[pos] = temp;
}
} Insert](https://image.slidesharecdn.com/insertionsort-111211174551-phpapp01/85/Insertion-sort-6-320.jpg)



Insertion sort works by iterating through an array, inserting each element into its sorted position by shifting other elements over. It finds the location where each element should be inserted into the sorted portion using a linear search, moving larger elements out of the way to make room. This sorting algorithm is most effective for small data sets and can be implemented recursively or iteratively through comparisons and shifts.





![public insertionSort(int[] arr)
{
for (int i = 1; i < arr.Length; ++i)
{
int temp = arr[i]; Select
int pos = i;
while (arr[pos-1].CompareTo(temp) > 0 && pos > 0)
{
Comparing
arr[pos] = arr[pos-1];
pos--;
} Shift
arr[pos] = temp;
}
} Insert](https://image.slidesharecdn.com/insertionsort-111211174551-phpapp01/85/Insertion-sort-6-320.jpg)


