logo

Binäre Einfügungssortierung

Die binäre Einfügungssortierung ist ein Sortieralgorithmus, der dem ähnelt Sortieren durch Einfügen , aber anstatt die lineare Suche zu verwenden, um die Position zu finden, an der ein Element eingefügt werden soll, verwenden wir binäre Suche . Somit reduzieren wir den Vergleichswert des Einfügens eines einzelnen Elements von O (N) auf O (log N).

Es handelt sich um einen flexiblen Algorithmus, was bedeutet, dass er schneller arbeitet, wenn dieselben gegebenen Mitglieder bereits stark sortiert sind, d. h. die aktuelle Position des Features näher an seiner tatsächlichen Position in der sortierten Liste liegt.



Es handelt sich um einen stabilen Filteralgorithmus – Elemente mit denselben Werten erscheinen in der gleichen Reihenfolge in der letzten Reihenfolge wie in der ersten Liste.

Anwendungen der binären Einfügungssortierung:

  • Die binäre Einfügungssortierung funktioniert am besten, wenn das Array eine geringere Anzahl von Elementen enthält.
  • Wenn Sie eine schnelle Sortierung oder Zusammenführungssortierung durchführen und die Subarray-Größe kleiner wird (z. B. <= 25 Elemente), ist es am besten, eine binäre Einfügungssortierung zu verwenden.
  • Dieser Algorithmus funktioniert auch, wenn der Aufwand für Vergleiche zwischen Schlüsseln hoch genug ist. Wenn wir beispielsweise mehrere Zeichenfolgen filtern möchten, ist die Vergleichsleistung der beiden Zeichenfolgen höher.

Wie funktioniert die binäre Einfügungssortierung?

  • Im binären Einfügungssortiermodus teilen wir dieselben Mitglieder in zwei Unterarrays auf – gefiltert und ungefiltert. Das erste Element derselben Mitglieder befindet sich im organisierten Subarray, alle anderen Elemente sind ungeplant.
  • Dann iterieren wir vom zweiten Element bis zum letzten. Bei der Wiederholung des i-ten machen wir das aktuelle Objekt zu unserem Schlüssel. Dieser Schlüssel ist eine Funktion, die wir zu unserer bestehenden Liste unten hinzufügen sollten.
  • Dazu verwenden wir zunächst eine binäre Suche im sortierten Subarray unten, um die Position eines Elements zu finden, das größer als unser Schlüssel ist. Nennen wir diese Position Pos. Anschließend verschieben wir alle Elemente nach rechts von pos auf 1 und erstellen Array[pos] = key.
  • Wir können feststellen, dass bei jeder i-ten Multiplikation der linke Teil des Arrays bis (i – 1) bereits sortiert ist.

Ansatz zur Implementierung der binären Einfügungssortierung:

  • Iterieren Sie das Array vom zweiten Element bis zum letzten Element.
  • Speichern Sie das aktuelle Element A[i] in einem variablen Schlüssel.
  • Finden Sie mithilfe der binären Suche die Position des Elements, das gerade größer als A[i] ist, im Subarray von A[0] bis A[i-1]. Angenommen, dieses Element befindet sich an der Indexposition.
  • Verschieben Sie alle Elemente vom Index pos nach i-1 nach rechts.
  • A[pos] = Schlüssel.

Nachfolgend finden Sie die Implementierung für den oben genannten Ansatz:

C++




string zu int





// C program for implementation of> // binary insertion sort> #include> using> namespace> std;> // A binary search based function> // to find the position> // where item should be inserted> // in a[low..high]> int> binarySearch(>int> a[],>int> item,> >int> low,>int> high)> {> >if> (high <= low)> >return> (item>a[niedrig]) ?> >(low + 1) : low;> >int> mid = (low + high) / 2;> >if> (item == a[mid])> >return> mid + 1;> >if> (item>a[mid])> >return> binarySearch(a, item,> >mid + 1, high);> >return> binarySearch(a, item, low,> >mid - 1);> }> // Function to sort an array a[] of size 'n'> void> insertionSort(>int> a[],>int> n)> {> >int> i, loc, j, k, selected;> >for> (i = 1; i { j = i - 1; selected = a[i]; // find location where selected should be inserted loc = binarySearch(a, selected, 0, j); // Move all elements after location to create space while (j>= loc) { a[j + 1] = a[j]; J--; } a[j + 1] = ausgewählt; } } // Treibercode int main() { int a[] = { 37, 23, 0, 17, 12, 72, 31, 46, 100, 88, 54 }; int n = sizeof(a) / sizeof(a[0]), i; insertSort(a, n); cout<<'Sorted array: '; for (i = 0; i cout <<' '<< a[i]; return 0; } // this code is contribution by shivanisinghss2110>

