1/排序算法:冒泡排序bubble sort,插入排序 insertion sort,选择排序 selection sort,快速排序 quick sort,归并排序 merge sort;堆排序 heap sort---基于排序

桶排序bucket sort  一种特殊情况下的排序。

2/实现

1)冒泡排序bubble sort:从位置0开始,一次比较到length - 0,前面大交换;再从位置1开始,依次类推。

 public void bubbleSort(int[] nums) {
for (int i = 0; i < nums.length; i++) {
for (int j = 1; j < nums.length - i; j++) {
if (num[j - 1] > nums[j]) {
swap(nums, j - 1, j);
}
}
}
} public void swap(int[] nums, int i , int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}

冒泡排序有一个优化算法,就是在一个排好序的数组,一轮比较没有swap,则是排好序的,那么直接返回

  1 public void bubbleSort(int[] nums) {
2 for (int i = 0; i < nums.length; i++) {
boolean isSwap = false;
3 for (int j = 1; j < nums.length - i; j++) {
4 if (num[j - 1] > nums[j]) {
5 swap(nums, j - 1, j);
isSwap = true;
6 }
7 }
if (!isSwap) {
return;
}
8 }
9 }
10
11 public void swap(int[] nums, int i , int j) {
12 int temp = nums[i];
13 nums[i] = nums[j];
14 nums[j] = temp;
15 }

2)插入排序insertion sort:先把前i个数字排好序,然后再把前i+1个数字排好序。

 public void insertSort(int[] nums) {
for (int i = 1; i < nums.length; i++) {
for (int j = i; j > 0; j --) {
if (nums[j - 1] > nums[j ]) {
swap(nums, j - 1; j);
}
}
}
} public void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}

3)选择排序selection sort:先从N里面选出最小的一个值,与0位置数字交换,再从剩下数字里面选出最小的值跟1位置交换。依次类推。

 public void selectionSort(int[] nums) {
for (int i = 0; i < nums.length - 1; i++) {//i < nums.length -1,j要从i + 1开始
int min = nums[i];
int minIndex = i;
for (int j = i + 1; j > nums.length; j++) {
if (num[j] < min) {
min = num[j];
minIndex = j;
}
}
num[minIndex] = num[i];
nums[i] = min;
}
}

4)快速排序quick sort:

 public void quickSort(int[] nums) {
sort(nums, 0, nums.length);
} publc void sort(int[] nums, int begin, int end) {
if (begin >= end) {
return;
}
int pivotIndex = partition(nums, begin, end);
sort(nums, begin, pivotIndex - 1);//pivot已经就位,所以跳过
sort(nums, pivotIndex + 1, end);
} public partition(int[] nums, int begin, int end) {
int pivot = num[begin];
while (begin < end) {
while (begin < end && num[end] > pivot) {
end--;
}
nums[begin] = nums[end];
while (begin < end && nums[begind] <= pivot) {
begin++;
}
nums[end] = nums[begin];
}
nums[begin] = pivot;
return begin;
}

Lecture--9 Sorting的更多相关文章

  1. Google Interview University - 坚持完成这套学习手册,你就可以去 Google 面试了

    作者:Glowin链接:https://zhuanlan.zhihu.com/p/22881223来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 原文地址:Google ...

  2. [C2P3] Andrew Ng - Machine Learning

    ##Advice for Applying Machine Learning Applying machine learning in practice is not always straightf ...

  3. HDU Cow Sorting (树状数组)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1  ...

  4. 1306. Sorting Algorithm 2016 12 30

    1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...

  5. 算法:POJ1007 DNA sorting

    这题比较简单,重点应该在如何减少循环次数. package practice; import java.io.BufferedInputStream; import java.util.Map; im ...

  6. note of introduction of Algorithms(Lecture 3 - Part1)

    Lecture 3(part 1) Divide and conquer 1. the general paradim of algrithm as bellow: 1. divide the pro ...

  7. U3D sorting layer, sort order, order in layer, layer深入辨析

    1,layer是对游戏中所有物体的分类别划分,如UIlayer, waterlayer, 3DModelLayer, smallAssetsLayer, effectLayer等.将不同类的物体划分到 ...

  8. WebGrid with filtering, paging and sorting 【转】

    WebGrid with filtering, paging and sorting by Jose M. Aguilar on April 24, 2012 in Web Development A ...

  9. ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting 【转】

    ASP.NET MVC WebGrid – Performing true AJAX pagination and sorting FEBRUARY 27, 2012 14 COMMENTS WebG ...

  10. codeforces 499B.Lecture 解题报告

    题目链接:http://codeforces.com/problemset/problem/499/B 题目意思:给出两种语言下 m 个单词表(word1, word2)的一一对应,以及 profes ...

随机推荐

  1. 进程中t.start(), t.daemon() t.jion()的使用

    #!/usr/bin/env python import multiprocessing import time def f1(a1): time.sleep(2) print(a1) if __na ...

  2. 【总结整理】javascript的函数调用时是否加括号

    javascript的函数调用时是否加括号 if(event.preventDefault){ event.preventDefault(); if判断条件里面不要加括号,不加括号是应该以属性形式,i ...

  3. SpringSecurity04 利用JPA和SpringSecurity实现前后端分离的认证和授权

    1 环境搭建 1.1 环境说明 JDK:1.8 MAVEN:3.5 SpringBoot:2.0.4 SpringSecurity:5.0.7 IDEA:2017.02旗舰版 1.2 环境搭建 创建一 ...

  4. 功能:formatter 表单提交 拼接参数的形式

    datagarid的formatter属性 value: 代表当前单元格中的值.row:代表当前行.index: 代表当前行的下标. {field:'is_hot',title:'是否热门',widt ...

  5. 11、perl语言的记录分割符$/ $\

    参考:https://www.jianshu.com/p/8ffd53b1bd41 perl自带的perldoc文档.perldoc -v '$/'  :perldoc -f join $/ #输入记 ...

  6. SPOJ - REPEATS Repeats (后缀数组+RMQ)

    题意:求一个串中出现重复子串次数最多的数目. 析:枚举每个长度的子串,至少要重复两次,必然会经过s[l*i]中相邻的两个,然后再分别向前和向后匹配即可. 代码如下: #pragma comment(l ...

  7. MySQL 文件导入出错

    ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot exec ...

  8. Linux 最小系统挂载U盘(SD、TF卡)并执行程序

    一.在Ubuntu下编译C文件 使用指令"arm-none-linux-gnueabi-gcc-4.4.1 -o HelloWorld HelloWorld.c -static"编 ...

  9. PHP开源系统学习之fluxbb_2

    谴责下某位同学,转载了我的上一篇文章,也不给个原文地址,希望这次再来转时能加上. //检查登录,在common.php判断 //cookie串: 2|dc4fab5bb354be5104bae0aff ...

  10. SQL Server 2012 安装——安装 OR 卸载

    前言     上篇介绍了.net framework 3.5的安装,这次介绍一下,SQL Server2012的安装和彻底卸载.根据百度,下面是自己根据自己实际情况整理的安装步骤: 安装 见安装步骤( ...