插入排序(INSERTION SORT)
2016-08-25 19:45
281 查看
先贴了代码,插入排序就是把所有的在数组中的数据拿出来和其他数组中的元素比较,时间复杂度为:n^2;
还是说下步骤:
初始化:数组从j=1开始,i=0;
迭代:当i=0 时和key(也就时arr[1])比较,key<a[i]的时候进入while 循环,这时,把arr[i] 负值给arr[i+1],i在减去1,如果条件满足while 循环,继续while 循环,如果条件不满足,把key 赋值给arr[i+1], 再继续for 循环;
终止:当数组中的所有的值循环排完序时,终止循环。附代码:
int* insert_sort(int *arr,int len){
int key,i;
for (int j=0;j<=len;j++){
key = arr[j];
i =j-1;
while (i>=0 && arr[i]> key) {
arr[i+1] = arr[i];
i=i-1;
}
arr[i+1] =key;
}
return arr;
}
相关文章推荐
- 算法:冒泡排序(Bubble Sort)、插入排序(Insertion Sort)和选择排序(Selection Sort)总结
- 链表的插入排序 Insertion Sort List
- 插入排序(Insertion Sort)
- C++ insertion sort(插入排序)
- 插入排序(INSERTION-SORT)
- 算法总结JS版(三)—— 插入排序(Insertion Sort)
- 基础排序算法 – 插入排序Insertion sort
- 选择排序(Selection Sort)和插入排序(Insertion Sort)
- PAT甲题题解1098. Insertion or Heap Sort (25)-(插入排序和堆排序)
- [算法练习]Insertion Sort 插入排序
- 排序算法——插入排序(Insertion Sort)
- insertionSort()插入排序
- 插入排序:直接插入排序-Direct insertion sort
- Leetcode Insertion Sort List 插入排序链表
- insertion sort(插入排序)
- 插入排序(InsertionSort)
- 插入排序(InsertionSort)
- Python 数据结构与算法——插入排序(insertion sort)
- 插入排序(INSERTION-SORT)
- 插入排序(InsertionSort)