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 ...
随机推荐
- Maven运行JUnit测试(http://www.360doc.com/content/13/0927/15/7304817_317455642.shtml)
Maven单元测试 分类: maven 2012-05-09 15:17 1986人阅读 评论(1) 收藏 举报 maven测试junit单元测试javarandom 目录(?)[-] maven ...
- pip3 更改安装源
经常在使用Python的时候需要安装各种模块,而pip是很强大的模块安装工具,但是由于国外官方pypi经常被墙,导致不可用,所以我们最好是将自己使用的pip源更换一下,这样就能解决被墙导致的装不上库的 ...
- JVM插庄之二:Java agent基础原理
javaagent 简介 Javaagent 只要作用在class被加载之前对其加载,插入我们需要添加的字节码. Javaagent面向的是我们java程序员,而且agent都是用java编写的,不需 ...
- sql 根据年份、月份查询数据
CREATE TABLE [dbo].[T_UserAccess]( ,) NOT NULL, [UserId] [int] NULL, [UserType] [int] NULL, ) NULL, ...
- python 基础 列表 小例子
存主机ip到列表 host_list=[] netip='192.168.1' for hostip in range(1,254): ip = netip +str(hostip) host_lis ...
- 最近一直是web前段,没什么意思,所以就不发资料了
最近一直是web前段,没什么意思,所以就不发资料了 版权声明:本文为博主原创文章,未经博主允许不得转载.
- Centos6.5_64位系统下安装Oracle 11g
一.硬件要求 1.内存与Sweap:内存2G(以上),Sweap 2G(以上) 内存: 1-2G 2-16G 16G以上 Sweap: 1.5倍内存 1倍内存 16G 检查:# grep MemTot ...
- ORA-12504:tns:监听程序在 CONNECT_DATA中未获得SERVICE_NAME
在VS2008中创建一个数据源时,提示以下错误 “ORA-12504:tns:监听程序在 CONNECT_DATA中未获得SERVICE_NAME” 本机安装ORACLE客户端,找出以下路径的文件D: ...
- Window 显示鼠标的坐标
Window 显示鼠标的坐标 GetCursorPos(POINT *p)函数 windows.h头文件里面的GetCursorPos(POINT *p)函数是用来获取鼠标在屏幕中的坐标信息的. Ge ...
- Entity Framework Code-First(9.6):DataAnnotations - StringLength Attribute
DataAnnotations - StringLength Attribute: StringLength attribute can be applied to a string type pro ...