Sunday 15 January 2012

c# - How does Binary Insertion Sort Work? -



c# - How does Binary Insertion Sort Work? -

i know how binary search works, , know how insertion sort works code binary insertion sort , have problem in understanding how works.

static void main(string[] args) { int[] b = binarysort(new[] { 4, 3, 7, 1, 9, 6, 2 }); foreach (var in b) console.writeline(i); } public static int[] binarysort(int[] list) { (int = 1; < list.length; i++) { int low = 0; int high = - 1; int temp = list[i]; //find while (low <= high) { int mid = (low + high) / 2; if (temp < list[mid]) high = mid - 1; else low = mid + 1; } //backward shift (int j = - 1; j >= low; j--) list[j + 1] = list[j]; list[low] = temp; } homecoming list; }

i don't understand part do:

//backward shift (int j = - 1; j >= low; j--) list[j + 1] = list[j]; list[low] = temp;

and purpose of using binary search here? can tell me how binary insertion sort works? (c# console)

code source:http://w3mentor.com/learn/asp-dot-net-c-sharp/asp-dot-net-language-basics/binary-insertion-sort-in-c-net/

binary insertion sort works insertion sort, separates locating insertion point actual insertion.

insertion sort implemented array move items @ same time locating insertion point. while looping through items find insertion point, shift items create room insertion.

binary insertion sort create utilize of fact items sorted sorted, can utilize binary search find insertion point. binary search can't shift items create room insertion, has done in separate step after insertion point has been found.

the code wanted explained code shifts items create room insertion.

c# sorting search

No comments:

Post a Comment