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. Poj 1067 取石子游戏(NIM,威佐夫博奕)

    一.Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子. ...

  2. HDOJ1677(铺砖问题)

    Nested Dolls Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  3. 问题5:如何快速找到多个字典中的公共键(key)

    方法一:for in循环 from random import randint, sample a1 = {k; randint(1, 4) for k in 'abcdefg'} a2 = {k; ...

  4. 【转】 Pro Android学习笔记(四八):ActionBar(1):Home图标区

    目录(?)[-] Home Icon 源代码 TextView的滚动 返回主activity或指定activity     ActionBar在Android 3.0 SDK中为平板引入,在4.0中也 ...

  5. 与input有关的一些操作

    单选 : 通过name指定为一组,只能选择一个  一组一个选项 <input type=" name="sex"/>男 <input type=&quo ...

  6. .net wcf调用java的需要认证的接口

    1.wcf直接添加java的webservice地址,这都是常规操作,没必要好说 2.修改config配置文件,添加headers消息头节点,这个需要注意 3.OK直接调用里面的方法即可,全部搞定 & ...

  7. asp后端弹出框

    RegisterStartupScript("提示信息", "<script>alert('Hello')</script>"); 这样 ...

  8. .net 缓存之数据库缓存依赖

    当监听的指定数据库内容某张表变化时就更新缓存 先来配置数据库,启动监听服务(SQL2008下) 执行如下语句: ALTER DATABASE OumindBlog SET NEW_BROKER WIT ...

  9. Angular14 Angular相关命令

    1 创建相关 1.1 创建项目 ng new 项目名  ->  创建新项目 ng new 项目名 --skip-install  -> 不进行模块安装  ng new 项目名 -si ng ...

  10. Entity Framework Code-First(9.10):DataAnnotations - NotMapped Attribute

    DataAnnotations - NotMapped Attribute: NotMapped attribute can be applied to properties of a class. ...