Java sort algorithms

insertion sort:

algorithm: given an int array arr with length=n, define key=arr[i: i= 1~n-1], and need to put each key in the correct position, how? first save the key=arr[i], and compare the key with arr[j: j=0~i-1], if arr[j] is larger then move arr[j] to arr[j+1].

how it works: each time we define a key, we’ll need to compare the key with all its preceding elements, and the key’s preceding subarray is always in a increasing order.



Leave a Reply

Your email address will not be published. Required fields are marked *