【数据结构】内排序算法 C语言描述(带注释)

插入排序

//插入排序 
void charupaixu(int inputArray[],int length){
	int i,j;
	int thisArray[length];//用来存接受的数组,以不更改原数组 
	for(i = 0;i<length;i++){
		thisArray[i]= inputArray[i];
	}
	//算法正式开始 
	int temp;//用来保存当前被插入元素
	for(i = 1;i<length;i++){
		
		temp = thisArray[i];
		j = i-1;
		while(j>=0 && thisArray[j]>temp){//核心 
			thisArray[j+1] = thisArray[j];//冒泡 
			j--; 
		}
		thisArray[j+1] = temp;
	} 
	//打印 
	printf("插入排序后:");
	int num;
 	for(num = 0;num<length;num++){
 		printf("%d,",thisArray[num]);
	}
	printf("\n");
} 

选择排序

//选择排序
void xuanzepaixu(int inputArray[],int length){
	int i,j,minlocation;
	int thisArray[length];//用来存接受的数组,以不更改原数组 
	for(i = 0;i<length;i++){
		thisArray[i]= inputArray[i];
	}
	//算法正式开始 
	int min;
	int temp;//用于元素交换 
	for(i=0;i<length;i++){
		min = thisArray[i];
		j = i;
		while(j<length){//求后排的最小元素 
			if(thisArray[j]<min){
				//如果发现最小元素,则更新最小元素的值和位置 
				min = thisArray[j];
				minlocation = j;
			}
			j++;//配合while 
		}
		if(thisArray[i]>min){//如果后面的最小值小于前面,则交换 
			temp = thisArray[i];
			thisArray[i] = min;
			thisArray[minlocation] = temp;
		}
	} 
	//打印 
	printf("选择排序后:");
	int num;
 	for(num = 0;num<length;num++){
 		printf("%d,",thisArray[num]);
	}
	printf("\n");
} 

冒泡排序

//冒泡排序 
void maopaopaixu(int inputArray[],int length){
	int i,j,temp;
	int thisArray[length];//用来存接受的数组,以不更改原数组 
	for(i = 0;i<length;i++){
		thisArray[i]= inputArray[i];
	}
	//算法正式开始 
	for(i=0;i<length-1;i++){
		for(j=0;j<length-1-i;j++){//注意j的条件,因为后排是有序数组,前排无序 
			if(thisArray[j]>thisArray[j+1]){
				temp = thisArray[j+1];
				thisArray[j+1] = thisArray[j];
				thisArray[j] = temp;
			}
		}
	}
	//打印 
	printf("冒泡排序后:");
	int num;
 	for(num = 0;num<length;num++){
 		printf("%d,",thisArray[num]);
	}
	printf("\n");
} 

以下为总代码:

#include <stdio.h>

void charupaixu(int inputArray[],int length);
void xuanzepaixu(int inputArray[],int length);
void maopaopaixu(int inputArray[],int length);

int main(int argc, char** argv) {
	int arr[] = {1,47, 2, 0, 4, 7, 98, 0, 12, 35, 99, 14}; 
 	int length = sizeof(arr)/sizeof(arr[0]);
 	
 	
 	//查看原数组 
 	printf("原数组为:");
 	int i;
 	for(i = 0;i<length;i++){
 		printf("%d,",arr[i]);
	}
	printf("\n");
	printf("长度为:%d\n",length);
	
	charupaixu(arr,length);
	xuanzepaixu(arr,length);
	maopaopaixu(arr,length);
}

//插入排序 
void charupaixu(int inputArray[],int length){
	int i,j;
	int thisArray[length];//用来存接受的数组,以不更改原数组 
	for(i = 0;i<length;i++){
		thisArray[i]= inputArray[i];
	}
	//算法正式开始 
	int temp;//用来保存当前被插入元素
	for(i = 1;i<length;i++){
		
		temp = thisArray[i];
		j = i-1;
		while(j>=0 && thisArray[j]>temp){//核心 
			thisArray[j+1] = thisArray[j];//冒泡 
			j--; 
		}
		thisArray[j+1] = temp;
	} 
	//打印 
	printf("插入排序后:");
	int num;
 	for(num = 0;num<length;num++){
 		printf("%d,",thisArray[num]);
	}
	printf("\n");
} 

