排序算法1:冒泡排序、快速排序、插入排序

排序算法:交换类排序,插入类排序、选择类排序、归并类排序

交换类排序:冒泡排序、快速排序

一、冒泡排序

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef int ElemType;
typedef struct{
	ElemType *elem; //整形指针,申请的堆空间的起始地址存入elem 
	int TableLen; //存储动态数组里边元素的个数 
}SSTable;
 
void ST_Init(SSTable &ST,int len){
	ST.TableLen=len;
	ST.elem=(ElemType *)malloc(sizeof(ElemType)*ST.TableLen);
	int i;
	srand(time(NULL)); //随机数生成
	for(i=0;i<ST.TableLen;i++){
		ST.elem[i]=rand()%100;
	} 
}
 
void ST_print(SSTable ST){
	int i;
	for(i=0;i<ST.TableLen;i++){
		printf("%3d",ST.elem[i]);
	}
	printf("\n");
}

void swap(ElemType &a,ElemType &b){
	ElemType tmp;
	tmp=a;
	a=b;
	b=tmp;
} 

//两层循环 
//优先写内层循环,再写外层循环 
void BubbleSort(ElemType A[],int n){
	int i,j;
	bool flag; 
	for(i=0;i<n-1;i++){ //控制的是有序数的数目
	    flag=false; 
		for(j=n-1;j>i;j--){ //内层控制比较和交换 
			if(A[j-1]>A[j]){
				swap(A[j-1],A[j]);
				flag=true;
			}
		}
		if(flag==false){ //如果一趟没有发生任何交换,说明数组有序,提前结束排序
		    return; 
		}
	} 	
}
 
int main(){
	SSTable ST;
	ST_Init(ST,10); 
	ST_print(ST);
	BubbleSort(ST.elem,10); 
	ST_print(ST);
	return 0;
}

时间复杂度:内层是j>i,外层是从0到n-1,运行的总次数是1+2+3+4+...+n-1,即O()

空间复杂度:O(1),没有使用额外空间,不会因为n的变化而变化

如果数组本身有序,最好的时间复杂度是O(n)

二、快速排序

核心:分治思想

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef int ElemType;
typedef struct{
	ElemType *elem; //整形指针,申请的堆空间的起始地址存入elem 
	int TableLen; //存储动态数组里边元素的个数 
}SSTable;
 
void ST_Init(SSTable &ST,int len){
	ST.TableLen=len;
	ST.elem=(ElemType *)malloc(sizeof(ElemType)*ST.TableLen);
	int i;
	srand(time(NULL)); //随机数生成
	for(i=0;i<ST.TableLen;i++){
		ST.elem[i]=rand()%100;
	} 
}
 
void ST_print(SSTable ST){
	int i;
	for(i=0;i<ST.TableLen;i++){
		printf("%3d",ST.elem[i]);
	}
	printf("\n");
}

void swap(ElemType &a,ElemType &b){
	ElemType tmp;
	tmp=a;
	a=b;
	b=tmp;
} 

int Partition(ElemType A[],int low,int high){
	ElemType pivot=A[low]; //拿最左边的作为分割值,并存储下来 
	while(low<high){
		while(low<high&&A[high]>=pivot){ //从后往前遍历,找到一个比分割值小的 
			--high;
		}
		A[low]=A[high]; 
		while(low<high&&A[low]<=pivot){
			++low;
		}
		A[high]=A[low];
	}
	A[low]=pivot;
	return low; //返回分割值所在的下标 
}

//递归实现
void QuickSort(ElemType A[],int low,int high){
	if(low<high){
		int pivotpos=Partition(A,low,high);
		QuickSort(A,low,pivotpos-1);
		QuickSort(A,pivotpos+1,high);
	}
} 
 
int main(){
	SSTable ST;
	ST_Init(ST,10); 
	ST_print(ST);
	QuickSort(ST.elem,0,9); 
	ST_print(ST);
	return 0;
}

空间复杂度与n有关

三、插入排序

插入排序:直接插入排序、折半插入排序、希尔排序

直接插入排序

cpp 复制代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef int ElemType;
typedef struct{
	ElemType *elem; //整形指针,申请的堆空间的起始地址存入elem 
	int TableLen; //存储动态数组里边元素的个数 
}SSTable;
 
void ST_Init(SSTable &ST,int len){
	ST.TableLen=len;
	ST.elem=(ElemType *)malloc(sizeof(ElemType)*ST.TableLen);
	int i;
	srand(time(NULL)); //随机数生成
	for(i=0;i<ST.TableLen;i++){
		ST.elem[i]=rand()%100;
	} 
}
 
void ST_print(SSTable ST){
	int i;
	for(i=0;i<ST.TableLen;i++){
		printf("%3d",ST.elem[i]);
	}
	printf("\n");
}

void InsertSort(ElemType *arr,int n){
	int i,j,insertVal;
	for(i=1;i<n;i++){ //控制要插入的数 
		insertVal=arr[i]; //先保存要插入的值
		//内层控制比较,往后覆盖 
		for(j=i-1;j>=0&&arr[j]>insertVal;j--){
			arr[j+1]=arr[j];
		} 
		arr[j+1]=insertVal;
	}
}
 
int main(){
	SSTable ST;
	ST_Init(ST,10); 
	ST_print(ST);
	InsertSort(ST.elem,10); 
	ST_print(ST);
	return 0;
}
相关推荐
番茄味城市甜菜12 分钟前
数据结构:单链表(C语言)
数据结构·链表·1024程序员节
蹊黎17 分钟前
C++中stack、queue、priority queue类的部分简单实现
数据结构·c++·visualstudio·1024程序员节
吴可可12331 分钟前
Accumark二次开发环境搭建与API入门
算法
木井巳2 小时前
【动态规划: 线性dp】解码方法
java·算法·leetcode·动态规划·推荐算法
春弦_10 小时前
自用算法整理二
数据结构·算法·图论
hetao173383711 小时前
2026-10-22~23 hetao1733837 的刷题记录
c++·算法
java_nnnn11 小时前
LeetCode刷题滑动窗口(串联所有单词的子串+最小覆盖的子串)Java
算法·leetcode·职场和发展
Navigator_Z12 小时前
LeetCode //C - 1301. Number of Paths with Max Score
c语言·算法·leetcode
卡卡~~12 小时前
“101计划”核心课程实践教材《数据实验结构指导-C++语言版》题目集
开发语言·c++·学习·算法
企业数字化笔记13 小时前
AI视频增强后画质怎么验收?插值基线、伪细节与帧间闪烁的检查方法
人工智能·算法·音视频