>

>

C




// C program for implementation of> // binary insertion sort> #include> // A binary search based function> // to find the position> // where item should be inserted> // in a[low..high]> int> binarySearch(>int> a[],>int> item,> >int> low,>int> high)> {> >if> (high <= low)> >return> (item>a[niedrig]) ?> >(low + 1) : low;> >int> mid = (low + high) / 2;> >if> (item == a[mid])> >return> mid + 1;> >if> (item>a[mid])> >return> binarySearch(a, item,> >mid + 1, high);> >return> binarySearch(a, item, low,> >mid - 1);> }> // Function to sort an array a[] of size 'n'> void> insertionSort(>int> a[],>int> n)> {> >int> i, loc, j, k, selected;> >for> (i = 1; i { j = i - 1; selected = a[i]; // find location where selected should be inserted loc = binarySearch(a, selected, 0, j); // Move all elements after location to create space while (j>= loc) { a[j + 1] = a[j]; J--; } a[j + 1] = ausgewählt; } } // Treibercode int main() { int a[] = { 37, 23, 0, 17, 12, 72, 31, 46, 100, 88, 54 }; int n = sizeof(a) / sizeof(a[0]), i; insertSort(a, n); printf('Sortiertes Array: '); for (i = 0; i printf('%d ', a[i]); return 0; }>

>

>

Java




// Java Program implementing> // binary insertion sort> import> java.util.Arrays;> class> GFG> {> > >public> static> void> main(String[] args)> >{> >final> int>[] arr = {>37>,>23>,>0>,>17>,>12>,>72>,> >31>,>46>,>100>,>88>,>54> };> >new> GFG().sort(arr);> >for> (>int> i =>0>; i System.out.print(arr[i] + ' '); } // Driver Code public void sort(int array[]) { for (int i = 1; i { int x = array[i]; // Find location to insert // using binary search int j = Math.abs( Arrays.binarySearch(array, 0, i, x) + 1); // Shifting array to one // location right System.arraycopy(array, j, array, j + 1, i - j); // Placing element at its // correct location array[j] = x; } } } // Code contributed by Mohit Gupta_OMG>

>

>

Python3




# Python Program implementation> # of binary insertion sort> def> binary_search(arr, val, start, end):> > ># we need to distinguish whether we> ># should insert before or after the> ># left boundary. imagine [0] is the last> ># step of the binary search and we need> ># to decide where to insert -1> >if> start>=>=> end:> >if> arr[start]>val:> >return> start> >else>:> >return> start>+>1> ># this occurs if we are moving> ># beyond left's boundary meaning> ># the left boundary is the least> ># position to find a number greater than val> >if> start>Ende:> >return> start> >mid>=> (start>+>end)>/>/>2> >if> arr[mid] return binary_search(arr, val, mid+1, end) elif arr[mid]>val: return Binary_search(arr, val, start, mid-1) else: return mid def insert_sort(arr): for i in range(1, len(arr)): val = arr[i] j = Binary_search(arr, val, 0, i-1) arr = arr[:j] + [val] + arr[j:i] + arr[i+1:] return arr print('Sorted array:') print(insertion_sort( [37, 23, 0, 31, 22, 17, 12, 72, 31, 46, 100, 88, 54])) # Code beigesteuert von Mohit Gupta_OMG>

>

>

C#




// C# Program implementing> // binary insertion sort> using> System;> class> GFG {> >public> static> void> Main()> >{> >int>[] arr = { 37, 23, 0, 17, 12, 72,> >31, 46, 100, 88, 54 };> >sort(arr);> >for> (>int> i = 0; i Console.Write(arr[i] + ' '); } // Driver Code public static void sort(int[] array) { for (int i = 1; i { int x = array[i]; // Find location to insert using // binary search int j = Math.Abs( Array.BinarySearch(array, 0, i, x) + 1); // Shifting array to one location right System.Array.Copy(array, j, array, j + 1, i - j); // Placing element at its correct // location array[j] = x; } } } // This code is contributed by nitin mittal.>

>

>

PHP




