Insert Sort Summary:
- The element that currently needs to be sorted (Array[i]), compared to the last element already sorted (Array[i-1]), and loops to the next element to be sorted if the condition continues to execute the subsequent program.
- Caches the value of the element that is currently being sorted so that the correct location is found for insertion.
- The sorted element is compared to the element that is already numbered, and moves backwards (ascending) larger than it is.
- The element to be sorted, inserted into the correct position.
Example code:
Array = [3, 4, 1, 6, 2, 9, 7, 0, 8, 5]#Insert_sort forIinchRange (1, Len (array)):ifARRAY[I-1] >Array[i]: Temp= Array[i]#elements that currently need to be sortedindex = i#used to record where the sort element needs to be inserted whileIndex > 0 andARRAY[INDEX-1] >Temp:array[index]= Array[index-1]#move the sorted element back one bit, leaving the position to be insertedIndex-= 1Array[index]= Temp#inserts the elements that need to be sorted into the specified position#print sort result.Print(array)
Python Insert Sort