简单插入排序的C语言实现
2010-08-17 15:29
288 查看
代码
#include <stdio.h>
#include <stdlib.h>
void PrintHeap(const char* strMsg,int array[],int nLength);
void InsertionSort1(int *items, int count)
void InsertionSort2(int a[],int size);
void PrintArray(const char* strMsg,int array[],int nLength);
int main(int argc, char *argv[])
{
int data[13]={8,5,4,6,13,7,1,9,12,11,3,10,2};
InsertionSort1(data,13);
PrintArray("Insertion Sort:",data,13);
system("PAUSE");
return 0;
}
/*
插入排序思路:
将数组分成两个区域:已排序区域和未排序区域。首先假设数组的第一个元素处于已排序区域,
第一个元素之后的所有元素都处于未排序区域。
排序时用到两层循环,第一层循环用于从未排序区域中取出待排序元素,并逐步缩小未排序区域,
第二层循环用于从已排序区域中寻找插入位置(即不断地从已排序区域中寻找比待排序元素大的元素,
然后将较大的已排序区的元素后移,后移的最终结果是已排序区元素的最后一个元素占据
待排序元素原来的位置,而已排序区中间空出一个位置),最后将待排序元素插入元素后移后留下的空位。
注:待排序元素所在位置与已排序元素的最后一个元素是相邻的,因此进行移位循环时第一次后移时将已排序元素的最后一个元素直接移至待排序元素的位置即可。元素比较和元素移位共用一个循环,即边比较边移位。
*/
void InsertionSort1(int *items, int count)
{
int x, y;
int c;
for ( x=1; x<count; ++x )
{
c = items[x];
for ( y=x-1; (y>=0) && (c<items[y]); y-- )
items[y+1] = items[y];
items[y+1] = c;
}
}
void InsertionSort2(int a[],int size)
{
int i,j,v;
//initially,the first item is considered to be sorted
//i divides a into a sorted region,x<i,and unsorted one,x>=i
for(i=1;i<size;i++)
{
//select the item at the beginning of the as yet unsorted section
v=a[i];
//work backwards through the array,finding where v should go
j=i;
//if this element is greater than v,move it up one
while(a[j-1]>v)
{
a[j]=a[j-1];
j--;
if(j<=0) break;
}
//stopped when a[j-1]<=v,put v at position
a[j]=v;
}
}
void PrintArray(const char* strMsg,int array[],int nLength)
{
int i;
printf("%s",strMsg);
for(i=0;i<nLength;i++)
{
printf("%d ",array[i]);
}
printf("\n");
}
参考资料:http://www.ahhf45.com/info/Data_Structures_and_Algorithms/algorithm/commonalg/sort/internal_sorting/insertion_sort/insertion_sort.htm http://baike.baidu.com/view/1193395.htm
http://www.javaeye.com/topic/547734
#include <stdio.h>
#include <stdlib.h>
void PrintHeap(const char* strMsg,int array[],int nLength);
void InsertionSort1(int *items, int count)
void InsertionSort2(int a[],int size);
void PrintArray(const char* strMsg,int array[],int nLength);
int main(int argc, char *argv[])
{
int data[13]={8,5,4,6,13,7,1,9,12,11,3,10,2};
InsertionSort1(data,13);
PrintArray("Insertion Sort:",data,13);
system("PAUSE");
return 0;
}
/*
插入排序思路:
将数组分成两个区域:已排序区域和未排序区域。首先假设数组的第一个元素处于已排序区域,
第一个元素之后的所有元素都处于未排序区域。
排序时用到两层循环,第一层循环用于从未排序区域中取出待排序元素,并逐步缩小未排序区域,
第二层循环用于从已排序区域中寻找插入位置(即不断地从已排序区域中寻找比待排序元素大的元素,
然后将较大的已排序区的元素后移,后移的最终结果是已排序区元素的最后一个元素占据
待排序元素原来的位置,而已排序区中间空出一个位置),最后将待排序元素插入元素后移后留下的空位。
注:待排序元素所在位置与已排序元素的最后一个元素是相邻的,因此进行移位循环时第一次后移时将已排序元素的最后一个元素直接移至待排序元素的位置即可。元素比较和元素移位共用一个循环,即边比较边移位。
*/
void InsertionSort1(int *items, int count)
{
int x, y;
int c;
for ( x=1; x<count; ++x )
{
c = items[x];
for ( y=x-1; (y>=0) && (c<items[y]); y-- )
items[y+1] = items[y];
items[y+1] = c;
}
}
void InsertionSort2(int a[],int size)
{
int i,j,v;
//initially,the first item is considered to be sorted
//i divides a into a sorted region,x<i,and unsorted one,x>=i
for(i=1;i<size;i++)
{
//select the item at the beginning of the as yet unsorted section
v=a[i];
//work backwards through the array,finding where v should go
j=i;
//if this element is greater than v,move it up one
while(a[j-1]>v)
{
a[j]=a[j-1];
j--;
if(j<=0) break;
}
//stopped when a[j-1]<=v,put v at position
a[j]=v;
}
}
void PrintArray(const char* strMsg,int array[],int nLength)
{
int i;
printf("%s",strMsg);
for(i=0;i<nLength;i++)
{
printf("%d ",array[i]);
}
printf("\n");
}
参考资料:http://www.ahhf45.com/info/Data_Structures_and_Algorithms/algorithm/commonalg/sort/internal_sorting/insertion_sort/insertion_sort.htm http://baike.baidu.com/view/1193395.htm
http://www.javaeye.com/topic/547734
相关文章推荐
- (数据结构排序的实验四)快速,冒泡,简单选择,直接插入排序的c语言实现!!
- 简单了解C语言中直接插入排序与直接选择排序实现
- C语言实现 排序源程序(包括直接插入、希尔、冒泡、快速、简单选择、堆排序)
- 排序(2)---------简单插入排序(C语言实现)
- C语言实现 排序源程序(包括直接插入、希尔、冒泡、快速、简单选择、堆排序)
- 排序(2)---------简单插入排序(C语言实现)
- C语言实现直接插入排序
- 插入排序及C语言实现
- 简单插入排序和shell插入排序【C语言】
- Java中实现简单的三大排序——冒泡、选择、插入
- java实现七种排序 (插入排序, 希尔排序, 插入排序, 快速排序, 简单选择排序, 堆排序, 归并排序)
- 黑马程序员_iOS_C语言实现排序、查找和插入
- 冒泡排序, 简单选择排序, 插入排序, 希尔排序, 快速排序 js 实现
- 简单选择、冒泡、插入排序思想及Java实现代码
- Python实现冒泡,插入,选择排序简单实例
- c语言实现直接插入排序(正序和逆序)
- 简单插入排序的实现
- C语言描述——简单插入排序
- c语言实现单链表的操作:创建,删除,插入,反转, 排序等
- 简单的C语言快速排序实现代码