// PHP program for implementation of // binary insertion sort // A binary search based function to find // the position where item should be // inserted in a[low..high] function binarySearch($a, $item, $low, $high) { if ($high <= $low) return ($item>$a[$low]) ? ($low + 1) : $low; $mid = (int)(($low + $high) / 2); if($item == $a[$mid]) return $mid + 1; if($item> $a[$mid]) return binarySearch($a, $item, $mid + 1, $high); return binarySearch($a, $item, $low, $mid - 1); } // Funktion zum Sortieren eines Arrays a der Größe 'n' function insertSort(&$a, $n) { $i; $loc; $j; $k; $ausgewählt; für ($i = 1; $i<$n; ++$i) { $j = $i - 1; $selected = $a[$i]; // find location where selected // item should be inserted $loc = binarySearch($a, $selected, 0, $j); // Move all elements after location // to create space while ($j>= $loc) { $a[$j + 1] = $a[$j]; $j--; } $a[$j + 1] = $selected; } } // Treibercode $a = array(37, 23, 0, 17, 12, 72, 31, 46, 100, 88, 54); $n = sizeof($a); insertSort($a, $n); echo 'Sortiertes Array: '; für ($i = 0; $i<$n; $i++) echo '$a[$i] '; // This code is contributed by // Adesh Singh ?>>

>

>

Javascript




