Given an integer array, sort it in ascending order. Use selection sort, bubble sort, insertion sort or any O(n2) algorithm.

 
Example

Given [3, 2, 1, 4, 5], return [1, 2, 3, 4, 5].

这道题让我们实现最基本的几个O(n2)的排序算法,选择排序,冒泡排序和插入排序,都是最基本的排序算法。我们一个一个来看,首先来看冒泡排序,算法思路很简单,遍历数组,把当前数字的后面所有的数字都遍历一遍,遇到小的跟当前数字交换,这样遍历的过程中,所有大的数字就像气泡一样都到数组的后面去了,这也是为啥叫冒泡排序的原因,参见代码如下:

解法一:

// Bubble sort
class Solution {
public:
/**
* @param A an integer array
* @return void
*/
void sortIntegers(vector<int>& A) {
for (int i = ; i < A.size(); ++i) {
for (int j = i + ; j < A.size(); ++j) {
if (A[i] > A[j]) {
swap(A[i], A[j]);
}
}
}
}
};

下面来看插入排序,算法思路是遍历数组,从A[i]开始往前比,如果遇到A[i] < A[i - 1],那么交换两者并--i,直到i=0位置停止,参见代码如下:

解法二:

// Insertion sort
class Solution {
public:
/**
* @param A an integer array
* @return void
*/
void sortIntegers(vector<int>& A) {
for (int i = ; i < A.size(); ++i) {
while (i > && A[i] < A[i - ]) {
swap(A[i], A[i - ]);
--i;
}
}
}
};

选择排序也不难,思路是遍历数组,对于当前位置i,我们定义一个变量min_idx,用来记录当前位置往后的最小值的坐标,我们通过遍历之后所有的数字来找到最小值的坐标,然后交换A[i]和A[min_idx]即可,参见代码如下:

解法三:

// Selection sort
class Solution {
public:
/**
* @param A an integer array
* @return void
*/
void sortIntegers(vector<int>& A) {
for (int i = ; i < A.size(); ++i) {
int min_idx = i;
for (int j = i + ; j < A.size(); ++j) {
if (A[j] < A[min_idx]) {
min_idx = j;
}
}
swap(A[i], A[min_idx]);
}
}
};

参考资料:

https://luqinblog.wordpress.com/2016/02/29/sort-integers/

[LintCode] Sort Integers 整数排序的更多相关文章

  1. [LintCode] Sort Integers II 整数排序之二

    Given an integer array, sort it in ascending order. Use quick sort, merge sort, heap sort or any O(n ...

  2. [LintCode] Sort List 链表排序

    Sort a linked list in O(n log n) time using constant space complexity. Have you met this question in ...

  3. sort 树 hash 排序

    STL 中 sort 函数用法简介 做 ACM 题的时候,排序是一种经常要用到的操作.如果每次都自己写个冒泡之类的 O(n^2) 排序,不但程序容易超时,而且浪费宝贵的比赛时间,还很有可能写错. ST ...

  4. C语言:10个整数排序(别忘了负数)

    题目内容: 10个整数排序(别忘了负数) 例如 input 1 0 2 0 3 4 1 9 8 7 output 0 0 1 1 2 3 4 7 8 9 编码: void sort(int *a); ...

  5. YTU 2427: C语言习题 整数排序

    2427: C语言习题 整数排序 时间限制: 1 Sec  内存限制: 128 MB 提交: 391  解决: 282 题目描述 用指向指针的指针的方法对n个整数排序并输出.要求将排序单独写成一个函数 ...

  6. 九度oj 题目1190:大整数排序

    题目1190:大整数排序 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:4142 解决:1867 题目描述: 对N个长度最长可达到1000的数进行排序. 输入: 输入第一行为一个整数N,( ...

  7. 【九度OJ】题目1190:大整数排序 解题报告

    [九度OJ]题目1190:大整数排序 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1190 题目描述: 对N个长度最长可达 ...

  8. bitmap对海量无重复的整数排序--转

    原文地址:http://blog.csdn.net/u013074465/article/details/46956295 现在有n个无重复的正整数(n 小于10的7次方),如果内存限制在1.5M以内 ...

  9. 考查SQLite 3索引对整数排序的性能影响

    做个实验,想了解SQLite3索引对整数排序的性能影响. 用这个测试表,考查绿色那列: id name date 自增型主键 字符串型,随机生成 整数型 随机生成,范围0到54354354 1 bMz ...

随机推荐

  1. The Basics of 3D Printing in 2015 - from someone with 16 WHOLE HOURS' experience

    全文转载自 Scott Hanselman的博文. I bought a 3D printer on Friday, specifically a Printrbot Simple Metal fro ...

  2. 类模板Queue的实现

    #include <iostream> #include <vector> using namespace std; template <class Type> c ...

  3. SQLServer批量创建有规则的数据

    根据需求要生成从kkk001,kkk002开始的100个递增账号 手插要死人的,用SQL脚本轻松完成: declare @a int ) ) begin ) ) end declare:申明变量, @ ...

  4. 【spring bean】spring中bean的懒加载和depends-on属性设置

    项目结构如下: ResourceBean.java代码: package com.it.res; import java.io.File; import java.io.FileNotFoundExc ...

  5. 临时变量不能作为非const类型引用形参的实参

    摘要:     非const 引用形参只能与完全同类型的非const对象关联.      具体含义为:(1)不能用const类型的对象传递给非const引用形参:                  ( ...

  6. 第二个div+css前端项目

    先展示效果图: 为了看全景,截图有点挫.实际效果比这个好一点. 通过 text-overflow可以隐藏多出的文字,而不会吧把div撑开或者溢出. html代码: <!DOCTYPE html& ...

  7. 到程序集里取DLL

    C:\Windows\assembly\gac_msil

  8. 怎么提高Jquery性能

    很久没有关注jQuery了,最近重新看了一下,看到一些不错的文章,转来坐一下笔记. 其内容和一些新提供的方法还是很多有值得学习的地方. 1. 使用最新版本的jQuery jQuery的版本更新很快,你 ...

  9. git merge 与 rebase 的区别

    http://gitbook.liuhui998.com/4_2.html merge rebase

  10. Java在ACM中的应用

    Java在ACM中的应用 —. 在java中的基本头文件(java中叫包) import java.io.*; import java.util.*; //输入Scanner import java. ...