//选择排序
void xuanzepaixu(int inputArray[],int length){
	int i,j,minlocation;
	int thisArray[length];//用来存接受的数组,以不更改原数组 
	for(i = 0;i<length;i++){
		thisArray[i]= inputArray[i];
	}
	//算法正式开始 
	int min;
	int temp;//用于元素交换 
	for(i=0;i<length;i++){
		min = thisArray[i];
		j = i;
		while(j<length){//求后排的最小元素 
			if(thisArray[j]<min){
				//如果发现最小元素,则更新最小元素的值和位置 
				min = thisArray[j];
				minlocation = j;
			}
			j++;//配合while 
		}
		if(thisArray[i]>min){//如果后面的最小值小于前面,则交换 
			temp = thisArray[i];
			thisArray[i] = min;
			thisArray[minlocation] = temp;
		}
	} 
	//打印 
	printf("选择排序后:");
	int num;
 	for(num = 0;num<length;num++){
 		printf("%d,",thisArray[num]);
	}
	printf("\n");
} 

//冒泡排序 
void maopaopaixu(int inputArray[],int length){
	int i,j,temp;
	int thisArray[length];//用来存接受的数组,以不更改原数组 
	for(i = 0;i<length;i++){
		thisArray[i]= inputArray[i];
	}
	//算法正式开始 
	for(i=0;i<length-1;i++){
		for(j=0;j<length-1-i;j++){//注意j的条件,因为后排是有序数组,前排无序 
			if(thisArray[j]>thisArray[j+1]){
				temp = thisArray[j+1];
				thisArray[j+1] = thisArray[j];
				thisArray[j] = temp;
			}
		}
	}
	//打印 
	printf("冒泡排序后:");
	int num;
 	for(num = 0;num<length;num++){
 		printf("%d,",thisArray[num]);
	}
	printf("\n");
} 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
全集内容结构如下: ├─图 │ ├─关键路径(有向无环图及其应用2) │ │ 1.txt │ │ ALGraph.cpp │ │ ALGraph.h │ │ CriticalPath.cpp │ │ CriticalPath.h │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ Main.cpp │ │ SqStack.cpp │ │ SqStack.h │ │ Status.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─图的关节点 │ │ 1.txt │ │ ALGraph.cpp │ │ ALGraph.h │ │ FindArticul.cpp │ │ FindArticul.h │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ Status.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─图的数组表示法 │ │ InfoType.cpp │ │ InfoType.h │ │ Main.cpp │ │ MGraph.cpp │ │ MGraph.h │ │ Status.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─图的遍历 │ │ ALGraph.cpp │ │ ALGraph.h │ │ DEBUG.txt │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ Main.cpp │ │ MGraph.cpp │ │ MGraph.h │ │ MTraverse.cpp │ │ MTraverse.h │ │ Status.h │ │ t1.txt │ │ t2.txt │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─图的邻接表存储结构 │ │ ALGraph.cpp │ │ ALGraph.h │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ Main.cpp │ │ Status.h │ │ t1.txt │ │ t2.txt │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─最短路径(从某个源点到其余各顶点的的最短路径) │ │ 1.txt │ │ 2.txt │ │ InfoType.cpp │ │ InfoType.h │ │ Main.cpp │ │ MGraph.cpp │ │ MGraph.h │ │ ShortestPath_DIJ.cpp │ │ ShortestPath_DIJ.h │ │ Status.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ └─最短路径(每一对顶点间的最短路径) │ 1.txt │ 2.txt │ InfoType.cpp │ InfoType.h │ Main.cpp │ map.txt │ MGraph.cpp │ MGraph.h │ RailwaySearch.cpp │ ShortestPath_FLOYD.cpp │ ShortestPath_FLOYD.h │ Status.h │ VertexType.cpp │ VertexType.h │ ├─排序 │ ├─冒泡排序 │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_BubbleSort.cpp │ │ Sq_BubbleSort.h │ │ │ ├─哈希表(哈希查找) │ │ ElemType.cpp │ │ ElemType.h │ │ HashTable.cpp │ │ HashTable.h │ │ main.cpp │ │ Records.txt │ │ │ ├─基数排序 │ │ 1.txt │ │ main.cpp │ │ SLL_RadixSort.cpp │ │ SLL_RadixSort.h │ │ │ ├─归并排序 │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ sq_MergeSort.cpp │ │ sq_MergeSort.h │ │ │ ├─快速排序 │ │ 1.txt │ │ 2.txt │ │ 3.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_QuitSort.cpp │ │ Sq_QuitSort.h │ │ │ ├─拓扑排序(有向无环图及其应用) │ │ 1.txt │ │ ALGraph.cpp │ │ ALGraph.h │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ Main.cpp │ │ SqStack.cpp │ │ SqStack.h │ │ Status.h │ │ TopologicalSort.cpp │ │ TopologicalSort.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─插入排序 │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_InsertSort.cpp │ │ Sq_InsertSort.h │ │ │ ├─插入排序(希尔排序) │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_InsertSort.cpp │ │ Sq_InsertSort.h │ │ │ ├─插入排序(表插入排序) │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ SL_InsertSort.cpp │ │ SL_InsertSort.h │ │ │ ├─选择排序(堆排序) │ │ 1.txt │ │ 2.txt │ │ 3.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_HeapSort.cpp │ │ Sq_HeapSort.h │ │ │ ├─选择排序(树形选择排序) │ │ 1.txt │ │ main.cpp │ │ RedType.cpp │ │ RedType.h │ │ Sq_TreeSelectionSort.cpp │ │ Sq_TreeSelectionSort.h │ │ │ └─选择排序(简单选择排序) │ 1.txt │ main.cpp │ RedType.cpp │ RedType.h │ Sq_SelectSort.cpp │ Sq_SelectSort.h │ ├─查找 │ ├─动态查找表(二叉排序树) │ │ 1.txt │ │ BiTree.cpp │ │ BiTree.h │ │ DElemType.cpp │ │ DElemType.h │ │ DSTable.cpp │ │ DSTable.h │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ QElemType.h │ │ Status.h │ │ TElmeType.h │ │ │ ├─平衡二叉树(二叉排序树的平衡旋转生成) │ │ 1.txt │ │ BBSTree.cpp │ │ BBSTree.h │ │ BiTree.cpp │ │ BiTree.h │ │ DElemType.cpp │ │ DElemType.h │ │ DSTable.cpp │ │ DSTable.h │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ QElemType.h │ │ Status.h │ │ TElmeType.h │ │ │ ├─平衡的m路查找树—B_树 │ │ BTree.cpp │ │ BTree.h │ │ main.cpp │ │ Record.h │ │ Status.h │ │ │ ├─键树(Trie树) │ │ 1.txt │ │ main.cpp │ │ Record.h │ │ Status.h │ │ TrieTree.cpp │ │ TrieTree.h │ │ │ ├─键树(双链键树) │ │ 1.txt │ │ DLTree.cpp │ │ DLTree.h │ │ main.cpp │ │ Record.h │ │ Status.h │ │ │ ├─静态查找表(有序表的查找) │ │ 1.txt │ │ main.cpp │ │ SElemType.cpp │ │ SElemType.h │ │ SSTable.cpp │ │ SSTable.h │ │ Status.h │ │ │ ├─静态查找表(静态树表查找) │ │ 1.txt │ │ BiTree.cpp │ │ BiTree.h │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ QElemType.h │ │ SElemType.cpp │ │ SElemType.h │ │ SSTable.cpp │ │ SSTable.h │ │ Status.h │ │ TElmeType.h │ │ │ └─静态查找表(顺序表的查找) │ 1.txt │ main.cpp │ SElemType.cpp │ SElemType.h │ SSTable.cpp │ SSTable.h │ Status.h │ ├─树 │ ├─二叉树的二叉链表存储 │ │ BiTree.cpp │ │ BiTree.h │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ QElemType.h │ │ Status.h │ │ TElmeType.h │ │ │ ├─二叉树的顺序存储结构 │ │ main.cpp │ │ SqBiTree.cpp │ │ SqBiTree.h │ │ Status.h │ │ TELemType_define.cpp │ │ │ ├─哈夫曼树和哈夫曼编码 │ │ HuffmanTree.cpp │ │ HuffmanTree.h │ │ main.cpp │ │ Status.h │ │ │ ├─最小生成树 │ │ 1.txt │ │ InfoType.cpp │ │ InfoType.h │ │ Main.cpp │ │ MGraph.cpp │ │ MGraph.h │ │ MiniSpanTree_Kruskal.cpp │ │ MiniSpanTree_Kruskal.h │ │ MiniSpanTree_PRIM.cpp │ │ MiniSpanTree_PRIM.h │ │ Status.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ ├─树的二叉链表 │ │ CSTree.cpp │ │ CSTree.h │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ main.cpp │ │ QElemType.h │ │ Status.h │ │ TElmeType.h │ │ │ ├─深度优先生成森林(无向图的连通性和生成树) │ │ ALGraph.cpp │ │ ALGraph.h │ │ CSTree.cpp │ │ CSTree.h │ │ DFSForest.cpp │ │ DFSForest.h │ │ InfoType.cpp │ │ InfoType.h │ │ LinkList.cpp │ │ LinkQueue.cpp │ │ LinkQueue.h │ │ Main.cpp │ │ QElemType.h │ │ Status.h │ │ TElmeType.h │ │ VertexType.cpp │ │ VertexType.h │ │ │ └─线索二叉树 │ BiThrTree.cpp │ BiThrTree.h │ main.cpp │ Status.h │ TElmeType.h │ └─表和数组 ├─KMP算法 │ Basic_operation_functions.h │ def_SString.h │ KMP.h │ main.cpp │ Status.h │ ├─n阶对称矩阵的压缩存储 │ Basic_operation_functions.h │ mian.cpp │ Status.h │ struct SyMatrix.h │ ├─三元组稀疏矩阵的压缩存储 │ Basic_operation_functions.h │ B_opera_fun_called_fun.h │ main.cpp │ Status.h │ struct TSMatrix.h │ Universal_main.h │ Universa_ts_b_opera_fun.h │ ├─不设头结点的单链表 │ LinkList.cpp │ LinkList.h │ main.cpp │ Status.h │ ├─串的堆存储结构 │ Basic_operation_functions.h │ HString.h │ Line_List.h │ main.cpp │ Status.h │ ├─串的定长顺序存储结构 │ Basic_operation_functions.h │ def_SString.h │ Main.cpp │ Status.h │ ├─广义表 │ GList.cpp │ GList.h │ main.cpp │ SString.cpp │ SString.h │ Status.h │ ├─数组 │ Basic_operation_functions.h │ main.cpp │ Status.h │ struct array.h │ ├─文本编辑(串和行表操作) │ Basic_operation_functions.h │ HString.h │ Line_List.h │ main.cpp │ Status.h │ ├─栈的顺序存储结构 │ main.cpp │ SqStack.cpp │ SqStack.h │ Status.h │ ├─走迷宫 │ Basic_called_functions.h │ Basic_operation_functions.h │ Main_Maze.cpp │ Status.h │ struct SqStack.h │ └─链队列 Basic_called_functions.cpp Basic_called_functions.h Basic_operation_functions.cpp main.cpp QElemType.h Status.h Struct_LinkQueue.h
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值