当前位置:网站首页>力扣912排序数组笔记
力扣912排序数组笔记
2022-07-17 07:33:00 【雷霆小嘎巴】
给你一个整数数组 nums,请你将该数组升序排列。
示例 1:
输入:nums = [5,2,3,1]
输出:[1,2,3,5]
示例 2:
输入:nums = [5,1,1,2,0,0]
输出:[0,0,1,1,2,5]
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/sort-an-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
首先,我用的是冒泡排序法(目前只知道这种),但是最后一个没通过(数字特别多),有限时间内不能得到结果,代码如下。
class Solution { public int[] sortArray(int[] nums) { int a = 0; for(int i=0;i<nums.length-1;i++){ //一共进行num.length次循环排序 boolean b=false;//这里用bool值以便减少不必要的循环次数 for(int j=0;j<nums.length-1-i;j++){ //每次排序都会把最大值移动到最右端,所以下一次右边的都不用排序,进行nums.length-1-i轮循环 if(nums[j+1]<nums[j]){ a=nums[j]; nums[j]=nums[j+1]; nums[j+1]=a; b=true; } //进行大小值换位 } if(b==false){ break; } //用bool值来判断此轮循环是否有必要,没必要则跳出循环 } return nums; } }
在题解中了解到了选择排序法,但是试过这种方法还是过不了,时间太久,感觉和冒泡排序法有相似之处,代码如下。
class Solution { // 选择排序:每一轮选择最小元素交换到未排定部分的开头 public int[] sortArray(int[] nums) { int len = nums.length; // 循环不变量:[0, i) 有序,且该区间里所有元素就是最终排定的样子 for (int i = 0; i < len - 1; i++) { // 选择区间 [i, len - 1] 里最小的元素的索引,交换到下标 i int minIndex = i; for (int j = i + 1; j < len; j++) { if (nums[j] < nums[minIndex]) { minIndex = j; } } swap(nums, i, minIndex); } return nums; } private void swap(int[] nums, int index1, int index2) { int temp = nums[index1]; nums[index1] = nums[index2]; nums[index2] = temp; } //交换元素的函数 }
然后又了解了插入排序法
优化:「将一个数字插入一个有序的数组」这一步,可以不使用逐步交换,使用先赋值给「临时变量」,然后「适当的元素」后移,空出一个位置,最后把「临时变量」赋值给这个空位的策略
public class Solution { // 插入排序:稳定排序,在接近有序的情况下,表现优异 public int[] sortArray(int[] nums) { int len = nums.length; // 循环不变量:将 nums[i] 插入到区间 [0, i) 使之成为有序数组 for (int i = 1; i < len; i++) { // 从第二个元素开始,先暂存这个元素,然后之前元素逐个后移,留出空位 int temp = nums[i]; int j = i; // 注意边界 j > 0 并且当前面的元素比后面的元素(暂存元素)大时,此元素后面的整个数列向后移一位 while (j > 0 && nums[j - 1] > temp) { nums[j] = nums[j - 1]; j--; } nums[j] = temp;//此时j为后半部分向后移的数列首个元素下标 } return nums; } }
边栏推荐
- Can seaport and erc-4907 become new ways to release NFT liquidity| Tokenview
- Strategic model of behavioral model
- Redis cluster
- C # read and write txt files
- Redis message subscription
- 5.1 安全漏洞與防範
- unity 自定义天空球模型防止被裁剪
- Redis distributed lock
- Viewing the technology stack of distributed system from the crash report of station B
- Use of OpenCV polar transformation function warppolar
猜你喜欢
Unity: window size adaptation when running on the browser after webgl Publishing
Redis新数据类型——Bitmaps
黑马程序员-软件测试-16阶段3-功能测试-175-198,URL组成介绍,请求内容以及组成说明行功能测试与数据库,url组成扩展说明,客户端与服务器请求与响应,-Fiddler按照以及功能检查确认,
Quanzhi v3s learning record (13) use of ov2640
Unity: WebGL发布后在浏览器上运行时窗口大小自适应
畅玩JVM——关于GC垃圾回收必须要掌握的知识
New redis6 features
美国压力激增,TikTok 更换全球安全主管
写代码遇到Qt相关问题
深度学习之 7 深度前馈网络2
随机推荐
Unity: window size adaptation when running on the browser after webgl Publishing
Redis的发布和订阅
Viewing the technology stack of distributed system from the crash report of station B
Complete square number
WVPPRO-ZLM-GB21818-摄像头
5G正当时,无人驾驶未来将驶向何方?
没那么大的组合数
With this "programmer code interview guide" from Zuo Chengyun (Zuo Shen), I joined byte
Deep learning 7 deep feedforward network
C#对txt文件的读写操作
Detailed explanation of type, user-defined type, preliminary understanding of structure
JS学习笔记09-12:原型对象以及Foreach+tostring及回收站
5.2 数据库安全
STM32CUBEIDE(9)----USART通过DMA收发
RestTemplate
Dark horse programmer - software testing -16 stage 3 - function testing -175-198, URL composition introduction, request content and composition description line function test and database, URL composi
Redis publishing and subscription
最新一代互联网:WEB 3.0
Can seaport and erc-4907 become new ways to release NFT liquidity| Tokenview
Redis6 新数据类型——Geospatial