当前位置:网站首页>选择排序
选择排序
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;
}
边栏推荐
- Do we media video, and share the necessary app for friendly new media operation
- Pytorch how to set random number seed to make the result repeatable
- Typescript definition type: type 'timeout' cannot be assigned to type 'number';
- 作用域和作用域链
- EDI 855 purchase order confirmation
- JS深浅拷贝
- The year of the outbreak of financial innovation! All dtinsight products of kangaroo cloud data stack have passed the special test of Xinchuang of ICT Institute
- String Basics
- Transaction code qs28 of SAP QM preliminary level
- What's a good gift for the goddess Festival? Gift recommendation for the goddess Festival on March 8
猜你喜欢

#113 Path Sum II

(11) Image frequency domain filtering with OpenCV

#141 Linked List Cycle

leetcode:210. 課程錶 II

Scala基础语法入门(三)Scala中的各种运算符

半路自学测试成功转行,第一份测试工作就拿10k

In the spring recruitment of 2022, the test engineer will have a full set of interview strategies to thoroughly understand all the technical stacks (all dry goods)

leetcode:210. 课程表 II

Data visualization diagram microblog forwarding diagram

A Zhu and Xu Baobao's high-rise game - difference
随机推荐
Double carbon in every direction: green demand and competition focus in the calculation from the east to the West
How to determine the sample size of an inspection lot in SAP QM's initial sampling strategy?
做自媒体视频,友好的新媒体运营必备app分享
测试人如何规划自己的未来?才能实现入行2年达到25k?
Typescript definition type: type 'timeout' cannot be assigned to type 'number';
#886 Possible Bipartition
(11) Image frequency domain filtering with OpenCV
#141 Linked List Cycle
The salted fish has been transmitted for 5W times, and the latest spring recruit face-to-face test questions of bytes have been leaked
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
JSP中的监听器
JS中如何实现重载
Mxnet record IO details
在同花顺开户安全么,买股票怎么网上开户
#141 Linked List Cycle
Troubleshooting of service port failure
Research Report on market supply and demand and strategy of China's hydraulic injection molding machine industry
How can CTCM in the inspection lot system status of SAP QM be eliminated?
[tutorial] Firefox send: deployment method of Firefox open source temporary file sharing service platform
My way of programming