当前位置:网站首页>各种排序的复习笔记
各种排序的复习笔记
2022-04-21 13:49:00 【招桃花都没用】
1、直接插入排序
int* sortedSquares(int* a, int n, int* returnSize){
*returnSize=n;
if(n==0) return a;
a[0]=a[0]*a[0];
int j;
for(int i=1;i<n;i++){
int temp=a[i]*a[i];
for(j=i-1;j>=0;j--){
if(temp<a[j]) a[j+1]=a[j];
else break;
}
a[j+1]=temp;
}
return a;
}
2、折半插入排序
int* sortedSquares(int* a, int n, int* returnSize){
*returnSize=n;
if(n==0) return a;
a[0]=a[0]*a[0];
int j;
for(int i=1;i<n;i++){
int temp=a[i]*a[i];
int low=0,high=i-1;
while(low<=high){
int mid=(low+high)/2;
if(temp>a[mid]) low=mid+1;
else high=mi-1;
}
for(j=i-1;j>high;j--){
a[j+1]=a[j];
}
a[j+1]=temp;
}
return a;
}
3、选择排序
int* sortedSquares(int* a, int n, int* returnSize){
*returnSize=n;
if(n==0) return a;
int k=0;
for(int i=0;i<n;i++){
a[k++]=a[i]*a[i];
}
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if(a[j]<a[i]){
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
return a;
}
4、冒泡排序
int* sortedSquares(int* a, int n, int* returnSize){
*returnSize=n;
if(n==0) return a;
int k=0;
for(int i=0;i<n;i++){
a[k++]=a[i]*a[i];
}
for(int i=0;i<n;i++){
for(int j=0;j<n-i-1;j++){
if(a[j+1]<a[j]){
int temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
return a;
}
5、带判断条件的冒泡排序
int* sortedSquares(int* a, int n, int* returnSize){
*returnSize=n;
if(n==0) return a;
int k=0;
for(int i=0;i<n;i++){
a[k++]=a[i]*a[i];
}
int flag=1;
while(n>1&&flag==1){
flag=0;
for(int j=0;j<n-1;j++){
if(a[j+1]<a[j]){
flag=1;
int temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;
}
}
}
return a;
}
选择排序 和冒泡排序会时间超限
6、快排
int Pattion(int *a,int low,int high){
int temp=a[low],pivoty=a[low];
while(low<high){
while(a[high]>=pivoty&&low<high) high--;
a[low]=a[high];
while(a[low]<=pivoty&&low<high) low++;
a[high]=a[low];
}
a[low]=temp;
return low;
}
void QuickSort(int *a,int low,int high){
int p;
if(low<high){
p=Pattion(a,low,high);
QuickSort(a,low,p-1);
QuickSort(a,p+1,high);
}
}
int* sortedSquares(int* a, int n, int* returnSize){
*returnSize=n;
if(n==0) return a;
int k=0;
for(int i=0;i<n;i++){
a[k++]=a[i]*a[i];
}
QuickSort(a,0,n-1);
return a;
}
7、归并排序
void MergeSort(int *a,int low,int mid,int high){
int *b=(int *)malloc(sizeof(int)*(high+1));
for(int i=low;i<=high;i++){
b[i]=a[i];
}
int i=low,j=mid+1,temp=low;
while(i<=mid&&j<=high){
if(b[i]<b[j]){
a[temp++]=b[i++];
}
else a[temp++]=b[j++];
}
while(i<=mid) a[temp++]=b[i++];
while(j<=high) a[temp++]=b[j++];
}
void Merge(int *a,int low,int high){
int mid;
if(low==high)return ;
else{
mid=(low+high)/2;
Merge(a,low,mid);
Merge(a,mid+1,high);
MergeSort(a,low,mid,high);
}
}
int* sortedSquares(int* a, int n, int* returnSize){
*returnSize=n;
if(n==0) return a;
int k=0;
for(int i=0;i<n;i++){
a[k++]=a[i]*a[i];
}
Merge(a,0,n-1);
return a;
}
8、双指针
int* sortedSquares(int* a, int n, int* returnSize){
*returnSize=n;
if(n==0) return a;
int p=0,q=-1;
for(int i=0;i<n;i++){
if(a[i]>=0) {
p=i;
q=i-1;
break;
}
}
int k=0;
int *res=(int *)malloc(sizeof(int)*n);
while(p<n&&q>=0){
if(a[p]*a[p]<a[q]*a[q]){
res[k++]=a[p]*a[p];
p++;
}
else{
res[k++]=a[q]*a[q];
q--;
}
}
while(p<n) {
res[k++]=a[p]*a[p];
p++;
}
while(q>=0){
res[k++]=a[q]*a[q];
q--;
}
版权声明
本文为[招桃花都没用]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_45939887/article/details/124109321
边栏推荐
- 第三个项目——PacMan(2)
- EsgynDB 关于收集core信息的小技巧
- Unittest单元测试(一)
- 2021-10-21软件测试理论
- String string dynamic change (10 points): in the following procedures, the function of fun is to find the character with the largest ASCII code value in the string STR, move all the characters before
- EsgynDB CQD-traf_ lock_ ddl
- Summary of several problems of esgyndb using JDBC UDR to access remote trafodion
- Machine learning notes - Moore Penrose pseudo inverse
- 【leetcode】144. Preorder traversal of binary tree
- Vagrant detailed tutorial
猜你喜欢

基于数值微分和误差反向传播的比较

Explication détaillée du mécanisme d'allocation de mémoire JVM

STM32单片机初学5-IIC通信驱动OLED屏幕
![[special topic of stack and queue] - Dual queue simulation stack](/img/5f/241804f418487e779f8bd762df5556.png)
[special topic of stack and queue] - Dual queue simulation stack

SECOND: Sparsely Embedded Convolutional Detection

Markdown grammar and test

JVM内存模型深度剖析与优化

Unittest单元测试(二)

<2021SC@SDUSC>山东大学软件工程应用与实践JPress代码分析(八)

电力系统相关知识
随机推荐
图像分类的训练基本过程——以MobileNet_v3为例
Explication détaillée du mécanisme d'allocation de mémoire JVM
Filter string only retains the alphabetic characters in the string (10 points). Please write a function fun. The function is to enter a string, filter the string, only retain the alphabetic characters
Fengqiu technology provides you with 10M Ethernet solution
Vagrant detailed tutorial
promise---几个关键问题
MySQL slow query log and index merge analysis
String - 1. Longueur de la chaîne (10 points) La Bibliothèque de fonctions standard du langage C comprend une fonction strlen qui calcule la longueur de la chaîne. Comme exercice, nous écrivons nous -
颜色渐变(柱子、圆环等)
STM32单片机初学5-IIC通信驱动OLED屏幕
数据库基础篇
深度学习与图像识别:原理与实践 笔记Day_18(目标检测)
EsgynDB 关于收集core信息的小技巧
EsgynDB CQD-traf_ lock_ ddl
《商用密码应用与安全性评估》第一章 密码基础知识-小结
基于VScode的ESP32开发学习(五):用户自定事件循环详解,dedicated task
并发编程之深入理解
EsgynDB 关于带索引的DELETE性能提升
String - 1 String length (10 points) the C language standard function library includes the strlen function, which is used to calculate the length of the string. As an exercise, we write a function wit
深度学习与图像识别:原理与实践 笔记Day_14