一、总体思想:将一个记录直接插入到一个已经排序好的列表中的对应位置,形成一个新的列表。直接插入排序为稳定排序。
二、排序过程(升序):
1、认为第一个记录为有序列表。
2、从第二个记录开始,依次和已经排序的列表中记录做比较,如果被比较记录大于该记录, 则将被比较记录后移一位,否则将该记录插入到不大于它的记录之后。
3、依次处理后续记录。
三、算法效率
时间复杂度:O(n^2)
空间复杂度:O(1)
四、代码-数组实现方式
/********************************************
Author: zchshen
Date: 2017-07-09
Brief: ascending order of insert sort by array.
********************************************/
#include <iostream>
#include <stdlib.h>
#include <string.h>
const int MaxArraySize = 128;
void InsertSortUp(int *array, const int &count){
int tmp = 0;
for (int i=1; i<count; i++){
tmp = array[i];
for (int j=i-1; j>=0; j--){
if (array[i] < array[j]){
array[j+1] = array[j];
}
else{
array[j+1] = tmp;
break;
}
}
}
}
int main(int argc, char **argv){
if (argc < 2){
std::cout << argv[0] << " your_array" << std::endl;
std::cout << "Example: " << argv[0] << " 1 3 2 4" << std::endl;
exit(1);
}
if (argc > MaxArraySize){
std::cout << "Input Array too long!" << std::endl;
}
std::cout << argc << std::endl;
std::cout << argv[1] << std::endl << std::endl;
int ArraySize = argc - 1;
int *Array = new int[MaxArraySize + 1];
memset(Array, 0, MaxArraySize + 1);
std::cout << "Org order:" << std::endl;
for (int i=1; i<argc; i++){
Array[i-1] = atoi(argv[i]);
std::cout << Array[i-1] << " ";
}
std::cout << std::endl;
InsertSortUp(Array, ArraySize);
std::cout << "ascending order:" << std::endl;
for (int i=0; i<ArraySize; i++){
std::cout << Array[i] << " ";
}
std::cout << std::endl;
return 0;
}
六、代码-链表实现方式
/*************************************
Author: zchshen
Date: 2017-07-25
Brief: ascending order of insert sort by link list(include head)
**************************************/
#include <iostream>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
using namespace std;
typedef struct Node{
int data;
struct Node* next;
}LinkNode;
void CreatListByEndInsert(LinkNode* head){
assert(head);
head->next = NULL;
LinkNode* Index = head;
LinkNode* node;
srand((uint32_t)time(NULL));
for (int i=0; i<20; i++){
try{
node = new LinkNode();
}
catch(...){
return;
}
node->data = rand()%100;
Index->next = node;
Index = node;
}
node->next = NULL;
}
void DisPlayList(LinkNode* head){
assert(head);
LinkNode* Index = head->next;
while (NULL != Index){
cout << "Data: " << Index->data << endl;
Index = Index->next;
}
cout << endl;
}
void ReleaseList(LinkNode* head){
assert(head);
LinkNode* Index = head->next;
while (NULL != Index){
delete Index;
Index = NULL;
}
delete head;
head = NULL;
}
void InsertSortUp(LinkNode* head){
assert(head);
assert(head->next);
LinkNode* curNode = head->next;
LinkNode* frontNode = head;
LinkNode* remainNode = curNode->next;
LinkNode* insertNode = remainNode;
curNode->next = NULL;
while (remainNode){
while (NULL != curNode && insertNode->data >= curNode->data){
frontNode = curNode;
curNode = curNode->next;
}
remainNode = remainNode->next;
frontNode->next = insertNode;
insertNode->next = curNode;
insertNode = remainNode;
curNode = head;
frontNode = head;
}
}
int main(int argc, char** argv){
LinkNode* head = NULL;
try{
head = new LinkNode();
}
catch(...){
exit(1);
}
CreatListByEndInsert(head);
DisPlayList(head);
InsertSortUp(head);
DisPlayList(head);
ReleaseList(head);
return 0;
}