> // Javascript Program implementing> // binary insertion sort> function> binarySearch(a, item, low, high)> {> > >if> (high <= low)> >return> (item>a[niedrig]) ?> >(low + 1) : low;> > >mid = Math.floor((low + high) / 2);> > >if>(item == a[mid])> >return> mid + 1;> > >if>(item>a[mid])> >return> binarySearch(a, item,> >mid + 1, high);> > >return> binarySearch(a, item, low,> >mid - 1);> }> function> sort(array)> {> >for> (let i = 1; i { let j = i - 1; let x = array[i]; // Find location to insert // using binary search let loc = Math.abs( binarySearch(array, x, 0, j)); // Shifting array to one // location right while (j>= loc) { array[j + 1] = array[j]; J--; } // Element an seiner // richtigen Position platzieren array[j+1] = x; } } // Treibercode let arr=[ 37, 23, 0, 17, 12, 72, 31, 46, 100, 88, 54]; sort(arr); for (let i = 0; i document.write(arr[i] + ' '); // Dieser Code wird von unsunknown2108 beigesteuert // C-Programm zur Implementierung von // binärer Einfügungssortierung #include // Eine binäre Suche basierte Funktion // um die Position zu finden // wo das Element eingefügt werden soll // in a[low..high] int BinarySearch(int a[], int item, int low, int high) { if (high<= low) return (item>a[niedrig]) ? (niedrig + 1): niedrig; int mid = (niedrig + hoch) / 2; if (item == a[mid]) return mid + 1; if (item> a[mid]) return binarySearch(a, item, mid + 1, high); return binarySearch(a, item, low, mid - 1); } // Funktion zum Sortieren eines Arrays a[] der Größe 'n' void insertSort(int a[], int n) { int i, loc, j, k, selected; for (i = 1; i { j = i - 1; selected = a[i]; // Position finden, an der selected eingefügt werden soll loc = BinarySearch(a, selected, 0, j); // Alle Elemente nach Position verschieben um Platz zu schaffen while (j>= loc) { a[j + 1] = a[j]; } a[j + 1] = selected; [] = { 37, 23, 0, 17, 12, 72, 31, 46, 100, 88, 54 }; int n = sizeof(a) / sizeof(a[0]), i; insertSort(a, n ); printf('Sorted array: '); for (i = 0; i printf('%d ', a[i]); r// C-Programm zur Implementierung der binären Einfügungssortierung #include // Eine auf binärer Suche basierende Funktion // um die Position zu finden // wo ein Element eingefügt werden soll // in a[low..high] int BinarySearch(int a[], int item, int low, int high) { wenn (hoch<= low) return (item>a[niedrig]) ? (niedrig + 1): niedrig; int mid = (niedrig + hoch) / 2; if (item == a[mid]) return mid + 1; if (item> a[mid]) return binarySearch(a, item, mid + 1, high); return binarySearch(a, item, low, mid - 1); } // Funktion zum Sortieren eines Arrays a[] der Größe 'n' void insertSort(int a[], int n) { int i, loc, j, k, selected; for (i = 1; i { j = i - 1; selected = a[i]; // Position finden, an der selected eingefügt werden soll loc = BinarySearch(a, selected, 0, j); // Alle Elemente nach Position verschieben um Platz zu schaffen while (j>= loc) { a[j + 1] = a[j]; } a[j + 1] = selected; [] = { 37, 23, 0, 17, 12, 72, 31, 46, 100, 88, 54 }; int n = sizeof(a) / sizeof(a[0]), i; insertSort(a, n ); printf('Sorted array: '); for (i = 0; i printf('%d ', a[i]); // C-Programm zur Implementierung von // binärer Einfügungssortierung # include // Eine auf einer binären Suche basierende Funktion // um die Position zu finden // wo ein Element eingefügt werden soll // in a[low..high] int BinarySearch(int a[], int item, int low, int high) { if (hoch<= low) return (item>a[niedrig]) ? (niedrig + 1): niedrig; int mid = (niedrig + hoch) / 2; if (item == a[mid]) return mid + 1; if (item> a[mid]) return binarySearch(a, item, mid + 1, high); return binarySearch(a, item, low, mid - 1); } // Funktion zum Sortieren eines Arrays a[] der Größe 'n' void insertSort(int a[], int n) { int i, loc, j, k, selected; for (i = 1; i { j = i - 1; selected = a[i]; // Position finden, an der selected eingefügt werden soll loc = BinarySearch(a, selected, 0, j); // Alle Elemente nach Position verschieben um Platz zu schaffen while (j>= loc) { a[j + 1] = a[j]; } a[j + 1] = selected; [] = { 37, 23, 0, 17, 12, 72, 31, 46, 100, 88, 54 }; int n = sizeof(a) / sizeof(a[0]), i; insertSort(a, n ); printf('Sorted array: '); for (i = 0; i printf('%d ', a[i]); // C-Programm zur Implementierung von // binärer Einfügungssortierung # include // Eine auf einer binären Suche basierende Funktion // um die Position zu finden // wo ein Element eingefügt werden soll // in a[low..high] int BinarySearch(int a[], int item, int low, int high) { if (hoch<= low) return (item>a[niedrig]) ? (niedrig + 1): niedrig; int mid = (niedrig + hoch) / 2; if (item == a[mid]) return mid + 1; if (item> a[mid]) return binarySearch(a, item, mid + 1, high); return binarySearch(a, item, low, mid - 1); } // Funktion zum Sortieren eines Arrays a[] der Größe 'n' void insertSort(int a[], int n) { int i, loc, j, k, selected; for (i = 1; i { j = i - 1; selected = a[i]; // Position finden, an der selected eingefügt werden soll loc = BinarySearch(a, selected, 0, j); // Alle Elemente nach Position verschieben um Platz zu schaffen while (j>= loc) { a[j + 1] = a[j]; } a[j + 1] = selected; [] = { 37, 23, 0, 17, 12, 72, 31, 46, 100, 88, 54 }; int n = sizeof(a) / sizeof(a[0]), i; insertSort(a, n ); printf('Sorted array: '); for (i = 0; i printf('%d ', a[i]); // C-Programm zur Implementierung von // binärer Einfügungssortierung # include // Eine auf einer binären Suche basierende Funktion // um die Position zu finden // wo ein Element eingefügt werden soll // in a[low..high] int BinarySearch(int a[], int item, int low, int high) { if (hoch<= low) return (item>a[niedrig]) ? (niedrig + 1): niedrig; int mid = (niedrig + hoch) / 2; if (item == a[mid]) return mid + 1; if (item> a[mid]) return binarySearch(a, item, mid + 1, high); return binarySearch(a, item, low, mid - 1); } // Funktion zum Sortieren eines Arrays a[] der Größe 'n' void insertSort(int a[], int n) { int i, loc, j, k, selected; for (i = 1; i { j = i - 1; selected = a[i]; // Position finden, an der selected eingefügt werden soll loc = BinarySearch(a, selected, 0, j); // Alle Elemente nach Position verschieben um Platz zu schaffen while (j>= loc) { a[j + 1] = a[j]; } a[j + 1] = selected; [] = { 37, 23, 0, 17, 12, 72, 31, 46, 100, 88, 54 }; int n = sizeof(a) / sizeof(a[0]), i; insertSort(a, n ); printf('Sorted array: '); for (i = 0; i printf('%d ', a[i]);// C-Programm zur Implementierung von // binärer Einfügungssortierung # include // Eine auf einer binären Suche basierende Funktion // um die Position zu finden // wo ein Element eingefügt werden soll // in a[low..high] int BinarySearch(int a[], int item, int low, int high) { if (hoch<= low) return (item>a[niedrig]) ? (niedrig + 1): niedrig; int mid = (niedrig + hoch) / 2; if (item == a[mid]) return mid + 1; if (item> a[mid]) return binarySearch(a, item, mid + 1, high); return binarySearch(a, item, low, mid - 1); } // Funktion zum Sortieren eines Arrays a[] der Größe 'n' void insertSort(int a[], int n) { int i, loc, j, k, selected; for (i = 1; i { j = i - 1; selected = a[i]; // Position finden, an der selected eingefügt werden soll loc = BinarySearch(a, selected, 0, j); // Alle Elemente nach Position verschieben um Platz zu schaffen while (j>= loc) { a[j + 1] = a[j]; } a[j + 1] = selected; [] = { 37, 23, 0, 17, 12, 72, 31, 46, 100, 88, 54 }; int n = sizeof(a) / sizeof(a[0]), i; insertSort(a, n ); printf('Sortiertes Array: '); for (i = 0; i printf('%d ', a[i])>

