整数排序
来源:互联网 发布:网络销售工作总结 编辑:程序博客网 时间:2024/06/08 10:11
问题描述:给一组整数,按照升序排序,使用选择排序,冒泡排序,插入排序或者任何 O(n2) 的排序算法。
样例:对于数组 [3, 2, 1, 4, 5]
, 排序后为:[1, 2, 3, 4, 5]
。
实现思路:我选用了直接插入排序算法,遍历数组,从A[i]开始往前比,如果遇到A[i]<A[i-1],那么交换两者并将i-1,直到i=0位置停止。
实现代码:
class Solution {
public:
/**
* @param A an integer array
* @return void
*/
void sortIntegers(vector<int>& A) {
// Write your code here
for(int i=1;i<A.size();i++)
{
while(i>0&&A[i]<A[i-1])
{
swap(A[i],A[i-1]);
i--;
}
}
}
};
做题感想:一开始外层循环是从i=1开始的,只有64%数据能通过。
阅读全文
0 0
- 整数排序
- 整数排序
- 整数排序
- 整数排序
- 整数排序
- 整数排序
- 整数排序
- 整数排序
- 整数排序
- 整数排序
- 整数排序
- 整数排序
- 整数排序
- 整数排序
- 整数排序
- 整数排序
- 整数排序
- 整数排序
- 解决:Error:Conflict with dependency 'com.google.code.findbugs:jsr305' in project ':app'.错误!
- OPENGL—参数裁剪(Liang-Barsky算法)
- 自定义View中遇到的那些坑
- Python自动化脚本splinter框架chromedriver的安装
- Java类集框架之Map映射接口
- 整数排序
- JavaEE学习--javascript中的正则表达式
- opencv3.2 SURF实现特征点匹配
- redis在Java web项目的简单应用
- RadioGroup和CheckBox设置背景以及4.4系统上隐藏默认样式的坑
- Windows 系统如何在有密码的情况下自动登录系统
- C. An impassioned circulation of affection
- 【LeetCode】 Search in Rotated Sorted Array 系列
- 数字位数