直接插入排序

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   strong   for   

 

演算法描述:

1.從第一個元素開始,該元素可以認為已經被排序;(j=0的那個元素)

2.取出下一個元素,在已經排序的元素序列中從後向前掃描;

10, 5, 2, 4, 7

5, 10, 2, 4, 7

2, 5, 10, 4, 7

2, 4, 5, 10, 7

2, 4, 5, 7, 10

3.如果元素(已排序)大於新元素,將該元素移到下一位置;(10,5 => 5,10)

4.重複步驟3,直到已經找到已排序元素小於或等於新元素的位置;

5.將新元素插入到該位置中;

6.重複步驟2。

 

 

#include<stdio.h>void insertionsort(int a[],int n){    int i,j,key;    for ( j = 1;j < n;j++)    {        key = a[j];        i = j-1;        while ( i >= 0 && a[i]>key)        {            a[i+1]=a[i];            i -= 1;        }        a[i+1]=key;    }}int main(){    int a[]={10,5,2,4,7};    insertionsort(a,5);        for ( int i = 0;i < 5;i++)        printf("%d ",a[i]);    return 0;}
View Code

 

 

 

 

插入排序,好比是洗撲克一樣,我們將牌分作兩堆,每次從後面一堆的牌抽出最前端的牌,然後插入前面一堆牌的適當位置,例如:
排序前: [92], 77, 67, 8, 6, 84, 55, 85, 43, 67  -- 將數組分為兩部分,第一個元素為一組
第 1 次排序:[77 92] 67 8 6 84 55 85 43 67     -- 將後一組的第一個元素 77 插入前一組的適當位置
第 2 次排序:[67 77 92] 8 6 84 55 85 43 67     -- 將後一組的第一個元素 67 插入前一組的適當位置
第 3 次排序:[8 67 77 92] 6 84 55 85 43 67     -- 將後一組的第一個元素 8 插入前一組的適當位置 
第 4 次排序:[6 8 67 77 92 84] 55 85 43 67      -- 將後一組的第一個元素 6 插入前一組的適當位置
第 5 次排序:[6 8 67 77 84 92 55] 85 43 67      -- 將後一組的第一個元素 84 插入前一組的適當位置
第 6 次排序:[6 8 55 67 77 84 92 85] 43 67      -- 將後一組的第一個元素 55 插入前一組的適當位置
第 7 次排序:[6 8 55 67 77 84 85 92 43] 67      -- 將後一組的第一個元素 85 插入前一組的適當位置
第 8 次排序:[6 8 43 55 67 77 84 85 92] 67      -- 將後一組的第一個元素 43 插入前一組的適當位置
第 9 次排序:[6 8 43 55 67 67 77 84 85 92]      -- 將後一組的第一個元素 67 插入前一組的適當位置

 

 

 

 

 

 

 

 

 

#include <stdio.h>
#define len 5
int a[len] = { 10, 5, 2, 4, 7 };
void insertion_sort(void)
{
int i, j, key;
for (j = 1; j < len; j++)
{
printf("%d, %d, %d, %d, %d\n", a[0], a[1], a[2], a[3], a[4]);
key = a[j];
i = j - 1;
while (i >= 0 && a[i] > key)
{
a[i+1] = a[i];
i--;
}
a[i+1] = key;
}
printf("%d, %d, %d, %d, %d\n", a[0], a[1], a[2], a[3], a[4]);
}

int main()
{
insertion_sort();
return 0;
}

 

 

 

 

 

#include <stdio.h>
#define len 5
int a[len] = { 10, 5, 2, 4, 7 };
void insertion_sort(void)
{
int i, j, key;
for (j = 1; j < len; j++)
{

key = a[j];
i = j - 1;
while (i >= 0 && a[i] > key)
{
a[i+1] = a[i];
i--;
}
a[i+1] = key;
}
printf("%d, %d, %d, %d, %d\n", a[0], a[1], a[2], a[3], a[4]);
}

int main()
{
insertion_sort();
return 0;
}

 

 

 

 

 

 

 

 

 

 

 

#include <stdio.h>
#define len 5
int a[len] = { 10, 5, 2, 4, 7 };
void insertion_sort(void)
{
int i, j, key;
for (j = 1; j < len; j++)
{
key = a[j];
i = j - 1;
while (i >= 0 && a[i] > key)
{
a[i+1] = a[i];
i--;
}
a[i+1] = key;
}
}

int main()
{
insertion_sort();

printf("%d, %d, %d, %d, %d\n", a[0], a[1], a[2], a[3], a[4]);
return 0;
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.