>

>

Ausgabe

Sorted array: 0 12 17 23 31 37 46 54 72 88 100>

Zeitkomplexität: Der Algorithmus als Ganzes hat immer noch eine laufende Worst-Case-Laufzeit von O(n).2) aufgrund der Reihe von Swaps, die für jede Einfügung erforderlich sind.

Ein anderer Ansatz: Es folgt eine iterative Implementierung des obigen rekursiven Codes

C++




#include> using> namespace> std;> // iterative implementation> int> binarySearch(>int> a[],>int> item,>int> low,>int> high)> {> >while> (low <= high) {> >int> mid = low + (high - low) / 2;> >if> (item == a[mid])> >return> mid + 1;> >else> if> (item>a[mid])> >low = mid + 1;> >else> >high = mid - 1;> >}> >return> low;> }> // Function to sort an array a[] of size 'n'> void> insertionSort(>int> a[],>int> n)> {> >int> i, loc, j, k, selected;> >for> (i = 1; i j = i - 1; selected = a[i]; // find location where selected should be inserted loc = binarySearch(a, selected, 0, j); // Move all elements after location to create space while (j>= loc) { a[j + 1] = a[j]; J--; } a[j + 1] = ausgewählt; } } // Treibercode int main() { int a[] = { 37, 23, 0, 17, 12, 72, 31, 46, 100, 88, 54 }; int n = sizeof(a) / sizeof(a[0]), i; insertSort(a, n); cout<<'Sorted array: '; for (i = 0; i cout <<' '<< a[i]; return 0; } // This code is contributed by shivanisinghss2110.>

>

>

C




#include> // iterative implementation> int> binarySearch(>int> a[],>int> item,>int> low,>int> high)> {> >while> (low <= high) {> >int> mid = low + (high - low) / 2;> >if> (item == a[mid])> >return> mid + 1;> >else> if> (item>a[mid])> >low = mid + 1;> >else> >high = mid - 1;> >}> >return> low;> }> // Function to sort an array a[] of size 'n'> void> insertionSort(>int> a[],>int> n)> {> >int> i, loc, j, k, selected;> >for> (i = 1; i j = i - 1; selected = a[i]; // find location where selected should be inserted loc = binarySearch(a, selected, 0, j); // Move all elements after location to create space while (j>= loc) { a[j + 1] = a[j]; J--; } a[j + 1] = ausgewählt; } } // Treibercode int main() { int a[] = { 37, 23, 0, 17, 12, 72, 31, 46, 100, 88, 54 }; int n = sizeof(a) / sizeof(a[0]), i; insertSort(a, n); printf('Sortiertes Array: '); for (i = 0; i printf('%d ', a[i]); return 0; } // beigetragen von tmeid>

>

>

Java




wie man die MySQL-Workbench verwendet

