Love丶FFC's Blog

LintCode_naive:整数排序

2019-08-18 15:59:49
阅读:516   •   评论:3
标签:,

描述

给一组整数,按照升序排序,使用选择排序,冒泡排序,插入排序或者任何 O(n2) 的排序算法。

样例

样例1

输入:  [3, 2, 1, 4, 5]
输出:  [1, 2, 3, 4, 5]
	
样例解释: 
返回排序后的数组。

样例2

输入:  [1, 1, 2, 1, 1]
输出:  [1, 1, 1, 1, 2]
	
样例解释: 
返回排好序的数组。

编程语言:Java

解题思想:

1.冒泡排序

2.使用双层for循环逐一比较各个元素的大小,并交换次序。

时间复杂度:O(n^2)

IDE代码如下:

  1. package Lintcode;
  2.  
  3. import java.util.Arrays;
  4.  
  5. public class _463 {
  6.  
  7. public static void main(String[] args) {
  8. int[] Number = new int[] {1,9,3,4,5};
  9. for(int i=0;i<Number.length;i++)
  10. {
  11. for(int j=i;j<Number.length;j++) //j=0换成j=i可减少循环次数
  12. {
  13. if(Number[i]>Number[j])
  14. {
  15. int swap;
  16. swap=Number[i]; //交换次序
  17. Number[i]=Number[j];
  18. Number[j]=swap;
  19. }
  20. }
  21. }
  22. System.out.println(Arrays.toString(Number));
  23. }
  24. }
  25.  

LintCode编辑器代码如下:

  1. public class Solution {
  2. public void sortIntegers(int[] A) {
  3. for(int i=0;i<A.length;i++)
  4. {
  5. for(int j=i;j<A.length;j++)
  6. {
  7. if(A[i]>A[j])
  8. {
  9. int swap;
  10. swap=A[i];
  11. A[i]=A[j];
  12. A[j]=swap;
  13. }
  14. }
  15. }
  16. System.out.println(Arrays.toString(A));
  17. }
  18. }

评论板

共有 3 条评论

  1. fruinly

    MCF7 T cells growth rate was almost doubled compared with parental, while T47D T and BT 474 T decreased compared with parental lines where to buy cialis cheap

  2. FoorryNox

    2F, suggesting that triptonide is a novel, potent inhibitor of TNBC cell vascular mimicry cialis viagra combo pack

  3. Preliaf

    cialis online ordering MA Cobleigh, CL Vogel, D Tripathy, etal Multinational study of the efficacy and safety of humanized anti HER2 monoclonal antibody in women who have HER2 overexpressing metastatic breast cancer that has progressed after chemotherapy for metastatic disease J Clin Oncol 17 2639 2648, 1999 Link, Google Scholar 15

--------查看该分类下最新文章--------
^
新版博客正在完善中!域名:http://www.loveffc:8080,点击跳转,完全移植后将去除端口号。

Copyright © 2018 - 2021 FFC的小站 - 滇 ICP 备 18010780 号 - 1

- Powered by WordPress & AliYun · Theme by FFC -

- Environment by Windows & XAMPP · Designed by WebStorm & VSCode -

已运行:

访问量:498283