排序算法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;
}
相关推荐
无极低码2 小时前
ecGlypher新手安装分步指南(标准化流程)
人工智能·算法·自然语言处理·大模型·rag
软件算法开发3 小时前
基于海象优化算法的LSTM网络模型(WOA-LSTM)的一维时间序列预测matlab仿真
算法·matlab·lstm·一维时间序列预测·woa-lstm·海象优化
罗超驿3 小时前
独立实现双向链表_LinkedList
java·数据结构·链表·linkedlist
superior tigre3 小时前
22 括号生成
算法·深度优先
努力也学不会java4 小时前
【缓存算法】一篇文章带你彻底搞懂面试高频题LRU/LFU
java·数据结构·人工智能·算法·缓存·面试
旖-旎5 小时前
二分查找(x的平方根)(4)
c++·算法·二分查找·力扣·双指针
ECT-OS-JiuHuaShan5 小时前
朱梁万有递归元定理,重构《易经》
算法·重构
智者知已应修善业6 小时前
【51单片机独立按键控制数码管移动反向,2片74CH573/74CH273段和位,按键按下保持原状态】2023-3-25
经验分享·笔记·单片机·嵌入式硬件·算法·51单片机
khddvbe6 小时前
C++并发编程中的死锁避免
开发语言·c++·算法
C羊驼6 小时前
C语言:两天打鱼,三天晒网
c语言·经验分享·笔记·算法·青少年编程