import> java.io.*;> class> GFG {> // iterative implementation> static> int> binarySearch(>int> a[],>int> item,>int> low,>int> high)> {> >while> (low <= high) {> >int> mid = low + (high - low) />2>;> >if> (item == a[mid])> >return> mid +>1>;> >else> if> (item>a[mid])> >low = mid +>1>;> >else> >high = mid ->1>;> >}> >return> low;> }> // Function to sort an array a[] of size 'n'> static> void> insertionSort(>int> a[],>int> n)> {> >int> i, loc, j, k, selected;> >for> (i =>1>; i j = i - 1; selected = a[i]; // find location where selected should be inserted loc = binarySearch(a, selected, 0, j); // Move all elements after location to create space while (j>= loc) { a[j + 1] = a[j]; J--; } a[j + 1] = ausgewählt; } } // Treibercode public static void main (String[] args) { int a[] = { 37, 23, 0, 17, 12, 72, 31, 46, 100, 88, 54 }; int n = a.length, i; insertSort(a, n); System.out.println('Sortiertes Array:'); for (i = 0; i System.out.print(a[i] +' '); } } // Dieser Code wurde von shivanisinghss2110 beigesteuert.>

>

>

Python3




# iterative implementation> def> binarySearch(a, item, low, high):> >while> (low <>=> high):> >mid>=> low>+> (high>-> low)>/>/> 2> >if> (item>=>=> a[mid]):> >return> mid>+> 1> >elif> (item>a[mid]):> >low>=> mid>+> 1> >else>:> >high>=> mid>-> 1> >return> low> > # Function to sort an array a[] of size 'n'> def> insertionSort(a, n):> >for> i>in> range> (n):> >j>=> i>-> 1> >selected>=> a[i]> > ># find location where selected should be inserted> >loc>=> binarySearch(a, selected,>0>, j)> > ># Move all elements after location to create space> >while> (j>>=> loc):> >a[j>+> 1>]>=> a[j]> >j>->=>1> >a[j>+> 1>]>=> selected> # Driver Code> a>=> [>37>,>23>,>0>,>17>,>12>,>72>,>31>,>46>,>100>,>88>,>54>]> n>=> len>(a)> insertionSort(a, n)> print>(>'Sorted array: '>)> for> i>in> range> (n):> >print>(a[i], end>=>' '>)> # This code is contributed by shivanisinghss2110>

>

>

C#




using> System;> class> GFG {> // iterative implementation> static> int> binarySearch(>int> []a,>int> item,>int> low,>int> high)> {> >while> (low <= high) {> >int> mid = low + (high - low) / 2;> >if> (item == a[mid])> >return> mid + 1;> >else> if> (item>a[mid])> >low = mid + 1;> >else> >high = mid - 1;> >}> >return> low;> }> // Function to sort an array a[] of size 'n'> static> void> insertionSort(>int> []a,>int> n)> {> >int> i, loc, j, selected;> >for> (i = 1; i j = i - 1; selected = a[i]; // find location where selected should be inserted loc = binarySearch(a, selected, 0, j); // Move all elements after location to create space while (j>= loc) { a[j + 1] = a[j]; J--; } a[j + 1] = ausgewählt; } } // Treibercode public static void Main (String[] args) { int []a = { 37, 23, 0, 17, 12, 72, 31, 46, 100, 88, 54 }; int n = a.Length, i; insertSort(a, n); Console.WriteLine('Sortiertes Array:'); for (i = 0; i Console.Write(a[i] +' '); } } // Dieser Code wurde von shivanisinghss2110>'> beigesteuert

> 




> // iterative implementation> function> binarySearch( a, item, low, high)> {> >while> (low <= high) {> >var> mid = low + (high - low) / 2;> >if> (item == a[mid])> >return> mid + 1;> >else> if> (item>a[mid])> >low = mid + 1;> >else> >high = mid - 1;> >}> >return> low;> }> // Function to sort an array a[] of size 'n'> function> insertionSort(a, n)> {> >var> i, loc, j, k, selected;> >for> (i = 1; i j = i - 1; selected = a[i]; // find location where selected should be inserted loc = binarySearch(a, selected, 0, j); // Move all elements after location to create space while (j>= loc) { a[j + 1] = a[j]; J--; } a[j + 1] = ausgewählt; } } // Treibercode var a = [ 37, 23, 0, 17, 12, 72, 31, 46, 100, 88, 54 ]; var n = a.length, i; insertSort(a, n); document.write('Sorted array:' + ' '); for (i = 0; i document.write(a[i] +' '); // Dieser Code wurde von shivanisinghss2110>'> beigesteuert

> 

Sorted array: 0 12 17 23 31 37 46 54 72 88 100>