Lecture--9 Sorting
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的更多相关文章
- Google Interview University - 坚持完成这套学习手册,你就可以去 Google 面试了
作者:Glowin链接:https://zhuanlan.zhihu.com/p/22881223来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 原文地址:Google ...
- [C2P3] Andrew Ng - Machine Learning
##Advice for Applying Machine Learning Applying machine learning in practice is not always straightf ...
- HDU Cow Sorting (树状数组)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2838 Cow Sorting Problem Description Sherlock's N (1 ...
- 1306. Sorting Algorithm 2016 12 30
1306. Sorting Algorithm Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description One of the f ...
- 算法:POJ1007 DNA sorting
这题比较简单,重点应该在如何减少循环次数. package practice; import java.io.BufferedInputStream; import java.util.Map; im ...
- 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 ...
- U3D sorting layer, sort order, order in layer, layer深入辨析
1,layer是对游戏中所有物体的分类别划分,如UIlayer, waterlayer, 3DModelLayer, smallAssetsLayer, effectLayer等.将不同类的物体划分到 ...
- WebGrid with filtering, paging and sorting 【转】
WebGrid with filtering, paging and sorting by Jose M. Aguilar on April 24, 2012 in Web Development A ...
- 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 ...
- codeforces 499B.Lecture 解题报告
题目链接:http://codeforces.com/problemset/problem/499/B 题目意思:给出两种语言下 m 个单词表(word1, word2)的一一对应,以及 profes ...
随机推荐
- 用Fiddler2来监听HTTP(记:用skydrive sdk访问时,出错后用Fidder抓包分析)
最近在写一个关于如何上传文件到skydrive的demo, 用REST上传失败. 安装Telerik的Fiddler后, 可以监听http或者https通信, 然后可以在软件中看到返回的json数据或 ...
- Python:正则表达式(三)*、+、?的用法
一.功能*——表示匹配前面的字符0个或多个:+——表示前面的字符1个或多个:?——(1)放在其他字符后面:表示匹配0次或1次: (2)放在*.+后面:表示匹配尽可能少的字符 二.例 字符串fooooo ...
- UE3代码阅读需知
转自:http://www.cnblogs.com/hmxp8/archive/2012/02/21/2361211.html 掌握一款庞大的引擎,要一下子掌握真的很难,慢慢地从Editor,Scri ...
- Go中使用动态库C/C++库
转自:http://studygolang.com/articles/1441 最近需要做一些在go中使用动态C++库的工作,经常碰到找不到动态库路径这种情况,所以就花点时间,专门做一下实验来了解Go ...
- Ruby中的%表示法
%{String} 用于创建一个使用双引号括起来的字符串,这个表示法与%Q{String}完全一样 result = %{hello} puts "result is: #{result ...
- day18-事务与连接池 4.事务特性
- Command line option syntax error. Type Command /? for Help.
--------------------------- Microsoft Visual C++ 2005 Redistributable --------------------------- Co ...
- Flask11 Session、CSRF、注销session、利用端点自动跳转
1 怎么对存储的cookie数据进行加密 利用response对象去设置cookie时,存储到浏览器中的cookie数据都是明文的,容易被一些计算机爱好者利用:利用session存的cookie数据可 ...
- 一些奇怪的Javascript用法
阅读AngularJS时,看到一些奇怪的Javascript用法.1.(function(){ a.work=function(){} })(a) 声明一个匿名函数并执行 2. ...
- mahout过滤推荐结果 Recommender.recommend(long userID, int howMany, IDRescorer rescorer)
Recommender.recommend(uid, RECOMMENDER_NUM, rescorer); Recommender.recommend(long userID, int howMan ...