当前位置:网站首页>选择排序
选择排序
2022-06-12 21:02:00 【又秃又弱】
思想:
i:即将要放的位置下标,从0地址开始依次向后走
j:从初始位置依次遍历查找最小值
min:先赋第一个值为初值,通过一个个比较,更替最小值min
minIndex:保存最小值的下标
//minIndex存在的意义:当查找到当前无序序列中最小值min后,如果直接将min与i位置交换,会导致原序列改变(eg:123045->023045,min=1)
从有序序列内选出最小序列,将最小的放到最前面(放到i处),
j目前为当前下标,与min比较。找到最小值后,保存下标,i与min交换,
代码实现:
#include<stdio.h>
#include<assert.h>
void Swap1(int *p,int*q)
{
int temp = *p;
*p = *q;
*q = temp;
}
//补充,对于一个数组不同下标的交换
void Swap2(int *arr,int index1,int index2)
{
int temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
void SelsectSort(int* arr, int length) //对数组本身操作,不需要返回值Void
{
assert(arr != 0);//断言,如有输出错误直接弹出弹窗
int min = arr[0], minIndex;
int j;
for (int i = 0; i < length; i++)
{
for (j = i+1; j < length; j++)//i的位置是前面已排好序
{
if (arr[j] < min)
{
min = arr[j];
minIndex = j;
}
}
Swap1(&arr[minIndex],&arr[i]);
//Swap2(arr,&index,&i);
}
}
void Show(int *arr,int length)
{
for (int i = 0; i < length; i++)
printf("% d ", arr[i]);
//return *arr;
}
int main()
{
int arr[] = { 3,5,0 };
int length = sizeof(arr) / sizeof(arr[0]);
SelsectSort(arr, length);
Show(arr, length);
return 0;
}
边栏推荐
- China hydraulic press market trend report, technical innovation and market forecast
- Foreign brands become a thing of the past? What are the key words of the TV industry in 2022?
- [tutorial] Firefox send: deployment method of Firefox open source temporary file sharing service platform
- Data visualization - Calendar chart
- JS deep and shallow copy
- 作用域和作用域链
- Lombok package is successfully installed, but the runtime prompts that get, set method and constructor solution cannot be found
- Is it really possible to find a testing job with a monthly income of more than 10000 without a degree and self-study software testing?
- #141 Linked List Cycle
- SAP WM preliminary transaction code lx29 - list of fixed storage bins
猜你喜欢
Typescript definition type: type 'timeout' cannot be assigned to type 'number';
torch. nn. Linear() function
Data visualization - histogram
Algorinote_ 2_ Main theorem and Akra bazzi theorem
阿里前辈给我推荐的软件测试人员必读书籍(附电子书),让我受益匪浅...
Li Mu [practical machine learning] 1.4 data annotation
Solve the cvxpy error the solver GLPK_ MI is not installed
Is it really possible to find a testing job with a monthly income of more than 10000 without a degree and self-study software testing?
(11) Image frequency domain filtering with OpenCV
GPU giant NVIDIA suffered a "devastating" network attack, and the number one malware shut down its botnet infrastructure | global network security hotspot on February 28
随机推荐
A blog written clearly by vit
UVa11991 Easy Problem from Rujia Liu
Draw according to weight
Lake shore PT-100 platinum resistance temperature sensor
torch. Finfo function
JS中如何实现重载
#981 Time Based Key-Value Store
Understanding of functions
String Basics
Leetcode: 210. Programme II
Double carbon in every direction: green demand and competition focus in the calculation from the east to the West
Data visualization - Calendar chart
EU officially released the data act, Ukraine was attacked by DDoS again, kitchen appliance giant Meiya was attacked, internal data leakage network security weekly
Transaction code qs28 of SAP QM preliminary level
Shell language
InRelease: 由于没有公钥,无法验证下列签名: NO_PUBKEY EB3E94ADBE1229CF
Design and practice of Hudi bucket index in byte skipping
Compréhension préliminaire des expressions régulières cognitives (regex)
JSON file handles object Tags
USB机械键盘改蓝牙键盘