# 插入排序

#### 1. 技術

`A[k-1]``A[j]`的所有元素都需要移位，`A[k]`將移動到`A[j + 1]`

#### 2. 複雜度

##### 3. 演算法
``````第1步 : 重複第2步到第5步：由 K = 1 到 N-1

設定 ARR[J + 1] = ARR[J]
設定 J = J ? 1
[結束內迴圈]

[結束回圈]

``````

``````#include<stdio.h>
void main ()
{
int i,j, k,temp;
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
printf("printing sorted elements...\n");
for(k=1; k<10; k++)
{
temp = a[k];
j= k-1;
while(j>=0 && temp <= a[j])
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
for(i=0;i<10;i++)
{
printf("\n%d\n",a[i]);
}
}
``````

``````Printing Sorted Elements . . .
7
9
10
12
23
23
34
44
78
101
``````

``````#include<iostream>
using namespace std;
int main ()
{
int i,j, k,temp;
int a[10] = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
cout<<"\nprinting sorted elements...\n";
for(k=1; k<10; k++)
{
temp = a[k];
j= k-1;
while(j>=0 && temp <= a[j])
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
for(i=0;i<10;i++)
{
cout <<a[i]<<"\n";
}
}
``````

``````printing sorted elements...
7
9
10
12
23
23
34
44
78
101
``````

``````public class InsertionSort {
public static void main(String[] args) {
int[] a = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
for(int k=1; k<10; k++)
{
int temp = a[k];
int j= k-1;
while(j>=0 && temp <= a[j])
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
System.out.println("printing sorted elements ...");
for(int i=0;i<10;i++)
{
System.out.println(a[i]);
}
}
}
``````

``````Printing sorted elements . . .
7
9
10
12
23
23
34
44
78
101
``````

``````using System;

public class Program
{

public static void Main()
{
int i,j, k,temp;
int[] a = { 10, 9, 7, 101, 23, 44, 12, 78, 34, 23};
Console.WriteLine("printing sorted elements...\n");
for(k=1; k<10; k++)
{
temp = a[k];
j= k-1;
while(j>=0 && temp <= a[j])
{
a[j+1] = a[j];
j = j-1;
}
a[j+1] = temp;
}
for(i=0;i<10;i++)
{
Console.WriteLine(a[i]);
}
}
}
``````

``````printing sorted elements . . .
7
9
10
12
23
23
34
44
78
101
``````

``````a=[10, 9, 7, 101, 23, 44, 12, 78, 34, 23]
for k in range(1,10):
temp = a[k]
j = k-1
while j>=0 and temp <=a[j]:
a[j+1]=a[j]
j = j-1
a[j+1] = temp
print("printing sorted elements...")
for i in a:
print(i)
``````

``````printing sorted elements . . .
7
9
10
12
23
23
34
44
78
101
``````

``````import Foundation
import Glibc
var a = [10, 9, 7, 101, 23, 44, 12, 78, 34, 23];
print("printing sorted elements...\n");
for k in 1...9
{
let temp = a[k];
var j = k-1;
while j>=0 && temp <= a[j]
{
a[j+1] = a[j];
j = j-1;
}

a[j+1] = temp;
}
for i in a
{
print(i);
}
``````

``````printing sorted elements...

7
9
10
12
23
23
34
44
78
101
``````

``````
var txt = "<br>";
var a = [10, 9, 7, 101, 23, 44, 12, 78, 34, 23];
document.writeln("printing sorted elements ... "+txt);
for(k=0;k<10;k++)
{
var temp = a[k]
j=k-1;
while (j>=0 && temp <= a[j])
{
a[j+1] = a[j];
jj = j-1;
}
a[j+1] = temp;
}

for(i=0;i<10;i++)
{
document.writeln(a[i]);
document.writeln(txt);
}
``````

``````printing sorted elements ...
7
9
10
12
23
23
34
44
78
101
``````

``````<?php
\$a = array(10, 9, 7, 101, 23, 44, 12, 78, 34, 23);
echo("printing sorted elements ... \n");
for(\$k=0;\$k<10;\$k++)
{
\$temp = \$a[\$k];
\$j=\$k-1;
while (\$j>=0 && \$temp <= \$a[\$j])
{
\$a[\$j+1] = \$a[\$j];
\$j = \$j-1;
}
\$a[\$j+1] = \$temp;
}
for(\$i=0;\$i<10;\$i++)
{
echo \$a[\$i];
echo "\n";
}
?>
``````

``````printing sorted elements ...
7
9
10
12
23
23
34
44
78
101
``````